Click here to Skip to main content
Sign Up to vote bad
good
See more: C#
plz solve my problem i have get This error message
 
< Implicit conversion from data type varchar to varbinary is not allowed. Use the CONVERT function to run this query. >
 
I am using SQL SERVER Date datatype using for Datetimepicker (custum format)
 
plz help me for this problam
 
private void button4_Click(object sender, EventArgs e)
       {
 
           Convert.ToDateTime(this.dateTimePicker1.Value).ToString("MM/dd/YYYY");
           textBox1.Text = dateTimePicker1.Text;
       }
 
       private void button1_Click(object sender, EventArgs e)
       {
           try
           {
              
               con.Open();
                   
               String q = "insert into Development_Detal values('" + Ename.Text + "','" + UserPass.Text + "','" + EmpID.Text + "','" + address.Text + "','" + emaill.Text + "','" +textBox1.Text+"','" + mobNo.Text + "','" + city.Text + "' )";
 
               SqlCommand cmd = new SqlCommand(q, con);
              
 
               if ((Ename.Text == "") || (UserPass.Text == "") || (EmpID.Text == "") || (address.Text == "") || (mobNo.Text == "") || (city.Text == ""))
               {
                   MessageBox.Show("Must be fill all data ");
               }
               else
               {
                   cmd.ExecuteNonQuery();
                   MessageBox.Show("Employee Registration Successfully");
               }
           }
 
               catch(Exception ex)
               {
                   MessageBox.Show(ex.Message);
               }
           finally
           {
               con.Close();
           }
 

         }

 
Thanks in advance
Posted 19-Feb-13 23:05pm
Edited 19-Feb-13 23:14pm


3 solutions

As the error suggests varchar cannot be converted to varbinary.
You are sending varchar text to the query.
 
Either use the Convert command on the database side
 
OR
 

instead of using +textBox1.Text+" directly in query, use
Convert.ToDateTime(textBox1.Text)
  Permalink  
Hi
 
The lines:
 
 Convert.ToDateTime(this.dateTimePicker1.Value).ToString("MM/dd/YYYY");
 
will not even allow you to compile. It's because dateTimePicker1.Value will return a datetime datatype and it will give you the error "Cannot implicitly conversion.
 
you have two options two store the value of dateTimePicker1 in a variable.
 
1. store it as string type.
string date;
date = dateTimePicker1.Value.ToString("yyyy/MM/dd");
OR
 
2. store it as datetime type.
DateTime date;
date = dateTimePicker1.Value;
 
and then you can set these with text box control like( textBox1.Text =date;) and set into sql query like('"+Date+"')
 
Thanks
 
There are various ways to save and retrieve this in a database.
 
Happy to Help....Thanks
  Permalink  
As the error suggests varchar cannot be converted to varbinary.
You are sending varchar text to the query.
 
Either use the Convert command on the database side[^]
 
OR
 

instead of using +textBox1.Text+" directly in query, use
Convert.ToDateTime(textBox1.Text)
  Permalink  
Comments
Syed Shahabuddin - 20-Feb-13 5:46am
Thanks sir but now i get erorr this messg "String was not recognized as a valid DateTime" how to solve how to String recognized??
Shubh Agrahari - 20-Feb-13 6:07am
its formatting problem...use correct for of your date like dd/mm/yyyy
Syed Shahabuddin - 21-Feb-13 1:21am
Thanks sir now its showing error " Conversion Failed when converting date/or time from charecter string" i already change custum in properties window plz solve this Thanks
Syed Shahabuddin - 21-Feb-13 5:59am
Thanks all I got solution :)
Shubh Agrahari - 21-Feb-13 7:44am
ya its our pleasure..........

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Prasad_Kulkarni 407
1 Christian Graus 363
2 Sergey Alexandrovich Kryukov 325
3 _Amy 288
4 OriginalGriff 285
0 Sergey Alexandrovich Kryukov 6,649
1 Prasad_Kulkarni 3,281
2 _Amy 3,065
3 OriginalGriff 2,989
4 CPallini 2,696


Advertise | Privacy | Mobile
Web01 | 2.6.130617.1 | Last Updated 20 Feb 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid