Click here to Skip to main content
15,845,681 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
I am trying to insert the path of my image in a varchar field of the db butit's giving me the incompatibility error. Can anybody tell me what am I doing wrong

C#
private void button3_Click(object sender, EventArgs e)
{
    string p = path.ToString();
    string name = this.textBox1.Text.ToString();
    string description = this.richTextBox1.Text.ToString();
    //MessageBox.Show(p);
    DateTime da = DateTime.Now;
    string date = da.ToString("dd/MM/YYYY");
    string connString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Gardezi\Documents\Visual Studio 2012\Projects\homeWork2\homeWork2\Database1.mdf;Integrated Security=True";
    SqlConnection con = new SqlConnection(connString);
    string query = "insert into diaryDB(Title , Description , Date , pic) values (@name , @description ,@date , @p)";
    SqlCommand comm = new SqlCommand(query, con);
    comm.Parameters.Add(new SqlParameter("name" , name));
    comm.Parameters.Add(new SqlParameter("description", description));
    comm.Parameters.Add(new SqlParameter("date", date));
    comm.Parameters.Add(new SqlParameter("p",p ));
    con.Open();
    comm.ExecuteNonQuery();
    con.Close();
}
Posted
Comments
DamithSL 23-Nov-14 23:10pm    
what is the error with stack trace?
Syed Muhammad Ali Gardezi 23-Nov-14 23:14pm    
System.Data.SqlClient.SqlException (0x80131904): Operand type clash: nvarchar is incompatible with image
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at homeWork2.entry.button3_Click(Object sender, EventArgs e) in c:\Users\Gardezi\Documents\Visual Studio 2012\Projects\homeWork2\homeWork2\entry.cs:line 88
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, In
[no name] 23-Nov-14 23:26pm    
What type of column is "pic". Also, what do you get in path.ToString() ?
Syed Muhammad Ali Gardezi 23-Nov-14 23:31pm    
I have a path variable declared at class level. In it I save the path of the image and this path is stored in the pic colomun of the db which is of type varchar
[no name] 24-Nov-14 3:17am    
What do you get in path.ToString(); ????
Is it the proper string that you are getting and stored in p ?

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