GmodCake
GmodCake

Reputation: 437

SQL insert resulting in null fields

I'm trying to input data from textboxes into a SQL database, and when I do, it is only inserting null fields.

here my code :

 sqlCommand = new SqlCommand("INSERT INTO Department (Description, Comments, Permissions, Active, Production) VALUES (@description, @comments, @permissions, @active, @production)", sqlConnection);

 sqlCommand.Parameters.Add("@description", SqlDbType.NVarChar, 50);
 sqlCommand.Parameters.Add("@comments", SqlDbType.NVarChar, 50);
 sqlCommand.Parameters.Add("@permissions", SqlDbType.NVarChar, 50);
 sqlCommand.Parameters.Add("@active", SqlDbType.Int);
 sqlCommand.Parameters.Add("@production", SqlDbType.Int);

 sqlCommand.Parameters.AddWithValue("@description", txtDescription.Text);
 sqlCommand.Parameters.AddWithValue("@comments", txtComments.Text);
 sqlCommand.Parameters.AddWithValue("@permissions", txtPermissions.Text);
 sqlCommand.Parameters.AddWithValue("@active", Convert.ToString(Convert.ToUInt32(chkActif.Checked)));
 sqlCommand.Parameters.AddWithValue("@production", Convert.ToString(Convert.ToUInt32(chkProduction.Checked)));
 sqlCommand.ExecuteNonQuery();

Upvotes: 2

Views: 179

Answers (2)

rach
rach

Reputation: 679

Try passing some static values to your query.

Second thing try converting your textbox.Text toString(). Make sure you have runat="server" in the textbox tag and check you are not setting value to "" anywhere in your code.

Upvotes: 2

scartag
scartag

Reputation: 17680

You are adding the Parameters twice! Change your code to.

sqlCommand = new SqlCommand("INSERT INTO Department (Description, Comments, Permissions, Active, Production) VALUES (@description, @comments, @permissions, @active, @production)", sqlConnection);

                sqlCommand.Parameters.AddWithValue("@description", txtDescription.Text);
                sqlCommand.Parameters.AddWithValue("@comments", txtComments.Text);
                sqlCommand.Parameters.AddWithValue("@permissions", txtPermissions.Text);
                sqlCommand.Parameters.AddWithValue("@active", Convert.ToString(Convert.ToUInt32(chkActif.Checked)));
                sqlCommand.Parameters.AddWithValue("@production", Convert.ToString(Convert.ToUInt32(chkProduction.Checked)));
                sqlCommand.ExecuteNonQuery();

Upvotes: 7

Related Questions