hello sir,
i am using this code to insert values in the book_info table in sqlserver database. i am using visual studio 2008.
private void btn_save_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS; AttachDbFileName=|DataDirectory|\library.mdf; Integrated Security=true; User Instance=true;");
try
{
conn.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO book_info (b_code) VALUES ('"+ txt_new_book_code.Text.Trim() +"') ", conn);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
conn.Close();
}
finally
{
conn.Close();
}
}
}
when i click the save button first time (i think the entry gone in database) and when i click the save button again it gives primary key violation (b_code is primary key).
but when i stop my application and check the values in table nothing has been entered.
what is the problem.
please help me.
thanks in advance
Loading
Kirtan PatelPosted Dec 1, 2009, 6:51 AM
Your Application Actually Writes into the database but not the one you see in Visual Studio ..
it creates a temporary copy of your actual database in /bin/debug and Write the data into it so your actual copy will remain untouched ..when you are developing the application ..
again when you run the application Your Blank Actual database File copied to Bin\Debug and You Test Writing so ...
after that when you look at your actual database you will find nothing it it because application writes in temporary database .
Just Take out Exe and Database File Together and Place them alone and run them without Visual Studio It will work fine .
so no need to worry just go ahead in development of your application this is Normal as your application will work correctly when you provide it to end user :)
Amit ChoudharyPosted Dec 2, 2009, 1:37 AM
try this..
int result=cmd.ExecuteNonQuery();
then the value of result if it is 1 then data saved successfully and if it is 0 then something wrong with you Database connection..