Jump to content

Is there anything wrong in my insert query?

Go to solution Solved by Postremus,

Looks to me like the value of pos is null.

Can you please check if any of your variables is null, please?

 

If any of them is null, you have to insert DBNull.Value instead, see https://msdn.microsoft.com/de-de/library/system.data.sqlclient.sqlparametercollection.addwithvalue(v=vs.110).aspx

I have been trying to figure out any mistake for a couple of hours now but i have failed to identify any. there's another insert query in my program written similarly to this and that works fine but this one doesnt work.here's the code

 

 public void new_employee(string company, string name, string description, string img, string pos, string key)
        {

            try
            {
                SqlCommand cmd = new SqlCommand();
 
                SqlConnection cn = new SqlConnection(@"Server  = .\;Integrated Security = True");
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@com", company);
                cmd.Parameters.AddWithValue("@nam", name);
                cmd.Parameters.AddWithValue("@des", description);
                cmd.Parameters.AddWithValue("@image", img);
                cmd.Parameters.AddWithValue("@post", pos);
                cmd.Parameters.AddWithValue("@ekey", key);
                cmd.Parameters.AddWithValue("@date", DateTime.Now);
                cmd.Connection = cn;
                cmd.CommandText = "INSERT INTO business_db.dbo.Sponsor(Branch_name,Name,Description,Image,Position,Emp_key,Date) VALUES (@com,@nam,@des,@image,@post,@ekey,@date);  
                cn.Open();
                cmd.ExecuteNonQuery();
                cn.Close();


            }

            catch (Exception ex)
            {

            }

        }


----------------------------The data base table look like this---------------------------------
ID                  int  (set to auto increament)
Branch_name         varchar(100)
Name                varchar(100)
Description         varchar(MAX)
Image               varchar(100)
Position            varchar(100)
Emp_key             varchar(50)
Date                date

 

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, Postremus said:

What exception is thrown, that gets swallowed by the catch block?

oops, completely forgot to print out the exception. i did it now and its a big confusing one 

1.JPG

Link to comment
Share on other sites

Link to post
Share on other sites

16 minutes ago, Postremus said:

Looks to me like the value of pos is null.

Can you please check if any of your variables is null, please?

 

If any of them is null, you have to insert DBNull.Value instead, see https://msdn.microsoft.com/de-de/library/system.data.sqlclient.sqlparametercollection.addwithvalue(v=vs.110).aspx

u were right, one of my variables were null. thanks for helping out

Link to comment
Share on other sites

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×