I am trying to implement a button_click
event in C# such that if the button is pressed, the gridview is filled with the query results but am getting the above error.
This is the code:
public partial class Pages_Managingpayment : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyServer"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
{
Load();
}
}
protected void Load() {
DataSet ds = new DataSet();
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
GridView1.DataSource = ds;
GridView1.DataBind();
int columncount = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
GridView1.Rows[0].Cells[0].Text = "No records on display";
}
protected void SearchButton_Click(object sender, EventArgs e)
{
if (IsPostBack)
{
BindEmployeeDetails();
}
}
protected void BindEmployeeDetails()
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from users", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
GridView1.DataSource = ds;
GridView1.DataBind();
int columncount = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
GridView1.Rows[0].Cells[0].Text = "No Records Found";
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindEmployeeDetails();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string passwords = GridView1.DataKeys[e.RowIndex].Value.ToString();
TextBox pass = (TextBox)GridView1.Rows[e.RowIndex].FindControl("Password");
TextBox usernames = (TextBox)GridView1.Rows[e.RowIndex].FindControl("username");
TextBox usertypes = (TextBox)GridView1.Rows[e.RowIndex].FindControl("usertype");
con.Open();
SqlCommand cmd = new SqlCommand("update users set User_Type='" + usertypes.Text + "',Username='" + usernames.Text + "' where password='" + passwords + "'", con);
cmd.ExecuteNonQuery();
con.Close();
//.ForeColor = Color.Green;
//lblresult.Text = username + " Details Updated successfully";
GridView1.EditIndex = -1;
BindEmployeeDetails();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindEmployeeDetails();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["UserId"].ToString());
string passwords = GridView1.DataKeys[e.RowIndex].Values["password"].ToString();
con.Open();
SqlCommand cmd = new SqlCommand("delete from users where password='" + passwords + "'", con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
BindEmployeeDetails();
// lblresult.ForeColor = Color.Red;
//lblresult.Text = username + " details deleted successfully";
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox usertypes = (TextBox)GridView1.FooterRow.FindControl("usertype");
TextBox usernames = (TextBox)GridView1.FooterRow.FindControl("username");
TextBox passwords = (TextBox)GridView1.FooterRow.FindControl("password");
con.Open();
SqlCommand cmd =
new SqlCommand(
"insert into users values('" + usertypes.Text + "','" +
usernames.Text + "','" + passwords.Text + "')", con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
BindEmployeeDetails();
// lblresult.ForeColor = Color.Green;
//lblresult.Text = txtUsrname.Text + " Details inserted successfully";
}
else
{
//lblresult.ForeColor = Color.Red;
//lblresult.Text = txtUsrname.Text + " Details not inserted";
}
}
}