Dear Friend,
Your code seems ok but its not that much perfect.
The Cause Of error might be becaue of key constraint(primary key violation or any other constraints).
Tip->Instead of letting the user to enter the id, I think it would be better to generate a key by the application and after saving it to the database the id value is shown to the user
Better progamming practice->
It is alwasys advised to use sqlparam instead of using as string directly
Eg
SqlConnection con=new SqlConnection("....");
con.Open();
SqlCommand cmd = new SqlCommand("Insert into std values(@id,@name)", con);
cmd.Parameters.Add("@id", SqlDbType.VarChar, 10);
cmd.Parameters["@id"].Value = "ID01";
cmd.Parameters.Add("@name", SqlDbType.VarChar, 10);
cmd.Parameters["@name"].Value = "Name";
cmd.ExecuteNonQuery()
con.Close();
Advantage of using sql parameters is that when inserting text containing ' it may cause error so it is always advised to use parameters
It would be better to use StringBuilder as it helps for creating dynamic strings and also delevers high performence compared to string.
ie
use a StringBuilder object and store the command in it then pass it to the command object's constructor as StingBuilderObject.toString()
Feel free to contact/replay
I hope your doubt is cleared now
Regards
Vipin Kumar Mallaya
Programmer