0

I have a form that include : 2 Textedit

one for input Identifier_number and another to input prod_name

1 lookUpEdit

to select prod_cat from database

1 MemoEdit

to input prod_desc

1 pictureedit

to input the pic

2 simplebutton

one for save data to database and another to fetch the pic

Note Im using tool from devexpress

I want the user input data and the program save it in data base this my code

  private void btn_done_Click(object sender, EventArgs e)
    {
        
        String sql = "insert into prodects_tab (prod_id,Identifier_number,prod_name,prod_cat,prod_desc)"
            + "Values(@prod_id,@Identifier_number,@prod_name,@prod_cat,@prod_desc)";
        using (SqlConnection cn = new SqlConnection(@"Server=.\SQLEXPRESS; Database=db_LPS_Supermarket; Integrated Security=true"))
        {

            using (SqlCommand cmd = new SqlCommand(sql, cn))
            {
                cmd.Parameters.Add("@prod_id", SqlDbType.Int).Value = 3;
                cmd.Parameters.Add("@Identifier_number", SqlDbType.BigInt).Value = txt_sri_num.Text;
                cmd.Parameters.Add("@prod_name", SqlDbType.VarChar).Value = txt_prod_name.Text;
                cmd.Parameters.Add("@prod_cat", SqlDbType.Int).Value = cmb_Cat.Text;
                cmd.Parameters.Add("@prod_desc", SqlDbType.VarChar).Value = txt_prod_desc.Text;
                // cmd.Parameters.Add("@prod_pic", SqlDbType.Image).Value = pics_prod_img;
              
                cn.Open();
                cmd.ExecuteNonQuery();
             
               
                cn.Close();
               }
            }
        }
    }

the error is in method ExecuteNonQuery()

and if anyone know how i can fatch the pic from picedit to database tell me

Yunha
  • 11
  • 2
  • `ExecuteNonQuery` has nothing to do with devepress or wpf. You can remove all the noise and rather add **complete** exception you get. – Sinatr Aug 31 '20 at 09:00
  • 1
    `cmd.Parameters.Add("@prod_cat", SqlDbType.Int).Value = cmb_Cat.Text.ToString()` - you are assigning `string` value to `int` type field. Probably you have to convert `cmb_Cat.Text` to a `int`. – Sinatr Aug 31 '20 at 09:02
  • 1
    Since you use `SqlDbType.Int` for `"@prod_id"` you need to give the value an value of type `int` but `"3".ToString()` is of type `string` – Ackdari Aug 31 '20 at 09:02
  • Does this answer your question? [How can I convert String to Int?](https://stackoverflow.com/questions/1019793/how-can-i-convert-string-to-int) – Sinatr Aug 31 '20 at 09:02
  • You're adding parameters with (ex) `SqlDbType.Int` but then passing a `string` for the value. Pass the correct type of data (`3` and not `"3"`) Also note that all your `ToString` calls are superfluous since those values are all already strings – pinkfloydx33 Aug 31 '20 at 09:04
  • no isnt the erroe . cuz even when i delete it the error stay – Yunha Aug 31 '20 at 09:23
  • Can you catch the database exception and post it here? Are you sure your Product ID column in your database table isn't an auto-generated identity column? – Brendon Aug 31 '20 at 12:30

0 Answers0