programming Cant find error in my sql statement
Go to solution
Solved by DXMember,
18 minutes ago, Shammikit said:
Modify your code to have commands in this order:
Before you execute your command, add this line:
cmd.Connection = cn;
private void button1_Click(object sender, EventArgs e) { try { int n = 1; string name = "sk"; string pt = "test"; SqlConnection cn = new SqlConnection(@"Server = .\; Integrated Security = True"); cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@p", n); cmd.Parameters.AddWithValue("@nam", name); cmd.Parameters.AddWithValue("@pat", pt); cmd.CommandText = "INSERT INTO Finalproj.dbo.Image(ID, Name, path) VALUES (@p, @nam, @pat)"; cn.Open(); cmd.Connection = cn; cmd.ExecuteNonQuery(); cn.Close(); } catch (Exception ex) { string x = ex.ToString(); MessageBox.Show(x); } }
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now