Showing posts with label sending. Show all posts
Showing posts with label sending. Show all posts

Thursday, March 29, 2012

error for sending(saving) data to database

Hi,

I’m using VS2005 and I’m trying to link the C# windows form to MSSQL. I used BindingNavigator Control to read the data. After that I add sqlcommand and sqldataadapter to send the data to the database. My code is this

sqlDataAdapter1.InsertCommand.CommandText =

"INSERT INTO SUDENT (" +

"S_ID, S_NAME, S_ADDRESS, S_PHONE" +

") VALUES ('" +

S_IDComboBox.Text +"', '" +

S_NAMETextBox.Text +"', '" +

S_ADDRESSTextBox.Text +"', '" +

S_PHONETextBox.Text +"')";

sqlDataAdapter1.InsertCommand.ExecuteNonQuery();

It gave me this error

NulleferenceException was Unhandled”and “ Object refrence not set to an instance of an object” so I add this line

sqlCommand1 = ("INSERT INTO STUDENT (S_ID,S_NAME,S_ADDRESS,S_PHONE) VALUES (@.S_ID,@.S_NAME,@.S_ADDRESS,@.S_PHONE)" ,sqlConnection1);

Then it gave me some stupid error. By the way it didn’t make a new object of SqlCommand although I tried ….

So can u help me to solve my problem ? Thank you

the first thing i noticed is "INSERT INTO SUDENT (" +

Spelling mistake

Madhu

|||Hi,

could you please send your whole code. As of now it seems that you did not initialize the DataAdapter object.

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de
|||

Form1.designer.cs

namespace student

{

partialclassForm1

{

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.IContainer components = null;

/// <summary>

/// Clean up any resources being used.

/// </summary>

/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

protectedoverridevoid Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

privatevoid InitializeComponent()

{

this.components = new System.ComponentModel.Container();

System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));

System.Windows.Forms.Label s_IDLabel;

System.Windows.Forms.Label s_NAMELabel;

System.Windows.Forms.Label s_ADDRESSLabel;

System.Windows.Forms.Label s_PHONELabel;

this.studentDataSet = new student.studentDataSet();

this.sTUDENTBindingSource = new System.Windows.Forms.BindingSource(this.components);

this.sTUDENTTableAdapter = new student.studentDataSetTableAdapters.STUDENTTableAdapter();

this.sTUDENTBindingNavigator = new System.Windows.Forms.BindingNavigator(this.components);

this.bindingNavigatorMoveFirstItem = new System.Windows.Forms.ToolStripButton();

this.bindingNavigatorMovePreviousItem = new System.Windows.Forms.ToolStripButton();

this.bindingNavigatorSeparator = new System.Windows.Forms.ToolStripSeparator();

this.bindingNavigatorPositionItem = new System.Windows.Forms.ToolStripTextBox();

this.bindingNavigatorCountItem = new System.Windows.Forms.ToolStripLabel();

this.bindingNavigatorSeparator1 = new System.Windows.Forms.ToolStripSeparator();

this.bindingNavigatorMoveNextItem = new System.Windows.Forms.ToolStripButton();

this.bindingNavigatorMoveLastItem = new System.Windows.Forms.ToolStripButton();

this.bindingNavigatorSeparator2 = new System.Windows.Forms.ToolStripSeparator();

this.bindingNavigatorAddNewItem = new System.Windows.Forms.ToolStripButton();

this.bindingNavigatorDeleteItem = new System.Windows.Forms.ToolStripButton();

this.sTUDENTBindingNavigatorSaveItem = new System.Windows.Forms.ToolStripButton();

this.s_IDTextBox = new System.Windows.Forms.TextBox();

this.s_NAMETextBox = new System.Windows.Forms.TextBox();

this.s_ADDRESSTextBox = new System.Windows.Forms.TextBox();

this.s_PHONETextBox = new System.Windows.Forms.TextBox();

this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();

this.sqlCommand1 = new System.Data.SqlClient.SqlCommand();

this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();

this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();

this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();

s_IDLabel = new System.Windows.Forms.Label();

s_NAMELabel = new System.Windows.Forms.Label();

s_ADDRESSLabel = new System.Windows.Forms.Label();

s_PHONELabel = new System.Windows.Forms.Label();

((System.ComponentModel.ISupportInitialize)(this.studentDataSet)).BeginInit();

((System.ComponentModel.ISupportInitialize)(this.sTUDENTBindingSource)).BeginInit();

((System.ComponentModel.ISupportInitialize)(this.sTUDENTBindingNavigator)).BeginInit();

this.sTUDENTBindingNavigator.SuspendLayout();

this.SuspendLayout();

//

// studentDataSet

//

this.studentDataSet.DataSetName = "studentDataSet";

this.studentDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;

//

// sTUDENTBindingSource

//

this.sTUDENTBindingSource.DataMember = "STUDENT";

this.sTUDENTBindingSource.DataSource = this.studentDataSet;

//

// sTUDENTTableAdapter

//

this.sTUDENTTableAdapter.ClearBeforeFill = true;

//

// sTUDENTBindingNavigator

//

this.sTUDENTBindingNavigator.AddNewItem = this.bindingNavigatorAddNewItem;

this.sTUDENTBindingNavigator.BindingSource = this.sTUDENTBindingSource;

this.sTUDENTBindingNavigator.CountItem = this.bindingNavigatorCountItem;

this.sTUDENTBindingNavigator.DeleteItem = this.bindingNavigatorDeleteItem;

this.sTUDENTBindingNavigator.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {

this.bindingNavigatorMoveFirstItem,

this.bindingNavigatorMovePreviousItem,

this.bindingNavigatorSeparator,

this.bindingNavigatorPositionItem,

this.bindingNavigatorCountItem,

this.bindingNavigatorSeparator1,

this.bindingNavigatorMoveNextItem,

this.bindingNavigatorMoveLastItem,

this.bindingNavigatorSeparator2,

this.bindingNavigatorAddNewItem,

this.bindingNavigatorDeleteItem,

this.sTUDENTBindingNavigatorSaveItem});

this.sTUDENTBindingNavigator.Location = new System.Drawing.Point(0, 0);

this.sTUDENTBindingNavigator.MoveFirstItem = this.bindingNavigatorMoveFirstItem;

this.sTUDENTBindingNavigator.MoveLastItem = this.bindingNavigatorMoveLastItem;

this.sTUDENTBindingNavigator.MoveNextItem = this.bindingNavigatorMoveNextItem;

this.sTUDENTBindingNavigator.MovePreviousItem = this.bindingNavigatorMovePreviousItem;

this.sTUDENTBindingNavigator.Name = "sTUDENTBindingNavigator";

this.sTUDENTBindingNavigator.PositionItem = this.bindingNavigatorPositionItem;

this.sTUDENTBindingNavigator.Size = new System.Drawing.Size(463, 25);

this.sTUDENTBindingNavigator.TabIndex = 0;

this.sTUDENTBindingNavigator.Text = "bindingNavigator1";

//

// bindingNavigatorMoveFirstItem

//

this.bindingNavigatorMoveFirstItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;

this.bindingNavigatorMoveFirstItem.Image = ((System.Drawing.Image)(resources.GetObject("bindingNavigatorMoveFirstItem.Image")));

this.bindingNavigatorMoveFirstItem.Name = "bindingNavigatorMoveFirstItem";

this.bindingNavigatorMoveFirstItem.RightToLeftAutoMirrorImage = true;

this.bindingNavigatorMoveFirstItem.Size = new System.Drawing.Size(23, 22);

this.bindingNavigatorMoveFirstItem.Text = "Move first";

//

// bindingNavigatorMovePreviousItem

//

this.bindingNavigatorMovePreviousItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;

this.bindingNavigatorMovePreviousItem.Image = ((System.Drawing.Image)(resources.GetObject("bindingNavigatorMovePreviousItem.Image")));

this.bindingNavigatorMovePreviousItem.Name = "bindingNavigatorMovePreviousItem";

this.bindingNavigatorMovePreviousItem.RightToLeftAutoMirrorImage = true;

this.bindingNavigatorMovePreviousItem.Size = new System.Drawing.Size(23, 22);

this.bindingNavigatorMovePreviousItem.Text = "Move previous";

//

// bindingNavigatorSeparator

//

this.bindingNavigatorSeparator.Name = "bindingNavigatorSeparator";

this.bindingNavigatorSeparator.Size = new System.Drawing.Size(6, 25);

//

// bindingNavigatorPositionItem

//

this.bindingNavigatorPositionItem.AccessibleName = "Position";

this.bindingNavigatorPositionItem.AutoSize = false;

this.bindingNavigatorPositionItem.Name = "bindingNavigatorPositionItem";

this.bindingNavigatorPositionItem.Size = new System.Drawing.Size(50, 21);

this.bindingNavigatorPositionItem.Text = "0";

this.bindingNavigatorPositionItem.ToolTipText = "Current position";

//

// bindingNavigatorCountItem

//

this.bindingNavigatorCountItem.Name = "bindingNavigatorCountItem";

this.bindingNavigatorCountItem.Size = new System.Drawing.Size(36, 22);

this.bindingNavigatorCountItem.Text = "of {0}";

this.bindingNavigatorCountItem.ToolTipText = "Total number of items";

//

// bindingNavigatorSeparator1

//

this.bindingNavigatorSeparator1.Name = "bindingNavigatorSeparator";

this.bindingNavigatorSeparator1.Size = new System.Drawing.Size(6, 25);

//

// bindingNavigatorMoveNextItem

//

this.bindingNavigatorMoveNextItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;

this.bindingNavigatorMoveNextItem.Image = ((System.Drawing.Image)(resources.GetObject("bindingNavigatorMoveNextItem.Image")));

this.bindingNavigatorMoveNextItem.Name = "bindingNavigatorMoveNextItem";

this.bindingNavigatorMoveNextItem.RightToLeftAutoMirrorImage = true;

this.bindingNavigatorMoveNextItem.Size = new System.Drawing.Size(23, 22);

this.bindingNavigatorMoveNextItem.Text = "Move next";

//

// bindingNavigatorMoveLastItem

//

this.bindingNavigatorMoveLastItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;

this.bindingNavigatorMoveLastItem.Image = ((System.Drawing.Image)(resources.GetObject("bindingNavigatorMoveLastItem.Image")));

this.bindingNavigatorMoveLastItem.Name = "bindingNavigatorMoveLastItem";

this.bindingNavigatorMoveLastItem.RightToLeftAutoMirrorImage = true;

this.bindingNavigatorMoveLastItem.Size = new System.Drawing.Size(23, 22);

this.bindingNavigatorMoveLastItem.Text = "Move last";

//

// bindingNavigatorSeparator2

//

this.bindingNavigatorSeparator2.Name = "bindingNavigatorSeparator";

this.bindingNavigatorSeparator2.Size = new System.Drawing.Size(6, 25);

//

// bindingNavigatorAddNewItem

//

this.bindingNavigatorAddNewItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;

this.bindingNavigatorAddNewItem.Image = ((System.Drawing.Image)(resources.GetObject("bindingNavigatorAddNewItem.Image")));

this.bindingNavigatorAddNewItem.Name = "bindingNavigatorAddNewItem";

this.bindingNavigatorAddNewItem.RightToLeftAutoMirrorImage = true;

this.bindingNavigatorAddNewItem.Size = new System.Drawing.Size(23, 22);

this.bindingNavigatorAddNewItem.Text = "Add new";

//

// bindingNavigatorDeleteItem

//

this.bindingNavigatorDeleteItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;

this.bindingNavigatorDeleteItem.Image = ((System.Drawing.Image)(resources.GetObject("bindingNavigatorDeleteItem.Image")));

this.bindingNavigatorDeleteItem.Name = "bindingNavigatorDeleteItem";

this.bindingNavigatorDeleteItem.RightToLeftAutoMirrorImage = true;

this.bindingNavigatorDeleteItem.Size = new System.Drawing.Size(23, 22);

this.bindingNavigatorDeleteItem.Text = "Delete";

//

// sTUDENTBindingNavigatorSaveItem

//

this.sTUDENTBindingNavigatorSaveItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;

this.sTUDENTBindingNavigatorSaveItem.Image = ((System.Drawing.Image)(resources.GetObject("sTUDENTBindingNavigatorSaveItem.Image")));

this.sTUDENTBindingNavigatorSaveItem.Name = "sTUDENTBindingNavigatorSaveItem";

this.sTUDENTBindingNavigatorSaveItem.Size = new System.Drawing.Size(23, 22);

this.sTUDENTBindingNavigatorSaveItem.Text = "Save Data";

this.sTUDENTBindingNavigatorSaveItem.Click += new System.EventHandler(this.sTUDENTBindingNavigatorSaveItem_Click);

//

// s_IDLabel

//

s_IDLabel.AutoSize = true;

s_IDLabel.Location = new System.Drawing.Point(44, 52);

s_IDLabel.Name = "s_IDLabel";

s_IDLabel.Size = new System.Drawing.Size(31, 13);

s_IDLabel.TabIndex = 1;

s_IDLabel.Text = "S ID:";

//

// s_IDTextBox

//

this.s_IDTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.sTUDENTBindingSource, "S_ID", true));

this.s_IDTextBox.Location = new System.Drawing.Point(81, 49);

this.s_IDTextBox.Name = "s_IDTextBox";

this.s_IDTextBox.Size = new System.Drawing.Size(100, 20);

this.s_IDTextBox.TabIndex = 2;

//

// s_NAMELabel

//

s_NAMELabel.AutoSize = true;

s_NAMELabel.Location = new System.Drawing.Point(51, 91);

s_NAMELabel.Name = "s_NAMELabel";

s_NAMELabel.Size = new System.Drawing.Size(51, 13);

s_NAMELabel.TabIndex = 3;

s_NAMELabel.Text = "S NAME:";

//

// s_NAMETextBox

//

this.s_NAMETextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.sTUDENTBindingSource, "S_NAME", true));

this.s_NAMETextBox.Location = new System.Drawing.Point(108, 88);

this.s_NAMETextBox.Name = "s_NAMETextBox";

this.s_NAMETextBox.Size = new System.Drawing.Size(100, 20);

this.s_NAMETextBox.TabIndex = 4;

//

// s_ADDRESSLabel

//

s_ADDRESSLabel.AutoSize = true;

s_ADDRESSLabel.Location = new System.Drawing.Point(49, 122);

s_ADDRESSLabel.Name = "s_ADDRESSLabel";

s_ADDRESSLabel.Size = new System.Drawing.Size(72, 13);

s_ADDRESSLabel.TabIndex = 5;

s_ADDRESSLabel.Text = "S ADDRESS:";

//

// s_ADDRESSTextBox

//

this.s_ADDRESSTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.sTUDENTBindingSource, "S_ADDRESS", true));

this.s_ADDRESSTextBox.Location = new System.Drawing.Point(127, 119);

this.s_ADDRESSTextBox.Name = "s_ADDRESSTextBox";

this.s_ADDRESSTextBox.Size = new System.Drawing.Size(100, 20);

this.s_ADDRESSTextBox.TabIndex = 6;

//

// s_PHONELabel

//

s_PHONELabel.AutoSize = true;

s_PHONELabel.Location = new System.Drawing.Point(77, 160);

s_PHONELabel.Name = "s_PHONELabel";

s_PHONELabel.Size = new System.Drawing.Size(58, 13);

s_PHONELabel.TabIndex = 7;

s_PHONELabel.Text = "S PHONE:";

//

// s_PHONETextBox

//

this.s_PHONETextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.sTUDENTBindingSource, "S_PHONE", true));

this.s_PHONETextBox.Location = new System.Drawing.Point(141, 157);

this.s_PHONETextBox.Name = "s_PHONETextBox";

this.s_PHONETextBox.Size = new System.Drawing.Size(100, 20);

this.s_PHONETextBox.TabIndex = 8;

//

// sqlConnection1

//

this.sqlConnection1.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=\"C:\\Documents and Settings\\Glad\\Desktop" +

"\\student\\student\\student.mdf\";Integrated Security=True;User Instance=True";

this.sqlConnection1.FireInfoMessageEventOnUserErrors = true;

this.sqlConnection1.StatisticsEnabled = true;

//

// sqlCommand1

//

this.sqlCommand1.Parameters.AddRange(new System.Data.SqlClient.SqlParameter[] {

new System.Data.SqlClient.SqlParameter("@.S_ID", System.Data.SqlDbType.Int, 0, System.Data.ParameterDirection.Output, false, ((byte)(0)), ((byte)(0)), "", System.Data.DataRowVersion.Current, null),

new System.Data.SqlClient.SqlParameter("@.S_NAME", System.Data.SqlDbType.VarChar),

new System.Data.SqlClient.SqlParameter("@.S_ADDRESS", System.Data.SqlDbType.VarChar),

new System.Data.SqlClient.SqlParameter("@.S_PHONE", System.Data.SqlDbType.Text)});

//

// sqlSelectCommand1

//

this.sqlSelectCommand1.CommandText = "SELECT S_ID, S_NAME, S_ADDRESS, S_PHONE\r\nFROM STUDENT";

this.sqlSelectCommand1.Connection = this.sqlConnection1;

//

// sqlInsertCommand1

//

this.sqlInsertCommand1.CommandText = "INSERT INTO [STUDENT] ([S_ID], [S_NAME], [S_ADDRESS], [S_PHONE]) VALUES (@.S_ID, @." +

"S_NAME, @.S_ADDRESS, @.S_PHONE)";

this.sqlInsertCommand1.Connection = this.sqlConnection1;

this.sqlInsertCommand1.Parameters.AddRange(new System.Data.SqlClient.SqlParameter[] {

new System.Data.SqlClient.SqlParameter("@.S_ID", System.Data.SqlDbType.Int, 0, "S_ID"),

new System.Data.SqlClient.SqlParameter("@.S_NAME", System.Data.SqlDbType.VarChar, 0, "S_NAME"),

new System.Data.SqlClient.SqlParameter("@.S_ADDRESS", System.Data.SqlDbType.VarChar, 0, "S_ADDRESS"),

new System.Data.SqlClient.SqlParameter("@.S_PHONE", System.Data.SqlDbType.Text, 0, "S_PHONE")});

//

// sqlDataAdapter1

//

this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;

this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;

this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {

new System.Data.Common.DataTableMapping("Table", "STUDENT", new System.Data.Common.DataColumnMapping[] {

new System.Data.Common.DataColumnMapping("S_ID", "S_ID"),

new System.Data.Common.DataColumnMapping("S_NAME", "S_NAME"),

new System.Data.Common.DataColumnMapping("S_ADDRESS", "S_ADDRESS"),

new System.Data.Common.DataColumnMapping("S_PHONE", "S_PHONE")})});

//

// Form1

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(463, 266);

this.Controls.Add(s_PHONELabel);

this.Controls.Add(this.s_PHONETextBox);

this.Controls.Add(s_ADDRESSLabel);

this.Controls.Add(this.s_ADDRESSTextBox);

this.Controls.Add(s_NAMELabel);

this.Controls.Add(this.s_NAMETextBox);

this.Controls.Add(s_IDLabel);

this.Controls.Add(this.s_IDTextBox);

this.Controls.Add(this.sTUDENTBindingNavigator);

this.Name = "Form1";

this.Text = "Form1";

this.Load += new System.EventHandler(this.Form1_Load);

((System.ComponentModel.ISupportInitialize)(this.studentDataSet)).EndInit();

((System.ComponentModel.ISupportInitialize)(this.sTUDENTBindingSource)).EndInit();

((System.ComponentModel.ISupportInitialize)(this.sTUDENTBindingNavigator)).EndInit();

this.sTUDENTBindingNavigator.ResumeLayout(false);

this.sTUDENTBindingNavigator.PerformLayout();

this.ResumeLayout(false);

this.PerformLayout();

}

#endregion

privatestudentDataSet studentDataSet;

private System.Windows.Forms.BindingSource sTUDENTBindingSource;

private student.studentDataSetTableAdapters.STUDENTTableAdapter sTUDENTTableAdapter;

private System.Windows.Forms.BindingNavigator sTUDENTBindingNavigator;

private System.Windows.Forms.ToolStripButton bindingNavigatorAddNewItem;

private System.Windows.Forms.ToolStripLabel bindingNavigatorCountItem;

private System.Windows.Forms.ToolStripButton bindingNavigatorDeleteItem;

private System.Windows.Forms.ToolStripButton bindingNavigatorMoveFirstItem;

private System.Windows.Forms.ToolStripButton bindingNavigatorMovePreviousItem;

private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator;

private System.Windows.Forms.ToolStripTextBox bindingNavigatorPositionItem;

private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator1;

private System.Windows.Forms.ToolStripButton bindingNavigatorMoveNextItem;

private System.Windows.Forms.ToolStripButton bindingNavigatorMoveLastItem;

private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator2;

private System.Windows.Forms.ToolStripButton sTUDENTBindingNavigatorSaveItem;

private System.Windows.Forms.TextBox s_IDTextBox;

private System.Windows.Forms.TextBox s_NAMETextBox;

private System.Windows.Forms.TextBox s_ADDRESSTextBox;

private System.Windows.Forms.TextBox s_PHONETextBox;

private System.Data.SqlClient.SqlConnection sqlConnection1;

private System.Data.SqlClient.SqlCommand sqlCommand1;

private System.Data.SqlClient.SqlCommand sqlSelectCommand1;

private System.Data.SqlClient.SqlCommand sqlInsertCommand1;

private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;

}

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

form1.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace student

{

publicpartialclassForm1 : Form

{

public Form1()

{

InitializeComponent();

}

privatevoid sTUDENTBindingNavigatorSaveItem_Click(object sender, EventArgs e)

{

sqlConnection1.Open();

sqlDataAdapter1.InsertCommand.CommandText=

"INSERT INTO STUDENT (" +

"S_ID, S_NAME, S_ADDRESS, S_PHONE" +

") VALUES ('" +

s_IDTextBox.Text + "', '" +

s_NAMETextBox.Text + "', '" +

s_ADDRESSTextBox.Text + "', '" +

s_PHONETextBox.Text + "')";

sqlDataAdapter1.InsertCommand.ExecuteNonQuery();

sqlConnection1.Close();

this.Validate();

this.sTUDENTBindingSource.EndEdit();

this.sTUDENTTableAdapter.Update(this.studentDataSet.STUDENT);

}

privatevoid Form1_Load(object sender, EventArgs e)

{

this.sTUDENTTableAdapter.Fill(this.studentDataSet.STUDENT);

}

}

}

|||Does the problem still exist ?

Jens K. Suessmeyer.

http://www.sqlserver2005.de

error for sending(saving) data to database

Hi,

I’m using VS2005 and I’m trying to link the C# windows form to MSSQL. I used BindingNavigator Control to read the data. After that I add sqlcommand and sqldataadapter to send the data to the database. My code is this

sqlDataAdapter1.InsertCommand.CommandText =

"INSERT INTO SUDENT (" +

"S_ID, S_NAME, S_ADDRESS, S_PHONE" +

") VALUES ('" +

S_IDComboBox.Text + "', '" +

S_NAMETextBox.Text + "', '" +

S_ADDRESSTextBox.Text + "', '" +

S_PHONETextBox.Text + "')";

sqlDataAdapter1.InsertCommand.ExecuteNonQuery();

It gave me this error

NulleferenceException was Unhandled”and “ Object refrence not set to an instance of an object” so I add this line

sqlCommand1 = ("INSERT INTO STUDENT (S_ID,S_NAME,S_ADDRESS,S_PHONE) VALUES (@.S_ID,@.S_NAME,@.S_ADDRESS,@.S_PHONE)" ,sqlConnection1);

Then it gave me some stupid error. By the way it didn’t make a new object of SqlCommand although I tried ….

So can u help me to solve my problem ? Thank you

the first thing i noticed is "INSERT INTO SUDENT (" +

Spelling mistake

Madhu

|||Hi,

could you please send your whole code. As of now it seems that you did not initialize the DataAdapter object.

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de|||

Form1.designer.cs

namespace student

{

partial class Form1

{

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.IContainer components = null;

/// <summary>

/// Clean up any resources being used.

/// </summary>

/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.components = new System.ComponentModel.Container();

System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));

System.Windows.Forms.Label s_IDLabel;

System.Windows.Forms.Label s_NAMELabel;

System.Windows.Forms.Label s_ADDRESSLabel;

System.Windows.Forms.Label s_PHONELabel;

this.studentDataSet = new student.studentDataSet();

this.sTUDENTBindingSource = new System.Windows.Forms.BindingSource(this.components);

this.sTUDENTTableAdapter = new student.studentDataSetTableAdapters.STUDENTTableAdapter();

this.sTUDENTBindingNavigator = new System.Windows.Forms.BindingNavigator(this.components);

this.bindingNavigatorMoveFirstItem = new System.Windows.Forms.ToolStripButton();

this.bindingNavigatorMovePreviousItem = new System.Windows.Forms.ToolStripButton();

this.bindingNavigatorSeparator = new System.Windows.Forms.ToolStripSeparator();

this.bindingNavigatorPositionItem = new System.Windows.Forms.ToolStripTextBox();

this.bindingNavigatorCountItem = new System.Windows.Forms.ToolStripLabel();

this.bindingNavigatorSeparator1 = new System.Windows.Forms.ToolStripSeparator();

this.bindingNavigatorMoveNextItem = new System.Windows.Forms.ToolStripButton();

this.bindingNavigatorMoveLastItem = new System.Windows.Forms.ToolStripButton();

this.bindingNavigatorSeparator2 = new System.Windows.Forms.ToolStripSeparator();

this.bindingNavigatorAddNewItem = new System.Windows.Forms.ToolStripButton();

this.bindingNavigatorDeleteItem = new System.Windows.Forms.ToolStripButton();

this.sTUDENTBindingNavigatorSaveItem = new System.Windows.Forms.ToolStripButton();

this.s_IDTextBox = new System.Windows.Forms.TextBox();

this.s_NAMETextBox = new System.Windows.Forms.TextBox();

this.s_ADDRESSTextBox = new System.Windows.Forms.TextBox();

this.s_PHONETextBox = new System.Windows.Forms.TextBox();

this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();

this.sqlCommand1 = new System.Data.SqlClient.SqlCommand();

this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();

this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();

this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();

s_IDLabel = new System.Windows.Forms.Label();

s_NAMELabel = new System.Windows.Forms.Label();

s_ADDRESSLabel = new System.Windows.Forms.Label();

s_PHONELabel = new System.Windows.Forms.Label();

((System.ComponentModel.ISupportInitialize)(this.studentDataSet)).BeginInit();

((System.ComponentModel.ISupportInitialize)(this.sTUDENTBindingSource)).BeginInit();

((System.ComponentModel.ISupportInitialize)(this.sTUDENTBindingNavigator)).BeginInit();

this.sTUDENTBindingNavigator.SuspendLayout();

this.SuspendLayout();

//

// studentDataSet

//

this.studentDataSet.DataSetName = "studentDataSet";

this.studentDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;

//

// sTUDENTBindingSource

//

this.sTUDENTBindingSource.DataMember = "STUDENT";

this.sTUDENTBindingSource.DataSource = this.studentDataSet;

//

// sTUDENTTableAdapter

//

this.sTUDENTTableAdapter.ClearBeforeFill = true;

//

// sTUDENTBindingNavigator

//

this.sTUDENTBindingNavigator.AddNewItem = this.bindingNavigatorAddNewItem;

this.sTUDENTBindingNavigator.BindingSource = this.sTUDENTBindingSource;

this.sTUDENTBindingNavigator.CountItem = this.bindingNavigatorCountItem;

this.sTUDENTBindingNavigator.DeleteItem = this.bindingNavigatorDeleteItem;

this.sTUDENTBindingNavigator.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {

this.bindingNavigatorMoveFirstItem,

this.bindingNavigatorMovePreviousItem,

this.bindingNavigatorSeparator,

this.bindingNavigatorPositionItem,

this.bindingNavigatorCountItem,

this.bindingNavigatorSeparator1,

this.bindingNavigatorMoveNextItem,

this.bindingNavigatorMoveLastItem,

this.bindingNavigatorSeparator2,

this.bindingNavigatorAddNewItem,

this.bindingNavigatorDeleteItem,

this.sTUDENTBindingNavigatorSaveItem});

this.sTUDENTBindingNavigator.Location = new System.Drawing.Point(0, 0);

this.sTUDENTBindingNavigator.MoveFirstItem = this.bindingNavigatorMoveFirstItem;

this.sTUDENTBindingNavigator.MoveLastItem = this.bindingNavigatorMoveLastItem;

this.sTUDENTBindingNavigator.MoveNextItem = this.bindingNavigatorMoveNextItem;

this.sTUDENTBindingNavigator.MovePreviousItem = this.bindingNavigatorMovePreviousItem;

this.sTUDENTBindingNavigator.Name = "sTUDENTBindingNavigator";

this.sTUDENTBindingNavigator.PositionItem = this.bindingNavigatorPositionItem;

this.sTUDENTBindingNavigator.Size = new System.Drawing.Size(463, 25);

this.sTUDENTBindingNavigator.TabIndex = 0;

this.sTUDENTBindingNavigator.Text = "bindingNavigator1";

//

// bindingNavigatorMoveFirstItem

//

this.bindingNavigatorMoveFirstItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;

this.bindingNavigatorMoveFirstItem.Image = ((System.Drawing.Image)(resources.GetObject("bindingNavigatorMoveFirstItem.Image")));

this.bindingNavigatorMoveFirstItem.Name = "bindingNavigatorMoveFirstItem";

this.bindingNavigatorMoveFirstItem.RightToLeftAutoMirrorImage = true;

this.bindingNavigatorMoveFirstItem.Size = new System.Drawing.Size(23, 22);

this.bindingNavigatorMoveFirstItem.Text = "Move first";

//

// bindingNavigatorMovePreviousItem

//

this.bindingNavigatorMovePreviousItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;

this.bindingNavigatorMovePreviousItem.Image = ((System.Drawing.Image)(resources.GetObject("bindingNavigatorMovePreviousItem.Image")));

this.bindingNavigatorMovePreviousItem.Name = "bindingNavigatorMovePreviousItem";

this.bindingNavigatorMovePreviousItem.RightToLeftAutoMirrorImage = true;

this.bindingNavigatorMovePreviousItem.Size = new System.Drawing.Size(23, 22);

this.bindingNavigatorMovePreviousItem.Text = "Move previous";

//

// bindingNavigatorSeparator

//

this.bindingNavigatorSeparator.Name = "bindingNavigatorSeparator";

this.bindingNavigatorSeparator.Size = new System.Drawing.Size(6, 25);

//

// bindingNavigatorPositionItem

//

this.bindingNavigatorPositionItem.AccessibleName = "Position";

this.bindingNavigatorPositionItem.AutoSize = false;

this.bindingNavigatorPositionItem.Name = "bindingNavigatorPositionItem";

this.bindingNavigatorPositionItem.Size = new System.Drawing.Size(50, 21);

this.bindingNavigatorPositionItem.Text = "0";

this.bindingNavigatorPositionItem.ToolTipText = "Current position";

//

// bindingNavigatorCountItem

//

this.bindingNavigatorCountItem.Name = "bindingNavigatorCountItem";

this.bindingNavigatorCountItem.Size = new System.Drawing.Size(36, 22);

this.bindingNavigatorCountItem.Text = "of {0}";

this.bindingNavigatorCountItem.ToolTipText = "Total number of items";

//

// bindingNavigatorSeparator1

//

this.bindingNavigatorSeparator1.Name = "bindingNavigatorSeparator";

this.bindingNavigatorSeparator1.Size = new System.Drawing.Size(6, 25);

//

// bindingNavigatorMoveNextItem

//

this.bindingNavigatorMoveNextItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;

this.bindingNavigatorMoveNextItem.Image = ((System.Drawing.Image)(resources.GetObject("bindingNavigatorMoveNextItem.Image")));

this.bindingNavigatorMoveNextItem.Name = "bindingNavigatorMoveNextItem";

this.bindingNavigatorMoveNextItem.RightToLeftAutoMirrorImage = true;

this.bindingNavigatorMoveNextItem.Size = new System.Drawing.Size(23, 22);

this.bindingNavigatorMoveNextItem.Text = "Move next";

//

// bindingNavigatorMoveLastItem

//

this.bindingNavigatorMoveLastItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;

this.bindingNavigatorMoveLastItem.Image = ((System.Drawing.Image)(resources.GetObject("bindingNavigatorMoveLastItem.Image")));

this.bindingNavigatorMoveLastItem.Name = "bindingNavigatorMoveLastItem";

this.bindingNavigatorMoveLastItem.RightToLeftAutoMirrorImage = true;

this.bindingNavigatorMoveLastItem.Size = new System.Drawing.Size(23, 22);

this.bindingNavigatorMoveLastItem.Text = "Move last";

//

// bindingNavigatorSeparator2

//

this.bindingNavigatorSeparator2.Name = "bindingNavigatorSeparator";

this.bindingNavigatorSeparator2.Size = new System.Drawing.Size(6, 25);

//

// bindingNavigatorAddNewItem

//

this.bindingNavigatorAddNewItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;

this.bindingNavigatorAddNewItem.Image = ((System.Drawing.Image)(resources.GetObject("bindingNavigatorAddNewItem.Image")));

this.bindingNavigatorAddNewItem.Name = "bindingNavigatorAddNewItem";

this.bindingNavigatorAddNewItem.RightToLeftAutoMirrorImage = true;

this.bindingNavigatorAddNewItem.Size = new System.Drawing.Size(23, 22);

this.bindingNavigatorAddNewItem.Text = "Add new";

//

// bindingNavigatorDeleteItem

//

this.bindingNavigatorDeleteItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;

this.bindingNavigatorDeleteItem.Image = ((System.Drawing.Image)(resources.GetObject("bindingNavigatorDeleteItem.Image")));

this.bindingNavigatorDeleteItem.Name = "bindingNavigatorDeleteItem";

this.bindingNavigatorDeleteItem.RightToLeftAutoMirrorImage = true;

this.bindingNavigatorDeleteItem.Size = new System.Drawing.Size(23, 22);

this.bindingNavigatorDeleteItem.Text = "Delete";

//

// sTUDENTBindingNavigatorSaveItem

//

this.sTUDENTBindingNavigatorSaveItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;

this.sTUDENTBindingNavigatorSaveItem.Image = ((System.Drawing.Image)(resources.GetObject("sTUDENTBindingNavigatorSaveItem.Image")));

this.sTUDENTBindingNavigatorSaveItem.Name = "sTUDENTBindingNavigatorSaveItem";

this.sTUDENTBindingNavigatorSaveItem.Size = new System.Drawing.Size(23, 22);

this.sTUDENTBindingNavigatorSaveItem.Text = "Save Data";

this.sTUDENTBindingNavigatorSaveItem.Click += new System.EventHandler(this.sTUDENTBindingNavigatorSaveItem_Click);

//

// s_IDLabel

//

s_IDLabel.AutoSize = true;

s_IDLabel.Location = new System.Drawing.Point(44, 52);

s_IDLabel.Name = "s_IDLabel";

s_IDLabel.Size = new System.Drawing.Size(31, 13);

s_IDLabel.TabIndex = 1;

s_IDLabel.Text = "S ID:";

//

// s_IDTextBox

//

this.s_IDTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.sTUDENTBindingSource, "S_ID", true));

this.s_IDTextBox.Location = new System.Drawing.Point(81, 49);

this.s_IDTextBox.Name = "s_IDTextBox";

this.s_IDTextBox.Size = new System.Drawing.Size(100, 20);

this.s_IDTextBox.TabIndex = 2;

//

// s_NAMELabel

//

s_NAMELabel.AutoSize = true;

s_NAMELabel.Location = new System.Drawing.Point(51, 91);

s_NAMELabel.Name = "s_NAMELabel";

s_NAMELabel.Size = new System.Drawing.Size(51, 13);

s_NAMELabel.TabIndex = 3;

s_NAMELabel.Text = "S NAME:";

//

// s_NAMETextBox

//

this.s_NAMETextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.sTUDENTBindingSource, "S_NAME", true));

this.s_NAMETextBox.Location = new System.Drawing.Point(108, 88);

this.s_NAMETextBox.Name = "s_NAMETextBox";

this.s_NAMETextBox.Size = new System.Drawing.Size(100, 20);

this.s_NAMETextBox.TabIndex = 4;

//

// s_ADDRESSLabel

//

s_ADDRESSLabel.AutoSize = true;

s_ADDRESSLabel.Location = new System.Drawing.Point(49, 122);

s_ADDRESSLabel.Name = "s_ADDRESSLabel";

s_ADDRESSLabel.Size = new System.Drawing.Size(72, 13);

s_ADDRESSLabel.TabIndex = 5;

s_ADDRESSLabel.Text = "S ADDRESS:";

//

// s_ADDRESSTextBox

//

this.s_ADDRESSTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.sTUDENTBindingSource, "S_ADDRESS", true));

this.s_ADDRESSTextBox.Location = new System.Drawing.Point(127, 119);

this.s_ADDRESSTextBox.Name = "s_ADDRESSTextBox";

this.s_ADDRESSTextBox.Size = new System.Drawing.Size(100, 20);

this.s_ADDRESSTextBox.TabIndex = 6;

//

// s_PHONELabel

//

s_PHONELabel.AutoSize = true;

s_PHONELabel.Location = new System.Drawing.Point(77, 160);

s_PHONELabel.Name = "s_PHONELabel";

s_PHONELabel.Size = new System.Drawing.Size(58, 13);

s_PHONELabel.TabIndex = 7;

s_PHONELabel.Text = "S PHONE:";

//

// s_PHONETextBox

//

this.s_PHONETextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.sTUDENTBindingSource, "S_PHONE", true));

this.s_PHONETextBox.Location = new System.Drawing.Point(141, 157);

this.s_PHONETextBox.Name = "s_PHONETextBox";

this.s_PHONETextBox.Size = new System.Drawing.Size(100, 20);

this.s_PHONETextBox.TabIndex = 8;

//

// sqlConnection1

//

this.sqlConnection1.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=\"C:\\Documents and Settings\\Glad\\Desktop" +

"\\student\\student\\student.mdf\";Integrated Security=True;User Instance=True";

this.sqlConnection1.FireInfoMessageEventOnUserErrors = true;

this.sqlConnection1.StatisticsEnabled = true;

//

// sqlCommand1

//

this.sqlCommand1.Parameters.AddRange(new System.Data.SqlClient.SqlParameter[] {

new System.Data.SqlClient.SqlParameter("@.S_ID", System.Data.SqlDbType.Int, 0, System.Data.ParameterDirection.Output, false, ((byte)(0)), ((byte)(0)), "", System.Data.DataRowVersion.Current, null),

new System.Data.SqlClient.SqlParameter("@.S_NAME", System.Data.SqlDbType.VarChar),

new System.Data.SqlClient.SqlParameter("@.S_ADDRESS", System.Data.SqlDbType.VarChar),

new System.Data.SqlClient.SqlParameter("@.S_PHONE", System.Data.SqlDbType.Text)});

//

// sqlSelectCommand1

//

this.sqlSelectCommand1.CommandText = "SELECT S_ID, S_NAME, S_ADDRESS, S_PHONE\r\nFROM STUDENT";

this.sqlSelectCommand1.Connection = this.sqlConnection1;

//

// sqlInsertCommand1

//

this.sqlInsertCommand1.CommandText = "INSERT INTO [STUDENT] ([S_ID], [S_NAME], [S_ADDRESS], [S_PHONE]) VALUES (@.S_ID, @." +

"S_NAME, @.S_ADDRESS, @.S_PHONE)";

this.sqlInsertCommand1.Connection = this.sqlConnection1;

this.sqlInsertCommand1.Parameters.AddRange(new System.Data.SqlClient.SqlParameter[] {

new System.Data.SqlClient.SqlParameter("@.S_ID", System.Data.SqlDbType.Int, 0, "S_ID"),

new System.Data.SqlClient.SqlParameter("@.S_NAME", System.Data.SqlDbType.VarChar, 0, "S_NAME"),

new System.Data.SqlClient.SqlParameter("@.S_ADDRESS", System.Data.SqlDbType.VarChar, 0, "S_ADDRESS"),

new System.Data.SqlClient.SqlParameter("@.S_PHONE", System.Data.SqlDbType.Text, 0, "S_PHONE")});

//

// sqlDataAdapter1

//

this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;

this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;

this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {

new System.Data.Common.DataTableMapping("Table", "STUDENT", new System.Data.Common.DataColumnMapping[] {

new System.Data.Common.DataColumnMapping("S_ID", "S_ID"),

new System.Data.Common.DataColumnMapping("S_NAME", "S_NAME"),

new System.Data.Common.DataColumnMapping("S_ADDRESS", "S_ADDRESS"),

new System.Data.Common.DataColumnMapping("S_PHONE", "S_PHONE")})});

//

// Form1

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(463, 266);

this.Controls.Add(s_PHONELabel);

this.Controls.Add(this.s_PHONETextBox);

this.Controls.Add(s_ADDRESSLabel);

this.Controls.Add(this.s_ADDRESSTextBox);

this.Controls.Add(s_NAMELabel);

this.Controls.Add(this.s_NAMETextBox);

this.Controls.Add(s_IDLabel);

this.Controls.Add(this.s_IDTextBox);

this.Controls.Add(this.sTUDENTBindingNavigator);

this.Name = "Form1";

this.Text = "Form1";

this.Load += new System.EventHandler(this.Form1_Load);

((System.ComponentModel.ISupportInitialize)(this.studentDataSet)).EndInit();

((System.ComponentModel.ISupportInitialize)(this.sTUDENTBindingSource)).EndInit();

((System.ComponentModel.ISupportInitialize)(this.sTUDENTBindingNavigator)).EndInit();

this.sTUDENTBindingNavigator.ResumeLayout(false);

this.sTUDENTBindingNavigator.PerformLayout();

this.ResumeLayout(false);

this.PerformLayout();

}

#endregion

private studentDataSet studentDataSet;

private System.Windows.Forms.BindingSource sTUDENTBindingSource;

private student.studentDataSetTableAdapters.STUDENTTableAdapter sTUDENTTableAdapter;

private System.Windows.Forms.BindingNavigator sTUDENTBindingNavigator;

private System.Windows.Forms.ToolStripButton bindingNavigatorAddNewItem;

private System.Windows.Forms.ToolStripLabel bindingNavigatorCountItem;

private System.Windows.Forms.ToolStripButton bindingNavigatorDeleteItem;

private System.Windows.Forms.ToolStripButton bindingNavigatorMoveFirstItem;

private System.Windows.Forms.ToolStripButton bindingNavigatorMovePreviousItem;

private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator;

private System.Windows.Forms.ToolStripTextBox bindingNavigatorPositionItem;

private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator1;

private System.Windows.Forms.ToolStripButton bindingNavigatorMoveNextItem;

private System.Windows.Forms.ToolStripButton bindingNavigatorMoveLastItem;

private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator2;

private System.Windows.Forms.ToolStripButton sTUDENTBindingNavigatorSaveItem;

private System.Windows.Forms.TextBox s_IDTextBox;

private System.Windows.Forms.TextBox s_NAMETextBox;

private System.Windows.Forms.TextBox s_ADDRESSTextBox;

private System.Windows.Forms.TextBox s_PHONETextBox;

private System.Data.SqlClient.SqlConnection sqlConnection1;

private System.Data.SqlClient.SqlCommand sqlCommand1;

private System.Data.SqlClient.SqlCommand sqlSelectCommand1;

private System.Data.SqlClient.SqlCommand sqlInsertCommand1;

private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;

}

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

form1.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace student

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void sTUDENTBindingNavigatorSaveItem_Click(object sender, EventArgs e)

{

sqlConnection1.Open();

sqlDataAdapter1.InsertCommand.CommandText=

"INSERT INTO STUDENT (" +

"S_ID, S_NAME, S_ADDRESS, S_PHONE" +

") VALUES ('" +

s_IDTextBox.Text + "', '" +

s_NAMETextBox.Text + "', '" +

s_ADDRESSTextBox.Text + "', '" +

s_PHONETextBox.Text + "')";

sqlDataAdapter1.InsertCommand.ExecuteNonQuery();

sqlConnection1.Close();

this.Validate();

this.sTUDENTBindingSource.EndEdit();

this.sTUDENTTableAdapter.Update(this.studentDataSet.STUDENT);

}

private void Form1_Load(object sender, EventArgs e)

{

this.sTUDENTTableAdapter.Fill(this.studentDataSet.STUDENT);

}

}

}

|||Does the problem still exist ?

Jens K. Suessmeyer.

http://www.sqlserver2005.de

error for sending(saving) data to database

Hi,
Im using VS2005 and Im trying to link the C# windows form to MSSQL. I used BindingNavigator Control to read the data. After that I add sqlcommand and sqldataadapter to send the data to the database. My code is this

sqlDataAdapter1.InsertCommand.CommandText =
"INSERT INTO SUDENT (" +
"S_ID, S_NAME, S_ADDRESS, S_PHONE" +
") VALUES ('" +
S_IDComboBox.Text + "', '" +
S_NAMETextBox.Text + "', '" +
S_ADDRESSTextBox.Text + "', '" +
S_PHONETextBox.Text + "')";

sqlDataAdapter1.InsertCommand.ExecuteNonQuery();

It gave me this error
NulleferenceException was Unhandled and Object refrence not set to an instance of an object so I add this line

sqlCommand1 = ("INSERT INTO STUDENT (S_ID,S_NAME,S_ADDRESS,S_PHONE) VALUES (@.S_ID,@.S_NAME,@.S_ADDRESS,@.S_PHONE)" ,sqlConnection1);

Then it gave me some stupid error. By the way it didnt make a new object of SqlCommand although I tried .

So can u help me to solve my problem ? Thank youHi. the only object reference I can see in your code is:
sqlDataAdapter1
You could check if this is the correct spelling or if indeed it exists as a valid object.|||hi thank you for reply.
i correct the spelling but it still gave me the same error.|||

Quote:

Originally Posted by csharpa

hi thank you for reply.
i correct the spelling but it still gave me the same error.


Interesting. You corrected the spelling to what?sql

error for sending(saving) data to database

Hi,
I'm using VS2005 and I'm trying to link the C# windows form to MSSQL.
I used BindingNavigator Control to read the data. After that I add
sqlcommand and sqldataadapter to send the data to the database. My
code is this

sqlDataAdapter1. InsertCommand. CommandText =
"INSERT INTO SUDENT (" +
"S_ID, S_NAME, S_ADDRESS, S_PHONE" +
") VALUES ('" +
S_IDComboBox. Text + "', '" +
S_NAMETextBox. Text + "', '" +
S_ADDRESSTextBox. Text + "', '" +
S_PHONETextBox. Text + "')";

sqlDataAdapter1. InsertCommand. ExecuteNonQuery( );

It gave me this error
"NulleferenceExcept ion was Unhandled"and " Object refrence not set to
an instance of an object" so I add this line

sqlCommand1 = ("INSERT INTO STUDENT (S_ID,S_NAME, S_ADDRESS, S_PHONE)
VALUES (@.S_ID,@.S_NAME, @.S_ADDRESS, @.S_PHONE) " ,sqlConnection1) ;

Then it gave me some stupid error. By the way it didn't make a new
object of SqlCommand although I tired ...

So can u help me to solve my problem ? Thank you(projectid2007@.yahoo.com) writes:

Quote:

Originally Posted by

I'm using VS2005 and I'm trying to link the C# windows form to MSSQL.
I used BindingNavigator Control to read the data. After that I add
sqlcommand and sqldataadapter to send the data to the database. My
code is this
>
sqlDataAdapter1. InsertCommand. CommandText =
"INSERT INTO SUDENT (" +
"S_ID, S_NAME, S_ADDRESS, S_PHONE" +
") VALUES ('" +
S_IDComboBox. Text + "', '" +
S_NAMETextBox. Text + "', '" +
S_ADDRESSTextBox. Text + "', '" +
S_PHONETextBox. Text + "')";


Since you ask this in an SQL Server newsgroup, I will tell you what is
wrong with this query from an SQL Server perspective. What do you think
happens if the user in the PhoneTextBoxs types:

'); DROP TABLE SUDENT'

Oops! Table gone!

You should use parameterised commands:

"INSERT INTO SUDENT (" +
"S_ID, S_NAME, S_ADDRESS, S_PHONE" +
") VALUES (@.ID, @.Name, @.Address, @.Phone)"

and then use the CreateParameter method to define your parameters.

Quote:

Originally Posted by

sqlDataAdapter1. InsertCommand. ExecuteNonQuery( );
>
It gave me this error
"NulleferenceExcept ion was Unhandled"and " Object refrence not set to
an instance of an object" so I add this line
>
sqlCommand1 = ("INSERT INTO STUDENT (S_ID,S_NAME, S_ADDRESS, S_PHONE)
VALUES (@.S_ID,@.S_NAME, @.S_ADDRESS, @.S_PHONE) " ,sqlConnection1) ;
>
Then it gave me some stupid error. By the way it didn't make a new
object of SqlCommand although I tired ...
>
So can u help me to solve my problem ? Thank you


You are probably better off asking in a .Net newsgroup about how to
write .Net programs. Particularly, if you want to link them to
forms.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx