Insert with Error when Including Box

José Couto

New member
Joined
Nov 14, 2024
Messages
2
Programming Experience
Beginner
SQL Server Management Studio 20 Database
Windows AuthenticationI'm using Visual Studio 2022

it seems that it doesn't recognize the commands, can someone help me?

C#:
string strCon = "Data Source=DESKTOP-FA03T93;Initial Catalog=Controle_Manejo_ASF; Trusted_Connection=True; Encrypt=False";
try { using (SqlConnection cn = new SqlConnection(strCon)) { cn.Open();
 string strSQL = @"INSERT INTO [dbo].[Caixa] ([ID_Caixa])“; VALUES (@cx1)";
 SqlCommand cm = new SqlCommand(strSQL, cn);
 cm.Parameters.Add(new SqlParameter("cx1", ID_Caixa.Text));
 cm.ExecuteNonQuery();
 cn.Close();
 MessageBox.Show("Inclusion successful", “Box”, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
 }
}
catch (Exception ex)
{
MessageBox.Show("Error Adding Box", “Box”,
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
 
Last edited by a moderator:
If you look at the ex.Message what does it say? That might give help you frame your question better than "it doesn't recognize the commands".

But right now, the left open double quote and semicolon in your SQL command text looks very suspicious:
C#:
INSERT INTO [dbo].[Caixa] ([ID_Caixa])“; VALUES (@cx1)
                                      ^^
                                      |What is this semicolon for?
                                      What is this left double quote for?
 
When you work with writing SQL, consider first writing the SQL in either SSMS. SSMS can start you off as explained here. All you need to do is add DECLARE statements. Once done copy the SQL statement to code minus and DECLARE statements. Creating statements this way if there are errors you fix them and then if there is an issue it will not be how the statement was created.
 
Back
Top Bottom