Click here to Skip to main content
15,886,055 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
SqlConnection con;
SqlCommand cmd;
con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlCon"].ToString());

try
{
   if (con.State == ConnectionState.Closed)
      con.Open();
}
catch { }

cmd= new SqlCommand("INSERT INTO [seismic_database_creation].[dbo].[tblFile] ([File_Name],[CreationDate]) VALUES('" + name + "'' " + temp + "')),con");

cmd.ExecuteNonQuery();
Posted
Updated 15-Apr-15 2:16am
v2

Try:
C#
SqlConnection con;
SqlCommand cmd;
con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlCon"].ToString());
 
try
{
   if (con.State == ConnectionState.Closed)
      con.Open();
}
catch { }

cmd = new SqlCommand("INSERT INTO [seismic_database_creation].[dbo].[tblFile] ([File_Name], [CreationDate]) VALUES(@name, @temp)", con);
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@temp", temp);
cmd.ExecuteNonQuery();


The values were not correctly specified (there was a comma missing) ; moreover, the best practice is to use parameterized queries to prevent any SQL injection attack. Plus, the ending double-quotes were not correctly positioned.
 
Share this answer
 
Comments
Sascha Lefèvre 15-Apr-15 8:30am    
+5
phil.o 15-Apr-15 8:34am    
Thanks Sascha
Um...
C#
cmd= new SqlCommand("INSERT INTO [seismic_database_creation].[dbo].[tblFile] ([File_Name],[CreationDate]) VALUES('" + name + "'' " + temp + "')),con");

Lets just rip some stuff out to make it more obvious:
C#
cmd= new SqlCommand("...'" + name + "'' " + temp + "')),con");

Your double quote is in the wrong place...
Try:
C#
cmd= new SqlCommand("INSERT INTO [seismic_database_creation].[dbo].[tblFile] ([File_Name],[CreationDate]) VALUES('" + name + "'' " + temp + "'))",con);


But please, don't swallow exceptions - at the very least log them, otherwise you don't know why subsequent code fails...
 
Share this answer
 
You have a typo there:
C#
cmd= new SqlCommand("INSERT INTO [seismic_database_creation].[dbo].[tblFile] ([File_Name],[CreationDate]) VALUES('" + name + "'' " + temp + "')", con);


Please read up on Sql-Parameters, you should use these instead of concatenating the values into your sql-strings. Sample here[^].
 
Share this answer
 
Comments
Member 11527624 15-Apr-15 9:41am    
Thanks all now its working :)
Member 11527624 15-Apr-15 9:42am    
I have dont this one with sql normal queries but how to do it in stored procedure?
Sascha Lefèvre 15-Apr-15 9:49am    
Here's a good sample:
http://www.aspsnippets.com/Articles/Calling-Insert-SQL-Server-Stored-Procedures-using-ADO.Net.aspx
Member 11527624 16-Apr-15 3:12am    
Thanks...Its useful link

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