Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi

this is my code for insert to database .... but it will stop and show a run time error in connection.Open();and CmdSql.ExecuteNonQuery();

NEED ur help to move forward here on

C#
SqlConnection connection = new SqlConnection();
           connection.ConnectionString = "Data Source=ASA-PC\\SQLEXPRESS; Initial Catalog=Material_database; User ID=sa;Password=sa;Integrated Security=false;Connect Timeout=10";
           connection.Open();

           {
               SqlCommand CmdSql = new SqlCommand("INSERT INTO [mat_utiAndtrans](Project, Date, Team_Leader, Vendor, WO, Rate,Code,Qty,Officer,Remark) VALUES (@Project, @Date, @Team_Leader, @Vendor, @WO, @Rate,@Code,@Qty,@Officer,@Remark)", connection);
               connection.Open();
               CmdSql.Parameters.AddWithValue("@Project", txtproject.Text);
               CmdSql.Parameters.AddWithValue("@Date", txtdte.Text);
               CmdSql.Parameters.AddWithValue("@Team_Leader", txtTeamleader.Text);
               CmdSql.Parameters.AddWithValue("@Vendor", txtvendor.Text);
               CmdSql.Parameters.AddWithValue("@WO", txtwo.Text);
               CmdSql.Parameters.AddWithValue("@Rate", txtrate.Text);
               CmdSql.ExecuteNonQuery();
               connection.Close();
Posted
Updated 20-Jan-14 16:59pm
v2
Comments
Ron Beyer 20-Jan-14 22:59pm    
What are the errors it is giving you? I can tell you one of the problems is you didn't provide enough parameters for the insert...
Please post the exception details as well.
Sandeep Singh Shekhawat 20-Jan-14 23:07pm    
you should add these parameter also: @Rate,@Code,@Qty,@Officer,@Remark with CmdSql.Parameters.AddWithValue()

"in connection (); The connection was not closed. The connection's current state is open."
and
"run time error in connection.Open();and CmdSql.ExecuteNonQuery();"
That likely means that something totally different went wrong. In a single-threaded application without the Application.DoEvents() crap, it either crashes in the first position, or it crashes in the last position - but both? What about those lines of code being called twice from different threads? Or a duplicate entry due to an Application.DoEvents() which you did not show?
 
Share this answer
 
AS others have said, you need to add all the parameters. In future, give us the ACTUAL error message. Even consider reading it, and thinking about what it might mean....


If the Open call also shows an error, then your connection string is broken.

You should also use using blocks or at least call Dispose on your disposable objects.
 
Share this answer
 
v2
Comments
Asa code 20-Jan-14 23:23pm    
after adding the all parameters the problem is still remains why is it happened in insert only? because in same form it's working well for retrieving data to datagridview from same database
Christian Graus 20-Jan-14 23:29pm    
WHAT is happening ? What is the error message ?
Asa code 21-Jan-14 0:05am    
in connection ();
The connection was not closed. The connection's current state is open.
Christian Graus 21-Jan-14 0:09am    
That's odd. Perhaps setting a connection string causes it to open automatically ? I'd expect the connection string to be passed to the constructor. Either way, if it's open, you don't need to open it, it's not actually an issue - add code that checks if the State != Open and then open it only if you need to.

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