Showing posts with label control. Show all posts
Showing posts with label control. 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

Monday, March 19, 2012

Error creating control

Hello everybody,
I my asp.net application, when I select a CrystalReportViewer control and put it on my form, it says "Error creating control" and hence I am unable to use it.
Any ideas ??
Thankslooks like ur .NET is corrupted. Try with another language (VB.NET). If it the same u gotta reinstall .NET.

Friday, March 9, 2012

Error Control

hi,

I'm trying to insert files from one table to anotherone. The problem is that the source table doesnt have any primary key, and it has duplicated PK that the destination needs to be unique. It's possible to ignore this kind of errors? I am using an OLEDB Destination transformation, and trying to omit this error configuring the error output, but it doesnt work.

Thanks!

There are several ways to deal with duplicates. But first you have to tell how would you like to handle them.

You can use a sort transformation with the remove duplicates checked; the thing is that you don't have control over which row get passed and which gets discarded. If the rows are identical; then this approach is easy enough.

Notice that Sort transformation will cache all incoming rows before sending them to the out put, so memory consumptions and performance could suffer.

|||

Thnaks Rafael,

The problem is that i need to control these duplicated records. Is any other way to handle this situation? what i need is to omit this error , continue executing over the flow, and record it on the log.

|||

You could do an insert of the full data set and on the errors (rows with duplicates) use the error output to write them to a "duplicates" table.

|||

Albertoim wrote:

Thnaks Rafael,

The problem is that i need to control these duplicated records. Is any other way to handle this situation? what i need is to omit this error , continue executing over the flow, and record it on the log.

You still are not providing the details on how do you want to handle the duplicates. Do you want to send them all to the duplicate tables, or do you want to insert at least one and reject the rest?

E.g if you have set of 3 duplicate rows, do you want to insert 1 and log the other 2 as duplicates or you want to log the 3 of them and insert none?

For the first case, and when the RANK() function is available in the source DB, like Oracle or SS2005 I use this:

http://rafael-salas.blogspot.com/2007/04/remove-duplicates-using-t-sql-rank.html

|||

I only want to insert the first record, all other duplicated records must by registered in the log (the error warning) not in other table, but it could be an alternative...

thanks both of you for your answers.

Error connectiong to database

Hi,
I am designing a webpage with the DbNetGrid control on it. I am folloing
its documentation and using the following connection string to connect the
grid control to a database:
'Provider=SQLOLEDB;Driver={SQL
Server};Server=(local);Database=pubs;Tru
sted_Connection=yes;'
But DbNetGrid keeps giving me the following error. I have tried different
variations of this connection string but nothing seems to work.
[Microsoft][ODBC Driver Manager] Data source name not found and no default
driver specified (-2147467259)
Thanks.helpful sql wrote:
> Hi,
> I am designing a webpage with the DbNetGrid control on it. I am folloin
g
> its documentation and using the following connection string to connect the
> grid control to a database:
> 'Provider=SQLOLEDB;Driver={SQL
> Server};Server=(local);Database=pubs;Tru
sted_Connection=yes;'
> But DbNetGrid keeps giving me the following error. I have tried different
> variations of this connection string but nothing seems to work.
> [Microsoft][ODBC Driver Manager] Data source name not found and no default
> driver specified (-2147467259)
You have to create ODBC Data Source object as the message states.
Look for Data Sources icon in the Control Panel/Administrative Tools.
Then use its name in the connection string.|||Not neccessarily.
http://www.connectionstrings.com
And no need really to cross-post so much!
Jeff
"Sericinus hunter" <serhunt@.flash.net> wrote in message
news:a2P_f.23982$NS6.16336@.newssvr30.news.prodigy.com...
> helpful sql wrote:
> You have to create ODBC Data Source object as the message states.
> Look for Data Sources icon in the Control Panel/Administrative Tools.
> Then use its name in the connection string.|||Jeff Dillon wrote:
> Not neccessarily.
> http://www.connectionstrings.com
Thanks, I did not know that.

> And no need really to cross-post so much!
I hope your intention was to say that to the original poster.

> Jeff
> "Sericinus hunter" <serhunt@.flash.net> wrote in message
> news:a2P_f.23982$NS6.16336@.newssvr30.news.prodigy.com...
>|||No, I had already tried this website and all their examples for Sql Server
2000 connection strings. They did work from my test application but they did
not work with DbNetGrid. I had to create an ODBC Data Source in
Administrative Tools. Then it worked fine.
Thanks all for help.
"Sericinus hunter" <serhunt@.flash.net> wrote in message
news:ACR_f.3328$mu2.302@.newssvr24.news.prodigy.net...
> Jeff Dillon wrote:
> Thanks, I did not know that.
>
> I hope your intention was to say that to the original poster.
>

Friday, February 17, 2012

Error at parse query

Hello

I have the following:

One data flow task in the control flow
In this data flow task I have an OLE DB Source with the following sql command

'.... where cc = ?'

Then I click on the Parameters button and I map the ? to the right Parameter

Parameter0 = User::var_cc

When I click on parse query I get the following message :

"Parameter Information cannot be derived from SQL statements. Set parameter information before preparing command."

Anyone has an idea?
ThxThis is a known issue. Parse Query does not know how to use the parameters you mapped.

If you do not get any error after clicking OK, your query should be fine and you can safely ignore the error from Parse Query.