Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've been trying all day, but despite there being no error, I can't insert anything at all to my table. I don't know what is honestly wrong with the code, since the data I'm sending is correct, unless there is something missing in my sintax.

It just executes the code but won't insert the new row. The other functions in the form that use for example SELECT work just fine.

C#
public static string constr = @"Data Source=.\SQLEXPRESS;
                        AttachDbFilename=|DataDirectory|\cybercafe.mdf;
                        Integrated Security=True;User Instance=True;"; //in another form

public SqlConnection myConnection = new SqlConnection(MainContainer.constr);//catches the connection from the other form

string sqlIns = "INSERT INTO [Programs] (CodProgram, directory, logo ,description ,class ,type) VALUES (@codprogram,@directory,@logo,@description,@class,@type)";
myConnection.Open();
try
{
SqlCommand cmdIns = new SqlCommand(sqlIns, myConnection);
cmdIns.Parameters.AddWithValue("@codprogram",idprogram);
cmdIns.Parameters.AddWithValue("@directory", "'shortcuts\\" +txtname.Text +".lnk'");
cmdIns.Parameters.AddWithValue("@logo", "'icons\\" + txtname.Text + ".jpg'");
cmdIns.Parameters.AddWithValue("@description", txtdescription.Text);
cmdIns.Parameters.AddWithValue("@class", class);
cmdIns.Parameters.AddWithValue("@type", type);
cmdIns.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString()); 
}
myConnection.Close();


Thanks in advance guys.
Posted
Comments
Irbaz Haider Hashmi 4-Feb-13 8:45am    
I am not sure right now as it looks ok to me but few things you can test here.
[Programs] replace with dbo.[Programs]
Run the query with the data and see if there is any length issue with the parameters you are passing and the parameter length in the datatable
Steven Borges 4-Feb-13 8:51am    
Got the same result, I also checked my table definition and compared it to the output I send to the table and it's indeed inside the boundaries of the size I specified.
Don't know why It doesn't work.
Fredrik Bornander 4-Feb-13 8:48am    
What does the cmdIns.ExecuteNonQuery call return?
Steven Borges 4-Feb-13 8:54am    
I don't know, how do I check that? I'm not very experienced with C# and Visual Studio.
I placed a breakpoint on it and it executes it, and also the rest of the entire code.
Fredrik Bornander 4-Feb-13 8:55am    
Change it to
int rows = cmdIns.ExecuteNonQuery();
and inspect the value of that.

1 solution

C#
cmdIns.Parameters.AddWithValue("@directory", "'shortcuts\\" +txtname.Text +".lnk'");


If I were you, I wouldn't include quotes (') in the beginning and end of the string (before shortcuts and after lnk).
 
Share this answer
 
Comments
Steven Borges 4-Feb-13 9:14am    
But wouldn't that generate an error in the query?
I'm sending a string to the field afterall.
Steven Borges 4-Feb-13 9:16am    
Tried and it actualy didn't generate any error, but it still gave me the same result.
phil.o 4-Feb-13 9:18am    
Did you take out quotes in the following line also (@logo) ?
Steven Borges 4-Feb-13 9:22am    
Yes, I removed it in both. No quotes in the entire code.
I was googling around and found this:
http://stackoverflow.com/questions/9382756/data-is-not-inserting-into-table

Could that be the same issue as me? If so I'm quite dissapointed since I don't realy want to create a connection in my machine, I want the .mdf file to be inside the application and to be accessible anytime I want by manipulating it's content.
phil.o 4-Feb-13 9:34am    
It may be the same issue. I can't really help further because I never use user instances MSSQL databases (always access to the SQL service itself on the dedicated port).

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