Click here to Skip to main content
15,117,830 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code in c# to insert data into table programmatically:

 using (SqlConnection conn = new SqlConnection(@"Data source = xxxx; Database=xxxxx; User Id=xxxx; Password=xxxx"))
{
 conn.Open();
 string sqlQuery = @"INSERT INTO UXFaturas(TransDocument, TransSerial, TransDocNumber, 
 Data, Estado) VALUES (@transdocument, @transserial, @transdocnumber, @data, @estado)";
 MessageBox.Show("step1");
 SqlCommand SQLcm = new SqlCommand();
 SQLcm.Connection = conn;
 SQLcm.CommandText = sqlQuery;
 SQLcm.CommandType = CommandType.Text;
 MessageBox.Show("step2");
 SQLcm.Parameters.AddWithValue("@transdocument", transdocument);
 SQLcm.Parameters.AddWithValue("@transsarial", transserial);
 SQLcm.Parameters.AddWithValue("@transdocnumber", transdocnumber);
 SQLcm.Parameters.AddWithValue("@data", data);
 SQLcm.Parameters.AddWithValue("@estado", estado);
 MessageBox.Show("step3");
 SQLcm.ExecuteNonQuery();
 MessageBox.Show("inseriu");
 conn.Close();
}


What I have tried:

I've added the three messagebox.show("");to know where the program stops working but it goes through all three steps and just skips the SQLcm.ExecuteNonQuery();.
I can provide the code to create the table and the variables if you need.
Posted
Updated 23-Aug-17 0:00am
v2
Comments
Nowfer Rifkan 23-Aug-17 5:07am
   
Simply you use this code to insert the data into database
one more thing , create you connection string in global so you can call it whenever you want to use it.... if u write the connection string for all the parts then it won't be a healthy programming. anyway use this code to insert data simply into database

try
{
using (SqlConnection conn = new SqlConnection("Put ur connection string here"))
{
conn.Open();
SqlCommand cmd = new SqlCommand("insert into TableName(Column Ids) values('" + values + "')", conn);
cmd.ExecuteNonQuery();
conn.Close();
messagebox.show("Your message here");


}


}
catch (Exception ex)
{
throw ex;
}
Member 13356973 23-Aug-17 5:09am
   
I'll try this, thanks
Nowfer Rifkan 23-Aug-17 5:13am
   
it will definitely work... if anything let us know.... good luck
Member 13356973 23-Aug-17 5:18am
   
I've tried this and got this error: System.InvalidOperationException: 'ExecuteNonQuery: Connection property has not been initialized.'
Patrice T 23-Aug-17 18:48pm
   
Use Improve question to update your question.
So that everyone can pay attention to this information.
Member 13356973 24-Aug-17 4:29am
   
My problem as been solved by Mohamed Rifkan

1 solution

Chances are that something to do with your request is failing and that is throwing an exception that is being caught elsewhere.
Change the line to this:
C#
rowsAffected = SQLcm.ExecuteNonQuery();

And add this line at the top of the method:
C#
int rowsAffected = -1;

Now encase that code in a try...catch block and add breakpoints on the ExecuteNonQuery line, and inside the catch block.
Run your code again inside the debugger (you can remove the MessageBoxes for this) and watch what happens. When you run the ExecuteNonQuery statement, one of two things will happen. The first is that control will move to the catch block, and you can look at the exception detail to see why. The other is that rowsAffected will get set to the number of rows inserted. If that's 1, then fine. If not ... well, the catch block should have caught it!

Try that, and see what you find.
   
Comments
Member 13356973 23-Aug-17 5:00am
   
I'll try this and soon I'll say something about it. Thanks
Member 13356973 23-Aug-17 5:08am
   
the rowsAffected still -1 and it didnt insert anything...
OriginalGriff 23-Aug-17 5:16am
   
Was that after you executed the line, or when the debugger stopped on it?
Member 13356973 23-Aug-17 5:21am
   
when the debugger stopped it
OriginalGriff 23-Aug-17 5:45am
   
When the debugger stops at a breakpoint, it doesn't execute the line. This is to give you a chance to look at variables and so forth and work out what should happen before the line is executed.
You can then use the single step toolbutton (which I can't describe as it changes from version to version of VS - but if you open the Debug menu you'll get text and a picture of the button) to execute your code line-by-line and follow exactly what is going on.

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