grid view update button not working..... [Resolved]

Posted by Ankitsrist under ASP.NET on 12/18/2012 | Points: 10 | Views : 1774 | Status : [Member] | Replies : 1
hello,
i have two dropdowns which is connected with databases and its selected value reflect on the gridview..gridview has delete and update button...delete is workin but update not working have a look on my code...
 protected void Page_Load(object sender, EventArgs e)
{
if(!Page .IsPostBack )
{
fillstateddl();
fillcityddl();
fillgrid();
}
}
public void fillstateddl()
{
dbclass.myconnection();
SqlCommand cmd = new SqlCommand("select * from states", dbclass .con);
SqlDataAdapter adp = new SqlDataAdapter(cmd );
DataSet ds = new DataSet();
adp.Fill(ds );
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "statename";
DropDownList1.DataValueField = "stateid";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem ("--select--", "0"));
}
public void fillcityddl()
{
dbclass.myconnection();
SqlCommand cmd = new SqlCommand("select * from cities", dbclass .con );
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds );
DropDownList2.DataSource = ds;
DropDownList2.DataTextField = "cityname";
DropDownList2.DataValueField = "cityid";
DropDownList2.DataBind();
DropDownList2.Items.Insert(0, new ListItem ("--select--", "0"));

}
protected void Button1_Click(object sender, EventArgs e)
{
if (Button1 .Text =="submit")
{
dbclass.myconnection();
SqlCommand cmd = new SqlCommand("insert into countrytable(statename, cityname)values('"+DropDownList1 .SelectedItem+"', '"+DropDownList2 .SelectedItem +"')", dbclass .con );
cmd.ExecuteNonQuery();
fillgrid();
GridView1.DataBind();
}
if (Button1.Text =="update")
{
dbclass.myconnection();
SqlCommand cmd1 = new SqlCommand("update countrytable set statename='"+DropDownList1 .SelectedItem .Value +"', cityname='"+DropDownList2 .SelectedItem .Value +"' where countryid='"+GridView1.SelectedValue.ToString() +"'", dbclass .con );
cmd1.ExecuteNonQuery();
fillgrid();
GridView1.DataBind();
}
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
dbclass.myconnection();
int countryid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["countryid"].ToString());
SqlCommand cmd = new SqlCommand("delete from countrytable where countryid=" + countryid, dbclass.con);
cmd.ExecuteNonQuery();
GridView1.DataBind();
fillgrid();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
dbclass.myconnection();
SqlCommand cmd = new SqlCommand("select * from countrytable where countryid='" + GridView1.SelectedValue.ToString()+ "'", dbclass.con);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
DropDownList1.SelectedItem .Value = dr["statename"].ToString();
DropDownList2.SelectedItem .Value = dr["cityname"].ToString ();
Button1.Text = "update";
}
else
{
DropDownList1.ClearSelection();
DropDownList2.ClearSelection();
}
}

please help




Responses

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

Up
0
Down

Resolved
i have got by solution myself....little changes as follows
DropDownList1.SelectedItem .Text  = dr["statename"].ToString();


DropDownList2.SelectedItem .Text = dr["cityname"].ToString ();

now its workin
and also one thing instead of selecteditem.value write selecteditem.text

SqlCommand cmd1 = new SqlCommand("update countrytable set statename='"+DropDownList1 .SelectedItem .Item +"', cityname='"+DropDownList2 .SelectedItem .Item +"' where countryid='"+GridView1.SelectedValue.ToString() +"'", dbclass .con );


i just a beginner doing engineering thats y asking such silly questions but it matters a lot for me....:)

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

Login to post response