Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have some textbox on my windows form. I want to insert those textbox value into a database table and show the updated table into a gridview when a button clicked. Here is my Insert code behind the button click event.
C#
// open connection
string connectionstring = ConfigurationManager.ConnectionStrings["myDBconn"].ToString();
SqlConnection objconnection = new SqlConnection(connectionstring);
objconnection.Open();               
// command for insert fire database              
string strInsertCommand= "INSERT INTO party ( party_name,  party_mobile, updated_at) VALUES( '" + txtAddPartyName.Text + "', '" + txtAddPartyMobile.Text + "', '" + DateTime.Now.ToShortDateString() + "')";
SqlCommand objCommand = new SqlCommand(strInsertCommand, objconnection);
objCommand.ExecuteNonQuery(); 
// close connection
objconnection.Close();

And here is my gridview update code.
C#
objconnection.Open();        
//fire the command object
objCommand = new SqlCommand("Select * from party", objconnection);
DataSet objDataset = new DataSet();
SqlDataAdapter objAdapter = new SqlDataAdapter(objCommand);
objAdapter.Fill(objDataset);
//bind the data from UI
dataGridView1.DataSource = objDataset.Tables[0];
//close the connection
objconnection.Close();

Problem is when I fill all textbox and hit the insert button then everything is work fine and datagridview also shows newly entered entry but database table does not updated with the new entry. There is no error message given at button click.
Thanks in advance, your help is greatly appreciated.
Sazzad
Posted
Comments
Anisuzzaman Sumon 17-Dec-14 0:01am    
Your code seems correct but as you are saying datagridview shows newly entered entry and you are filling the datasource from the database query .If the database table not updated how it shows please update your question and make sure you are searching in the same table is which you are inserting
sazzad37 17-Dec-14 0:10am    
I am also wonder that when I entered new value by insert button click they shows on the gridview but when I restart my application the newly entered rows become disappeared from gridview and only those row shows that I have entered by Show Table Data function(manually).
DamithSL 17-Dec-14 0:14am    
your database may be replaced by the visual studio in every run, check my answer for more information

Check the value that is returned in ExecuteNonQuery, it might be that there are some issues in insert statement and use try catch to track the exception
 
Share this answer
 
Comments
sazzad37 17-Dec-14 0:14am    
I have already use try catch but There are no exception message.
your database may be replace in every run you done by visual studio. right click on your database file in visual studio solution explorer and go to properties. there you need to set build action as content and copy to output directory as "copy if newer" option.
 
Share this answer
 
v3
Comments
sazzad37 17-Dec-14 0:29am    
"your database may be replace in every run" I think you are right but after changing build action It's still not working. :(
DamithSL 17-Dec-14 0:37am    
have you rebuild the application and run again? what is your connection string in app config?
sazzad37 17-Dec-14 0:41am    
Yes. This is my appconfig code.
add name="myDBconn" connectionstring="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\pepsi.mdf;Integrated Security=True" providername="System.Data.SqlClient"
DamithSL 17-Dec-14 0:52am    
rebuild your project and go to bin directory in your application folder and run your application exe file directly. are you facing same issue?
sazzad37 17-Dec-14 0:56am    
No, It's work fine. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900