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

Sunday, March 11, 2012

Error converting data type varchar to int?

So I have this asp 3.0 page. The page is taking form-submitted data and
saving it to a SQL Server 2000 database via stored procedure.
Just one problem - every time I execute the code, I get the following error:
Microsoft OLE DB Provider for ODBC Drivers error '80040e07'
[Microsoft][ODBC SQL Server Driver][SQL Server]Error converting data type
varchar to int.
/erf2005/forums/abstractcontactinfo1.asp, line 36
Now, this error is a new thing. This page worked fine until I added the
RegID parameter just a little while ago. Somehow that new parameter is the
cause of the error, but after 19 hours on the job, I'll be damned if I can
figure out why. I need fresher eyes. I also need to stop working so damned
much, but that is an issue for another day.
Below is the code form the asp page. Note that the 'line 36' referred to in
the error message is the oCmd.Execute line:
dim oCmd, oCmd4, oCmd2, oCmd5, oCmd6, oCmd7, oCmd8, oCmd9, oCmd10, oCmd11,
oCmd12, oCmd13, oCmd14
dim vRegID
vRegID = Request.Form("RegID")
vRegID = CInt(vRegID)
Set oCmd = GetStoredProcedure(getConnection(),"sp_addAbstract")
oCmd.Parameters.append oCmd.CreateParameter("RegID", adInteger,
adParamInput,4,vRegID)
oCmd.Parameters.append oCmd.CreateParameter("AbstractTitle", adVarChar,
adParamInput,160,(Request.Form("AbstractTitle")))
oCmd.Parameters.append oCmd.CreateParameter("AbstractText", adLongVarChar,
adParamInput,32000,(Request.Form("AbstractText")))
oCmd.Parameters.append oCmd.CreateParameter("WebAddress", adVarChar,
adParamInput,255,(Request.Form("WebAddress")))
oCmd.Parameters.append oCmd.CreateParameter("AbstractType", adVarChar,
adParamInput,15,(Request.Form("AbstractType")))
oCmd.Parameters.append oCmd.CreateParameter("PresentChoice", adVarChar,
adParamInput,15,(Request.Form("PresentChoice")))
oCmd.Parameters.append oCmd.CreateParameter("SessionChoice1", adVarChar,
adParamInput,7,(Request.Form("SessionChoice1")))
oCmd.Parameters.append oCmd.CreateParameter("SessionChoice2", adVarChar,
adParamInput,7,(Request.Form("SessionChoice2")))
oCmd.Parameters.append oCmd.CreateParameter("Keyword1", adVarChar,
adParamInput,50,(Request.Form("Keyword1")))
oCmd.Parameters.append oCmd.CreateParameter("Keyword2", adVarChar,
adParamInput,50,(Request.Form("Keyword2")))
oCmd.Parameters.append oCmd.CreateParameter("Keyword3", adVarChar,
adParamInput,50,(Request.Form("Keyword3")))
oCmd.Parameters.append oCmd.CreateParameter("Keyword4", adVarChar,
adParamInput,50,(Request.Form("Keyword4")))
oCmd.Parameters.append oCmd.CreateParameter("StudentType", adVarChar,
adParamInput,15,(Request.Form("StudentType")))
oCmd.Parameters.append oCmd.CreateParameter("Judge", adVarChar,
adParamInput,3,(Request.Form("Judge")))
oCmd.Parameters.append oCmd.CreateParameter("Author1FirstInitial",
adVarChar, adParamInput,1,(Request.Form("Author1FirstInitial")))
oCmd.Parameters.append oCmd.CreateParameter("Author1MI", adVarChar,
adParamInput,1,(Request.Form("Author1MI")))
oCmd.Parameters.append oCmd.CreateParameter("Author1LastName", adVarChar,
adParamInput,50,(Request.Form("Author1LastName")))
oCmd.Parameters.append oCmd.CreateParameter("Author1Organization",
adVarChar, adParamInput,100,(Request.Form("Author1Organization")))
oCmd.Parameters.append oCmd.CreateParameter("Author1City", adVarChar,
adParamInput,50,(Request.Form("Author1City")))
oCmd.Parameters.append oCmd.CreateParameter("Author1State", adVarChar,
adParamInput,25,(Request.Form("Author1State")))
oCmd.Parameters.append oCmd.CreateParameter("Author1Country", adVarChar,
adParamInput,35,(Request.Form("Author1Country")))
oCmd.Parameters.append oCmd.CreateParameter("Author1Email", adVarChar,
adParamInput,75,(Request.Form("Author1Email")))
oCmd.Parameters.append oCmd.CreateParameter("Author1Presents", adVarChar,
adParamInput,3,(Request.Form("Author1Presents")))
oCmd.Parameters.append oCmd.CreateParameter("AbstractID", adInteger,
adParamOutput,4)
oCmd.execute()
And here's the stored procedure:
CREATE Procedure sp_addAbstract
/*
(
@.parameter1 datatype = default value,
@.parameter2 datatype OUTPUT
)
*/
@.AbstractTitle VARCHAR(255) = NULL,
@.AbstractText TEXT,
@.WebAddress VARCHAR(255) = NULL,
@.AbstractType VARCHAR(15) = NULL,
@.PresentChoice VARCHAR(15) = NULL,
@.SessionChoice1 VARCHAR(7) = NULL,
@.SessionChoice2 VARCHAR(7) = NULL,
@.Keyword1 VARCHAR(50) = NULL,
@.Keyword2 VARCHAR(50) = NULL,
@.Keyword3 VARCHAR(50) = NULL,
@.Keyword4 VARCHAR(50) = NULL,
@.StudentType VARCHAR(15) = NULL,
@.Judge VARCHAR(3) = NULL,
@.Author1FirstInitial VARCHAR(1) = NULL,
@.Author1MI VARCHAR(1) = NULL,
@.Author1LastName VARCHAR(50) = NULL,
@.Author1Organization VARCHAR(100) = NULL,
@.Author1City VARCHAR(50) = NULL,
@.Author1State VARCHAR(25) = NULL,
@.Author1Country VARCHAR(35) = NULL,
@.Author1Email VARCHAR(75) = NULL,
@.Author1Presents VARCHAR(3) = NULL,
@.RegID INT,
@.AbstractID int output
as
insert dbo.tblAbstractInfo
(
AbstractTitle,
AbstractText,
WebAddress,
AbstractType,
PresentChoice,
SessionChoice1,
SessionChoice2,
Keyword1,
Keyword2,
Keyword3,
Keyword4,
Author1FirstInitial,
Author1MI,
Author1LastName,
Author1Organization,
Author1City,
Author1State,
Author1Country,
Author1Email,
Author1Presents,
StudentType,
Judge,
RegID
)
values
(
@.AbstractTitle,
@.AbstractText,
@.WebAddress,
@.AbstractType,
@.PresentChoice,
@.SessionChoice1,
@.SessionChoice2,
@.Keyword1,
@.Keyword2,
@.Keyword3,
@.Keyword4,
@.Author1FirstInitial,
@.Author1MI,
@.Author1LastName,
@.Author1Organization,
@.Author1City,
@.Author1State,
@.Author1Country,
@.Author1Email,
@.Author1Presents,
@.StudentType,
@.Judge,
@.RegID
)
set @.AbstractID = @.@.IDENTITY
if(@.@.error <> 0)
return 1
return(0)
GO
Before anyone asks, the underlying table does have a RegID field, and it is
indeed of datatype INT.
So what am I missing?I'm not an ASP or ADO person, but perhaps ASP/ADO passes the parameters by position and not by name?
You would be able to see this using a Profiler trace. Or do a quick test "positioning" the regid
ASP/ADO parameter in the correct place according to the procedure definition for the parameter.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"Norrick" <Norrick@.discussions.microsoft.com> wrote in message
news:C9A88262-AF9A-4270-A21A-C26427EB3529@.microsoft.com...
> So I have this asp 3.0 page. The page is taking form-submitted data and
> saving it to a SQL Server 2000 database via stored procedure.
> Just one problem - every time I execute the code, I get the following error:
>
> Microsoft OLE DB Provider for ODBC Drivers error '80040e07'
> [Microsoft][ODBC SQL Server Driver][SQL Server]Error converting data type
> varchar to int.
> /erf2005/forums/abstractcontactinfo1.asp, line 36
>
> Now, this error is a new thing. This page worked fine until I added the
> RegID parameter just a little while ago. Somehow that new parameter is the
> cause of the error, but after 19 hours on the job, I'll be damned if I can
> figure out why. I need fresher eyes. I also need to stop working so damned
> much, but that is an issue for another day.
> Below is the code form the asp page. Note that the 'line 36' referred to in
> the error message is the oCmd.Execute line:
>
> dim oCmd, oCmd4, oCmd2, oCmd5, oCmd6, oCmd7, oCmd8, oCmd9, oCmd10, oCmd11,
> oCmd12, oCmd13, oCmd14
> dim vRegID
> vRegID = Request.Form("RegID")
> vRegID = CInt(vRegID)
> Set oCmd = GetStoredProcedure(getConnection(),"sp_addAbstract")
> oCmd.Parameters.append oCmd.CreateParameter("RegID", adInteger,
> adParamInput,4,vRegID)
> oCmd.Parameters.append oCmd.CreateParameter("AbstractTitle", adVarChar,
> adParamInput,160,(Request.Form("AbstractTitle")))
> oCmd.Parameters.append oCmd.CreateParameter("AbstractText", adLongVarChar,
> adParamInput,32000,(Request.Form("AbstractText")))
> oCmd.Parameters.append oCmd.CreateParameter("WebAddress", adVarChar,
> adParamInput,255,(Request.Form("WebAddress")))
> oCmd.Parameters.append oCmd.CreateParameter("AbstractType", adVarChar,
> adParamInput,15,(Request.Form("AbstractType")))
> oCmd.Parameters.append oCmd.CreateParameter("PresentChoice", adVarChar,
> adParamInput,15,(Request.Form("PresentChoice")))
> oCmd.Parameters.append oCmd.CreateParameter("SessionChoice1", adVarChar,
> adParamInput,7,(Request.Form("SessionChoice1")))
> oCmd.Parameters.append oCmd.CreateParameter("SessionChoice2", adVarChar,
> adParamInput,7,(Request.Form("SessionChoice2")))
> oCmd.Parameters.append oCmd.CreateParameter("Keyword1", adVarChar,
> adParamInput,50,(Request.Form("Keyword1")))
> oCmd.Parameters.append oCmd.CreateParameter("Keyword2", adVarChar,
> adParamInput,50,(Request.Form("Keyword2")))
> oCmd.Parameters.append oCmd.CreateParameter("Keyword3", adVarChar,
> adParamInput,50,(Request.Form("Keyword3")))
> oCmd.Parameters.append oCmd.CreateParameter("Keyword4", adVarChar,
> adParamInput,50,(Request.Form("Keyword4")))
> oCmd.Parameters.append oCmd.CreateParameter("StudentType", adVarChar,
> adParamInput,15,(Request.Form("StudentType")))
> oCmd.Parameters.append oCmd.CreateParameter("Judge", adVarChar,
> adParamInput,3,(Request.Form("Judge")))
>
> oCmd.Parameters.append oCmd.CreateParameter("Author1FirstInitial",
> adVarChar, adParamInput,1,(Request.Form("Author1FirstInitial")))
> oCmd.Parameters.append oCmd.CreateParameter("Author1MI", adVarChar,
> adParamInput,1,(Request.Form("Author1MI")))
> oCmd.Parameters.append oCmd.CreateParameter("Author1LastName", adVarChar,
> adParamInput,50,(Request.Form("Author1LastName")))
> oCmd.Parameters.append oCmd.CreateParameter("Author1Organization",
> adVarChar, adParamInput,100,(Request.Form("Author1Organization")))
> oCmd.Parameters.append oCmd.CreateParameter("Author1City", adVarChar,
> adParamInput,50,(Request.Form("Author1City")))
> oCmd.Parameters.append oCmd.CreateParameter("Author1State", adVarChar,
> adParamInput,25,(Request.Form("Author1State")))
> oCmd.Parameters.append oCmd.CreateParameter("Author1Country", adVarChar,
> adParamInput,35,(Request.Form("Author1Country")))
> oCmd.Parameters.append oCmd.CreateParameter("Author1Email", adVarChar,
> adParamInput,75,(Request.Form("Author1Email")))
> oCmd.Parameters.append oCmd.CreateParameter("Author1Presents", adVarChar,
> adParamInput,3,(Request.Form("Author1Presents")))
> oCmd.Parameters.append oCmd.CreateParameter("AbstractID", adInteger,
> adParamOutput,4)
> oCmd.execute()
>
> And here's the stored procedure:
>
> CREATE Procedure sp_addAbstract
> /*
> (
> @.parameter1 datatype = default value,
> @.parameter2 datatype OUTPUT
> )
> */
> @.AbstractTitle VARCHAR(255) = NULL,
> @.AbstractText TEXT,
> @.WebAddress VARCHAR(255) = NULL,
> @.AbstractType VARCHAR(15) = NULL,
> @.PresentChoice VARCHAR(15) = NULL,
> @.SessionChoice1 VARCHAR(7) = NULL,
> @.SessionChoice2 VARCHAR(7) = NULL,
> @.Keyword1 VARCHAR(50) = NULL,
> @.Keyword2 VARCHAR(50) = NULL,
> @.Keyword3 VARCHAR(50) = NULL,
> @.Keyword4 VARCHAR(50) = NULL,
> @.StudentType VARCHAR(15) = NULL,
> @.Judge VARCHAR(3) = NULL,
> @.Author1FirstInitial VARCHAR(1) = NULL,
> @.Author1MI VARCHAR(1) = NULL,
> @.Author1LastName VARCHAR(50) = NULL,
> @.Author1Organization VARCHAR(100) = NULL,
> @.Author1City VARCHAR(50) = NULL,
> @.Author1State VARCHAR(25) = NULL,
> @.Author1Country VARCHAR(35) = NULL,
> @.Author1Email VARCHAR(75) = NULL,
> @.Author1Presents VARCHAR(3) = NULL,
> @.RegID INT,
> @.AbstractID int output
> as
> insert dbo.tblAbstractInfo
> (
> AbstractTitle,
> AbstractText,
> WebAddress,
> AbstractType,
> PresentChoice,
> SessionChoice1,
> SessionChoice2,
> Keyword1,
> Keyword2,
> Keyword3,
> Keyword4,
> Author1FirstInitial,
> Author1MI,
> Author1LastName,
> Author1Organization,
> Author1City,
> Author1State,
> Author1Country,
> Author1Email,
> Author1Presents,
> StudentType,
> Judge,
> RegID
> )
> values
> (
> @.AbstractTitle,
> @.AbstractText,
> @.WebAddress,
> @.AbstractType,
> @.PresentChoice,
> @.SessionChoice1,
> @.SessionChoice2,
> @.Keyword1,
> @.Keyword2,
> @.Keyword3,
> @.Keyword4,
> @.Author1FirstInitial,
> @.Author1MI,
> @.Author1LastName,
> @.Author1Organization,
> @.Author1City,
> @.Author1State,
> @.Author1Country,
> @.Author1Email,
> @.Author1Presents,
> @.StudentType,
> @.Judge,
> @.RegID
> )
> set @.AbstractID = @.@.IDENTITY
>
> if(@.@.error <> 0)
> return 1
> return(0)
> GO
>
> Before anyone asks, the underlying table does have a RegID field, and it is
> indeed of datatype INT.
> So what am I missing?|||You've misspecified the parameter names. In the procedure, you've indicated
names like @.RegID, @.AbstractTitle, etc.; however, in your ASP code when you
are creating and appendending to your parameters collections, you are
spcifying names like "RegID", "AbstractTitle," etc. These are mismatched;
so, ODBC has to convert to using positional parameters instead of named
parameters.
When you added the RegID to the collection, you did not add it in the same
order as the parameters are listed in the T-SQL stored procedure. This
causes SQL Server to attempt to store the passed "Author1Presents"
VARCHAR(3) value into the @.RegID INT value, which is incompatible.
Also, you are using ASP and ADO. Why are you using ODBC instead of OLEDB
directly? There are several drivers available, check that you are calling
the correct provider: SQLOLEDB or MSDASQL? SQLOLEDB would be the better
choice. If it is because you are using a DSN type configuration
persistance, consider switching to UDL, universal data links, instead, and
use the SQLOLEDB driver directly. Why have the extra API layer to slow you
down?
Sincerely,
Anthony Thomas
"Norrick" <Norrick@.discussions.microsoft.com> wrote in message
news:C9A88262-AF9A-4270-A21A-C26427EB3529@.microsoft.com...
So I have this asp 3.0 page. The page is taking form-submitted data and
saving it to a SQL Server 2000 database via stored procedure.
Just one problem - every time I execute the code, I get the following error:
Microsoft OLE DB Provider for ODBC Drivers error '80040e07'
[Microsoft][ODBC SQL Server Driver][SQL Server]Error converting data type
varchar to int.
/erf2005/forums/abstractcontactinfo1.asp, line 36
Now, this error is a new thing. This page worked fine until I added the
RegID parameter just a little while ago. Somehow that new parameter is the
cause of the error, but after 19 hours on the job, I'll be damned if I can
figure out why. I need fresher eyes. I also need to stop working so damned
much, but that is an issue for another day.
Below is the code form the asp page. Note that the 'line 36' referred to in
the error message is the oCmd.Execute line:
dim oCmd, oCmd4, oCmd2, oCmd5, oCmd6, oCmd7, oCmd8, oCmd9, oCmd10, oCmd11,
oCmd12, oCmd13, oCmd14
dim vRegID
vRegID = Request.Form("RegID")
vRegID = CInt(vRegID)
Set oCmd = GetStoredProcedure(getConnection(),"sp_addAbstract")
oCmd.Parameters.append oCmd.CreateParameter("RegID", adInteger,
adParamInput,4,vRegID)
oCmd.Parameters.append oCmd.CreateParameter("AbstractTitle", adVarChar,
adParamInput,160,(Request.Form("AbstractTitle")))
oCmd.Parameters.append oCmd.CreateParameter("AbstractText", adLongVarChar,
adParamInput,32000,(Request.Form("AbstractText")))
oCmd.Parameters.append oCmd.CreateParameter("WebAddress", adVarChar,
adParamInput,255,(Request.Form("WebAddress")))
oCmd.Parameters.append oCmd.CreateParameter("AbstractType", adVarChar,
adParamInput,15,(Request.Form("AbstractType")))
oCmd.Parameters.append oCmd.CreateParameter("PresentChoice", adVarChar,
adParamInput,15,(Request.Form("PresentChoice")))
oCmd.Parameters.append oCmd.CreateParameter("SessionChoice1", adVarChar,
adParamInput,7,(Request.Form("SessionChoice1")))
oCmd.Parameters.append oCmd.CreateParameter("SessionChoice2", adVarChar,
adParamInput,7,(Request.Form("SessionChoice2")))
oCmd.Parameters.append oCmd.CreateParameter("Keyword1", adVarChar,
adParamInput,50,(Request.Form("Keyword1")))
oCmd.Parameters.append oCmd.CreateParameter("Keyword2", adVarChar,
adParamInput,50,(Request.Form("Keyword2")))
oCmd.Parameters.append oCmd.CreateParameter("Keyword3", adVarChar,
adParamInput,50,(Request.Form("Keyword3")))
oCmd.Parameters.append oCmd.CreateParameter("Keyword4", adVarChar,
adParamInput,50,(Request.Form("Keyword4")))
oCmd.Parameters.append oCmd.CreateParameter("StudentType", adVarChar,
adParamInput,15,(Request.Form("StudentType")))
oCmd.Parameters.append oCmd.CreateParameter("Judge", adVarChar,
adParamInput,3,(Request.Form("Judge")))
oCmd.Parameters.append oCmd.CreateParameter("Author1FirstInitial",
adVarChar, adParamInput,1,(Request.Form("Author1FirstInitial")))
oCmd.Parameters.append oCmd.CreateParameter("Author1MI", adVarChar,
adParamInput,1,(Request.Form("Author1MI")))
oCmd.Parameters.append oCmd.CreateParameter("Author1LastName", adVarChar,
adParamInput,50,(Request.Form("Author1LastName")))
oCmd.Parameters.append oCmd.CreateParameter("Author1Organization",
adVarChar, adParamInput,100,(Request.Form("Author1Organization")))
oCmd.Parameters.append oCmd.CreateParameter("Author1City", adVarChar,
adParamInput,50,(Request.Form("Author1City")))
oCmd.Parameters.append oCmd.CreateParameter("Author1State", adVarChar,
adParamInput,25,(Request.Form("Author1State")))
oCmd.Parameters.append oCmd.CreateParameter("Author1Country", adVarChar,
adParamInput,35,(Request.Form("Author1Country")))
oCmd.Parameters.append oCmd.CreateParameter("Author1Email", adVarChar,
adParamInput,75,(Request.Form("Author1Email")))
oCmd.Parameters.append oCmd.CreateParameter("Author1Presents", adVarChar,
adParamInput,3,(Request.Form("Author1Presents")))
oCmd.Parameters.append oCmd.CreateParameter("AbstractID", adInteger,
adParamOutput,4)
oCmd.execute()
And here's the stored procedure:
CREATE Procedure sp_addAbstract
/*
(
@.parameter1 datatype = default value,
@.parameter2 datatype OUTPUT
)
*/
@.AbstractTitle VARCHAR(255) = NULL,
@.AbstractText TEXT,
@.WebAddress VARCHAR(255) = NULL,
@.AbstractType VARCHAR(15) = NULL,
@.PresentChoice VARCHAR(15) = NULL,
@.SessionChoice1 VARCHAR(7) = NULL,
@.SessionChoice2 VARCHAR(7) = NULL,
@.Keyword1 VARCHAR(50) = NULL,
@.Keyword2 VARCHAR(50) = NULL,
@.Keyword3 VARCHAR(50) = NULL,
@.Keyword4 VARCHAR(50) = NULL,
@.StudentType VARCHAR(15) = NULL,
@.Judge VARCHAR(3) = NULL,
@.Author1FirstInitial VARCHAR(1) = NULL,
@.Author1MI VARCHAR(1) = NULL,
@.Author1LastName VARCHAR(50) = NULL,
@.Author1Organization VARCHAR(100) = NULL,
@.Author1City VARCHAR(50) = NULL,
@.Author1State VARCHAR(25) = NULL,
@.Author1Country VARCHAR(35) = NULL,
@.Author1Email VARCHAR(75) = NULL,
@.Author1Presents VARCHAR(3) = NULL,
@.RegID INT,
@.AbstractID int output
as
insert dbo.tblAbstractInfo
(
AbstractTitle,
AbstractText,
WebAddress,
AbstractType,
PresentChoice,
SessionChoice1,
SessionChoice2,
Keyword1,
Keyword2,
Keyword3,
Keyword4,
Author1FirstInitial,
Author1MI,
Author1LastName,
Author1Organization,
Author1City,
Author1State,
Author1Country,
Author1Email,
Author1Presents,
StudentType,
Judge,
RegID
)
values
(
@.AbstractTitle,
@.AbstractText,
@.WebAddress,
@.AbstractType,
@.PresentChoice,
@.SessionChoice1,
@.SessionChoice2,
@.Keyword1,
@.Keyword2,
@.Keyword3,
@.Keyword4,
@.Author1FirstInitial,
@.Author1MI,
@.Author1LastName,
@.Author1Organization,
@.Author1City,
@.Author1State,
@.Author1Country,
@.Author1Email,
@.Author1Presents,
@.StudentType,
@.Judge,
@.RegID
)
set @.AbstractID = @.@.IDENTITY
if(@.@.error <> 0)
return 1
return(0)
GO
Before anyone asks, the underlying table does have a RegID field, and it is
indeed of datatype INT.
So what am I missing?

Friday, February 24, 2012

error changing subscription properties

Hi

Made a change to the publisher security parameters, tried to save and got the following message:

"An error occurred saving the changes to the subscription.

Additional information.

"Value" is longer than 128 that it should not exceed.
Parameter name: value (Microsoft.sqlserver.rmo)

Doesn't make much sense to me.
Tried to change it back and all I get is this error (i had set the userid to domain/userid which was obviously an error).Hi, Bruce

Would you like provide more repro steps? And which version of SQL server are you using?

BTW, did you really input authentication login/password which is londer than 128 characters or any change will cause this error?

Also, when you tried to change back, were you stucked with this error or you succeeded in changing back.

Thanks
Yunjing|||

SQL 2005 standard - current release version

No it wasn't longer than 128

I'm stuck with it - I can't change it back....

Steps:
a) Well I had a 2005 subscription which was working fine (anonymous merge over HTTPS)
b) I made the mistake of changing the subscription properties-> publisher connection -> Use sql server authentication-> Login to domain\userid
c) pressed ok + ok - then got that error

Thanks

Bruce

|||

Hi, Bruce

Thanks for the info you provided.

I tried with 2005 anonymous merge pull sub with sync through web. But I couldn't reproduce the problem by following the steps you showed. I also read the product related source code and didn't find any code work different for changing login no matter for standard or enterprise SKU.

Did you change other sub properties value besides this login? Could you check if the issue was caused by other changes?

Thanks a lot

Yunjing

|||

Hi

It's a bit hard now to provide further info - I'm pretty sure it was the only thing I changed at the time....

thanks for your help anyway - this is a great service....
Bruce

|||

Thanks for all info you provided. If I can reproduce the issue in the future, I will let you know as well.

-Yunjing

error changing subscription properties

Hi

Made a change to the publisher security parameters, tried to save and got the following message:

"An error occurred saving the changes to the subscription.

Additional information.

"Value" is longer than 128 that it should not exceed.
Parameter name: value (Microsoft.sqlserver.rmo)

Doesn't make much sense to me.
Tried to change it back and all I get is this error (i had set the userid to domain/userid which was obviously an error).

Hi, Bruce

Would you like provide more repro steps? And which version of SQL server are you using?

BTW, did you really input authentication login/password which is londer than 128 characters or any change will cause this error?

Also, when you tried to change back, were you stucked with this error or you succeeded in changing back.

Thanks
Yunjing|||

SQL 2005 standard - current release version

No it wasn't longer than 128

I'm stuck with it - I can't change it back....

Steps:
a) Well I had a 2005 subscription which was working fine (anonymous merge over HTTPS)
b) I made the mistake of changing the subscription properties-> publisher connection -> Use sql server authentication-> Login to domain\userid
c) pressed ok + ok - then got that error

Thanks

Bruce

|||

Hi, Bruce

Thanks for the info you provided.

I tried with 2005 anonymous merge pull sub with sync through web. But I couldn't reproduce the problem by following the steps you showed. I also read the product related source code and didn't find any code work different for changing login no matter for standard or enterprise SKU.

Did you change other sub properties value besides this login? Could you check if the issue was caused by other changes?

Thanks a lot

Yunjing

|||

Hi

It's a bit hard now to provide further info - I'm pretty sure it was the only thing I changed at the time....

thanks for your help anyway - this is a great service....
Bruce

|||

Thanks for all info you provided. If I can reproduce the issue in the future, I will let you know as well.

-Yunjing