hello there ,,, i get tired from my code , it's not updating when i used to an other gridview i'ts updating ......
i have 3 tables employee,gender,country
employee colums are employeeid(PK),name,age,genderid,countryid,mobile
gender coulms are genderid(PK),gendername
country coums are countryid(PK),countryname
in row updating event not updating , here is my code
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string s = GridView1.DataKeys[e.RowIndex].Value.ToString();
Label id = (Label)GridView1.Rows[e.RowIndex].FindControl("lblEditID") as Label;
Label EmployeeID = (Label)GridView1.Rows[e.RowIndex].FindControl("lblEmployeeID") as Label;
TextBox name = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditName") as TextBox;
TextBox age = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditAge") as TextBox;
DropDownList gender = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropGender") as DropDownList;
DropDownList country = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropCountry") as DropDownList;
TextBox mobile = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditMobile") as TextBox ;
string stringconnectiong = ConfigurationManager.ConnectionStrings["Employee_TestConnectionString"].ConnectionString;
string sql = "UPDATE Employee SET Name=@Name,Age=@Age,GenderID=@GenderID ,CountryID=@CountryID ,Mobile=@Mobile WHERE (EmployeeID=@EmployeeID) AND (ID=@ID)";
SqlConnection con = new SqlConnection(stringconnectiong);
// DataTable dt = new DataTable();
try
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Parameters.AddWithValue("@Name", name.Text);
cmd.Parameters.AddWithValue("@Age", age.Text);
cmd.Parameters.AddWithValue("@GenderID", gender.SelectedValue);
cmd.Parameters.AddWithValue("@CountryID", country.SelectedValue);
cmd.Parameters.AddWithValue("@Mobile", mobile.Text);
cmd.Parameters.AddWithValue("@EmployeeID", EmployeeID.Text);
cmd.Parameters.AddWithValue("@ID", id.Text);
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
cmd.Connection = con;
cmd.ExecuteNonQuery();
SqlDataAdapter dr = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
dr.Fill(ds);
GridView1.DataSource = ds;
GridView1.EditIndex = -1;
BindGridview();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Error Updating ";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
con.Close();
con.Dispose();
}
}
void BindGridview()
{
string connectionstring = ConfigurationManager.ConnectionStrings["Employee_TestConnectionString"].ConnectionString;
string query = "SELECT Employee.ID,Employee.EmployeeID,Employee.Name,Employee.Age,Employee.GenderID, Gender.GenderName,Employee.CountryID,Country.CountryName,Employee.Mobile FROM Employee INNER JOIN Country ON Employee.CountryID=Country.CountryID INNER JOIN Gender ON Employee.GenderID=Gender.GenderID" ;
SqlConnection con = new SqlConnection(connectionstring);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = query;
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}