Doe Jowns Doe Jowns - 1 year ago 68
ASP.NET (C#) Question

Why does not this SQL server INSERT query execute?

I work on an ASP.NET project (using serenity.is) and have now a problem with a database query :

[HttpGet]
[Route("SimonTest/{id=0}/{obj=0}")]
public ActionResult SimonTest(int id, int obj)
{
SqlConnection myConn = new SqlConnection(@"Server=(LocalDb)\MSSqlLocalDB;Integrated security=SSPI;database=Serene5_Default_v1");

// skipped code building the following command :

command = "INSERT INTO [Serene5_Default_v1].[tcpdump].[Errors] (TimeStp,IdSource,IdDestination,PortSource,PortDestination,ToTheRight,ToTheLeft) VALUES ('11:2','11','1','1','1',1,1);";


System.Diagnostics.Debug.WriteLine(command);
SqlCommand myCommand2 = new SqlCommand(listDb, myConn);


myCommand2.ExecuteNonQuery();

System.Diagnostics.Debug.WriteLine("Commande exécutée");
myCommand2.Dispose();


myConn.Close();
return View("~/Modules/Default/TcpDump/TcpDumpIndex.cshtml");
}


When I execute the query
INSERT INTO ...
with Microsoft SQL Server Management Studio, nothing goes wrong, but here the command seems to be skipped (no exception raised and nothing written in database)

Answer Source

You forgot to assign sql text to command myCommand2.CommandText = command before executing query.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download