Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a windows form that get input from a user then store the data in a sql database. One of the feilds is a picturebox. I want to save the image as binary and then store it along with the other values in the sql database. My code for the save button is as follows:
C#
SqlConnection cs = new SqlConnection ("Data Source=LAPTOP-QB470LUM\\SQLEXPRESS01;Initial Catalog=AssertManagement;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter();
da.InsertCommand  = new SqlCommand ("Insert into Road_Sign values (@sign_id, @year_made, @route, @section, @date_intalled, @expiry_date, @position, @condition, @chainage_from,  @chainage_to, @picture)",cs);
            
da.InsertCommand.Parameters.Add("@sign_id", SqlDbType.Text).Value = txtSignId.Text;
da.InsertCommand.Parameters.Add("@year_made", SqlDbType.Date).Value =dtpYearMade.Value;
da.InsertCommand.Parameters.Add("@route", SqlDbType.Text).Value = txtRoute.Text;
da.InsertCommand.Parameters.Add("@section", SqlDbType.Text).Value = txtSection.Text;
da.InsertCommand.Parameters.Add("@date_intalled", SqlDbType.Date).Value = dtpDateInstalled.Value;
da.InsertCommand.Parameters.Add("@expiry_date", SqlDbType.Date).Value = dtpExpiryDate.Value;
if (radioButton1.Checked == true)
{
    da.InsertCommand.Parameters.Add("@position", SqlDbType.Text).Value = radioButton1.Checked;
}
else if (radioButton2.Checked == true)
{
    da.InsertCommand.Parameters.Add("@position", SqlDbType.Text).Value = radioButton2.Checked;
}
else
{
    da.InsertCommand.Parameters.Add("@position", SqlDbType.Text).Value = radioButton3.Checked;
}
da.InsertCommand.Parameters.Add("@condition", SqlDbType.Text).Value = txtCondition.Text;
da.InsertCommand.Parameters.Add("@chainage_from", SqlDbType.Text).Value = txtChainfrom.Text;
da.InsertCommand.Parameters.Add("@chainage_to", SqlDbType.Text).Value = txtChainTo.Text;
da.InsertCommand.Parameters.Add("@picture", SqlDbType.VarBinary).Value = DBNull.Value;
           
        
cs.Open();
da.InsertCommand.ExecuteNonQuery();
cs.Close();

btnSave.Enabled = false;

MessageBox.Show("Data Saved Successfully");

When I debug, I get the following error: operand type clash: text is incompatable with date.

The data type for the picture column in sql is a varbinary.

Is there an error in my code that I'm not picking up? Or do I need to change my approach to the project? Kindly assist.

What I have tried:

When I comment out the da.InsertComand.ExecuteNonQuery(); it goes to disable the save button and displays the message box. Obviously this isn't helping because the data is not being saved.
Posted
Updated 12-Feb-20 4:31am
v2
Comments
Richard Deeming 12-Feb-20 8:59am    
(Moved to solution)
phil.o 12-Feb-20 9:10am    
I still wonder why you did not submit this as a solution :)
Richard Deeming 12-Feb-20 10:31am    
Done. :)

1 solution

The error you're getting is nothing to do with the picture (which you're not actually saving at the moment).

It means one of the columns in your table doesn't match the type of data you're trying to insert into it.

Start by specifying an explicit column list in your insert statement:
SQL
INSERT INTO Road_Sign (sign_id, year_made, route, section, date_intalled, expiry_date, position, condition, chainage_from, chainage_to, picture) VALUES (@sign_id, @year_made, @route, @section, @date_intalled, @expiry_date, @position, @condition, @chainage_from, @chainage_to, @picture)

If it still doesn't work, compare the column types in your table to the parameter types you're passing in.
 
Share this answer
 

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