GridViews

The point of this page is to demonstrate the use of C# code inside the ASP.net Framework with examples of Data Entry through GridViews.

 Text OnlyNumbers Only
Edit Delete This is Text 123456
Edit Delete Here Again once more 1243738
Add    

The following code is the LOADING DATA HERE:

public partial class About : Page
{
    private String dbConnection = ConfigurationManager.ConnectionStrings["MYCONNECTIONSTRING"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
 
    private void BindData()
    {
        try
        {
            string strQuery = "SELECT DataExample.* FROM DataExample";
            SqlCommand cmd = new SqlCommand(strQuery);
            GridView1.DataSource = GetData(cmd);
            GridView1.DataBind();
            GridView1.FooterRow.Cells.RemoveAt(3);
        }
        catch
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "showalert""alert('There was an Error accessing the Database');"true);
        }
    }
 
    private DataTable GetData(SqlCommand cmd)
    {
 
 
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(dbConnection);
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        con.Open();
        sda.SelectCommand = cmd;
        sda.Fill(dt);
        return dt;
        
     }
}

The following code is the EDITING DATA HERE:

protected void EditData(object sender, GridViewEditEventArgs e)
    {
        try
        {
        GridView1.EditIndex = e.NewEditIndex;
        BindData();
        }
        catch
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "showalert""alert('There was an Error accessing the Database');"true);
        }
    }
    protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindData();
    }
    public void UpdateData(object sender, GridViewUpdateEventArgs e)
    {
        string ID = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblID")).Text;
        string TextOnly = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTextOnly")).Text;
        string NumbersOnly = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtNumbersOnly")).Text;
 
        try
        { 
        SqlConnection con = new SqlConnection(dbConnection);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
 
        cmd.CommandText = "UPDATE DataExample SET TextOnly=@TextOnly,NumbersOnly=@NumbersOnly, DateOnly=@DateOnly WHERE ID=@ID;" +
         "SELECT DataExample.* FROM DataExample";
        cmd.Parameters.Add("@ID"SqlDbType.VarChar).Value = ID;
        cmd.Parameters.Add("@TextOnly"SqlDbType.VarChar).Value = TextOnly;
        cmd.Parameters.Add("@NumbersOnly"SqlDbType.VarChar).Value = NumbersOnly;
        cmd.Parameters.Add("@DateOnly"SqlDbType.VarChar).Value = DateTime.Now;
 
 
        GridView1.EditIndex = -1;
        GridView1.DataSource = GetData(cmd);
        GridView1.DataBind();
        }
        catch
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "showalert""alert('There was an Error accessing the Database');"true);
        }
    }

The following code is the ADDING DATA HERE:

protected void AddNewData(object sender, EventArgs e)
    {
        try
        { 
        string TextOnly = ((TextBox)GridView1.FooterRow.FindControl("txtAddTextOnly")).Text;
        double NumbersOnly = double.Parse(((TextBox)GridView1.FooterRow.FindControl("txtAddNumbersOnly")).Text);
        //string DateOnly = ((TextBox)GridView1.FooterRow.FindControl("txtAddDateOnly")).Text;
 
        SqlConnection con = new SqlConnection(dbConnection);
        SqlCommand cmd = new SqlCommand();
 
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "Insert INTO DataExample(TextOnly, NumbersOnly, DateOnly) values(@TextOnly, @NumbersOnly, @DateOnly)" +
         "SELECT DataExample.* FROM DataExample";
 
        cmd.Parameters.AddWithValue("@TextOnly"SqlDbType.VarChar).Value = TextOnly;
        cmd.Parameters.AddWithValue("@NumbersOnly"SqlDbType.Decimal).Value = NumbersOnly;
        cmd.Parameters.AddWithValue("@DateOnly"SqlDbType.Date).Value = DateTime.Now;
 
        GridView1.DataSource = GetData(cmd);
        GridView1.DataBind();
        }
        catch
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "showalert""alert('There was an Error accessing the Database');"true);
        }
    }

The following code is the DELETING DATA HERE:

protected void DeleteData(object sender, EventArgs e)
   {
       try
       {
       LinkButton lnkRemove = (LinkButton)sender;
       SqlConnection con = new SqlConnection(dbConnection);
       SqlCommand cmd = new SqlCommand();
       cmd.CommandType = CommandType.Text;
       cmd.CommandText = "DELETE FROM  DataExample WHERE ID=@ID;" +
        "SELECT DataExample.* FROM DataExample";
       cmd.Parameters.Add("@ID"SqlDbType.VarChar).Value = lnkRemove.CommandArgument;
       GridView1.DataSource = GetData(cmd);
       GridView1.DataBind();
       }
       catch
       {
 
           ScriptManager.RegisterStartupScript(this, GetType(), "showalert""alert('There was an Error accessing the Database');"true);
       }
   }