0
Chirag is right..
the autonumber field does not allow you to insert data...
because its automatically generated..
0
hi
You can not insert value in Identity (primary key value where you have set autoincrement true) in
table 'SERVICES'. So don't insert this ID value in this table. so it will work.
|
|
Don't forget to Mark Do you like this Answer that solved your problem! |
0
thank you!.. can I ask 1 more question? :)
may primary key service_code is a autonumber..
I get this error:
Error inserting
LabelCannot insert explicit value for identity column in table 'SERVICES' when IDENTITY_INSERT is set to OFF.
Why?
0
check the "name" of connection string i think thats y you r getting this erro:
Object reference not set to an instance of an object.
0
yes sir. I've already include that.. I use
String connectionstring = WebConfigurationManager.ConnectionStrings["HelpDesk_System"].ConnectionString;
then I use your suggested code.. but still no luck..
What do you mean by my connection string? is that the name of my database?
0
may be you are accessing wrong connection string Use the ConfigurationManager.ConnectionStrings
try this...
protected void Button1_Click(object sender, EventArgs e)
{
String insertSQL;
insertSQL = "INSERT INTO SERVICES (Service_Code, Service_Name)VALUES (@Service_Code, @Service_Name)";
String connectionstring = ConfigurationManager.ConnectionStrings["HelpDesk_System"].ConnectionString;
SqlConnection conn = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand(insertSQL, conn);
cmd.Parameters.AddWithValue("@Service_Code", TextBox1.Text);
cmd.Parameters.AddWithValue("@Service_Name", TextBox2.Text);
int added = 0;
try
{
conn.Open();
added = cmd.ExecuteNonQuery();
UpdatePanel1.Update();
conn.Close();
}
catch (Exception err)
{
Label1.Text = "Error inserting"; Label2.Text += err.Message;
}
}