Reputation: 5
I'm trying this coding , what is the error in this coding:
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source =TWPL-PC\\SQLEXPRESS;Initial Catalog=TWPLSOFTWARE;Integrated security = True");
con.Open();
SqlCommand cmd = new SqlCommand("select ACC_ID ,ACC_TYPE ,ACC_ADD,ACC_CITY ,ACC_STATE ,ACC_COUNTRY ,ACC_TIN_N0 ,ACC_ECC_NO,ACC_RANGE,ACC_DIVISION ,ACC_COMMISS ,ACC_GST_NO ,ACC_CONTACT_PERSON ,ACC_PHONE_NO ,ACC_MOBILE_NO ,ACC_EMAIL ,ACC_PRICE_CODE ,ACC_APPROVAL_STATUS,ACC_PAN_NO from ACCOUNT where ACC_NAME='" + comboBox2.SelectedItem.ToString() + "'", con);
cmd.Parameters.Add(new SqlParameter("@ACC_NAME", comboBox2.SelectedItem.ToString()));
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
textBox17.Text = dr.GetString(0).ToString();
comboBox1.SelectedItem = dr.GetString(1).ToString();
richTextBox1.Text = dr.GetString(2).ToString();
textBox2.Text = dr.GetString(3).ToString();
textBox3.Text = dr.GetString(4).ToString();
textBox4.Text = dr.GetString(5).ToString();
textBox9.Text = dr.GetString(6).ToString();
textBox11.Text = dr.GetString(7).ToString();
textBox13.Text = dr.GetString(8).ToString();
textBox14.Text = dr.GetString(9).ToString();
textBox16.Text = dr.GetString(10).ToString();
textBox12.Text = dr.GetString(11).ToString();
textBox5.Text = dr.GetString(12).ToString();
textBox6.Text = dr.GetString(13).ToString();
textBox8.Text = dr.GetString(14).ToString();
textBox7.Text = dr.GetString(15).ToString();
textBox15.Text = dr.GetString(16).ToString();
//checkBox1.Checked=dr.GetString().ToString();
textBox10.Text = dr.GetString(18).ToString();
}
}
this is my problem :
"The data types text and varchar are incompatible in the equal to operator."
Upvotes: 0
Views: 5847
Reputation: 98858
You are assing @ACC_NAME
variable in your query first, then you are tring to add as a parameter again. Check out SqlCommand.Parameters
from MSDN.
Try like this;
SqlCommand cmd = new SqlCommand("select ACC_ID ,ACC_TYPE ,ACC_ADD,ACC_CITY ,ACC_STATE ,ACC_COUNTRY ,ACC_TIN_N0 ,ACC_ECC_NO,ACC_RANGE,ACC_DIVISION ,ACC_COMMISS ,ACC_GST_NO ,ACC_CONTACT_PERSON ,ACC_PHONE_NO ,ACC_MOBILE_NO ,ACC_EMAIL ,ACC_PRICE_CODE ,ACC_APPROVAL_STATUS,ACC_PAN_NO from ACCOUNT where ACC_NAME=@ACC_NAME;", con);
cmd.Parameters.AddWithValue("@ACC_NAME", comboBox2.SelectedItem.ToString()));
Also based on your error message;
The data types text and varchar are incompatible in the equal to operator.
In your database, I think your ACC_NAME
column type is text
. That's why you are getting an error when you try to assing it in a string. You have two options;
ACC_NAME
column type to nvarchar
in your database. Text
datatype is deprecated.LIKE
operator instead of =
in your WHERE
clause. You can't compare against text
with the =
operator. Try;where ACC_NAME LIKE @ACC_NAME;
Upvotes: 2
Reputation: 1150
There is no @ACC_NAME parameter in your SQL query. Either modify your query like
SqlCommand cmd = new SqlCommand("select ACC_ID ,ACC_TYPE ,ACC_ADD,ACC_CITY ,ACC_STATE ,ACC_COUNTRY ,ACC_TIN_N0 ,ACC_ECC_NO,ACC_RANGE,ACC_DIVISION ,ACC_COMMISS ,ACC_GST_NO ,ACC_CONTACT_PERSON ,ACC_PHONE_NO ,ACC_MOBILE_NO ,ACC_EMAIL ,ACC_PRICE_CODE ,ACC_APPROVAL_STATUS,ACC_PAN_NO from ACCOUNT where ACC_NAME=@ACC_NAME;", con);
or remove the cmd.Parameters.Add(new SqlParameter("@ACC_NAME", comboBox2.SelectedItem.ToString()));
statement.
EDIT :
It seems that comboBox1.SelectedItem = dr.GetString(1).ToString();
is causing the problem.
I would suggest using comboBox2.Text = dr.GetString(1).ToString();
or better to set using comboBox2.SelectedIndex
Upvotes: 0
Reputation: 317
I suppose this is a winform project and the error comes from the
comboBox2.SelectedItem.ToString()
If comboBox2 is bound to a datasource, try to use SelectedValue or SelectedText
Otherwise you can also use the Text property of the Combobox also
Upvotes: 0