Getting Error---Conversion failed when converting the varchar value 'vadodra' to data type int. [Resolved]

Posted by Ankitsrist under ASP.NET on 12/10/2012 | Points: 10 | Views : 3033 | Status : [Member] | Replies : 8
i am trying to update gridview row but m getting error:-----Conversion failed when converting the varchar value 'vadodra' to data type int.
my code is

 protected void Button1_Click(object sender, EventArgs e)
{
if (Button1.Text == "Submit")
{
myobj.myconnection();
SqlCommand sqlcmd = new SqlCommand("Insert into regional(Regional, stateid) values ('" + TextBox1.Text + "','" + DropDownList1.SelectedItem.Value + "')", myobj.sqlcon);
sqlcmd.ExecuteNonQuery();
fillgrid();
TextBox1.Text = "";
DropDownList1.ClearSelection();
}
else if(Button1.Text == "update")
{

myobj.myconnection();
SqlCommand cmd = new SqlCommand("UPDATE regional SET Regional='" + DropDownList1.SelectedItem.Value + "', stateid='"+TextBox1.Text+"' where ID='" + GridView1.SelectedValue.ToString() + "'", myobj.sqlcon);
cmd.ExecuteNonQuery();
fillgrid();

}

}





Responses

Posted by: Pavanandey on: 12/10/2012 [Member] Bronze | Points: 50

Up
0
Down

Resolved
Check the update command

UPDATE regional SET Regional='" + TextBox1.Text + "', stateid='"+ DropDownList1.SelectedItem.Value +"' where ID='" + GridView1.SelectedValue.ToString() + "'", myobj.sqlcon);

you have swapped Regional and stateid......

Mark the answer if this helps

Thanks
Pavan Kumar
Mark Answer if this fits the need

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/10/2012 [Member] Starter | Points: 25

Up
0
Down

Resolved
oooohhhhkk sir i got it....u are right....thanku sir actually i hav did silly mistake thanks a lot pavan sir.....:)

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 12/10/2012 [Member] Bronze | Points: 50

Up
0
Down

Resolved
the update query you have given shows an error

Regional is a varchar column, stateid is an integer column

"UPDATE regional SET Regional='" + DropDownList1.SelectedItem.Value + "', stateid='"+TextBox1.Text+"' where ID='" + GridView1.SelectedValue.ToString() + "'"

in your update statement you have specfied Regional = int data ( DropDownList1.SelectedItem.Value )
and stateid = varchar data ( TextBox1.Text )

check this.

Mark the answer if this helps



Thanks
Pavan Kumar
Mark Answer if this fits the need

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/10/2012 [Member] Starter | Points: 25

Up
0
Down
nope....actually regional is my table....and Regional is the column of the regional table and stateid is the foreign key of state table

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/10/2012 [Member] Starter | Points: 25

Up
0
Down
thanks again pavan sir.....

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/10/2012 [Member] Starter | Points: 25

Up
0
Down
pavan sir can i get ur email id.....so that if i get problem later i can contact u....

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/10/2012 [Member] Starter | Points: 25

Up
0
Down
pavan sir can i get ur email id.....so that if i get problem later i can contact u....

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 12/10/2012 [Member] Starter | Points: 25

Up
0
Down
yes sir i corrected that problem and now my application running flawless.....thanks

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response