Back to Home Page

/*****************************************************************************
Grid View / Data OleDb Example
Author : Guillermo Julca

View Output
*****************************************************************************/

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Text;

using System.Collections;

using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page

{

    string strconnectString;

    string strcommandText;

 

    protected void Page_Load(object sender, EventArgs e)

    {

        strconnectString = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" + Server.MapPath("App_Data/Northwind.mdb");

        Session["StringConnection"] = strconnectString;    

    }

    protected void btnAddRow_Click(object sender, EventArgs e)

    {

        Response.Redirect("AddForm.aspx");

    }

    protected void btnDeleteRow_Click(object sender, EventArgs e)

    {

        ArrayList ProductIdList;

        OleDbCommand cmd = new OleDbCommand();

        OleDbConnection cn = new OleDbConnection(strconnectString);

 

        ProductIdList = new ArrayList();

 

        try

        {

            // To populate "ProductIdList" with the ProductIds primary keys

            // that were selected for the user from the GridView control checkbox column

 

            for (int i = 0; i < GridViewProducts.Rows.Count; i++)

            {

                GridViewRow row = GridViewProducts.Rows[i];

                bool isChecked = ((CheckBox)row.FindControl("chkDelete")).Checked;

 

                if (isChecked)

                {

                    ProductIdList.Add(GridViewProducts.DataKeys[i].Values[0].ToString());

                }

            }

 

            cmd.Connection = cn;

            cmd.CommandTimeout = 120; // 2 minutes.

 

            //open connection

            cn.Open();

 

            // delete selected Product Names

            foreach (string strProductID in ProductIdList)

            {

                strcommandText = "Delete From Products " +

                                 "Where ProductID = " + strProductID;

 

                cmd.CommandText = strcommandText;

 

                //Execute Query

                cmd.ExecuteNonQuery();

            }

        }

        finally

        {

            //Close connection

            cn.Close();

 

            //Binds the data source

            GridViewProducts.DataBind();

        }

    }

}

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.OleDb;

using Microsoft.Build.Tasks;

 

 

public partial class AddForm : System.Web.UI.Page

{

    string strconnectString;

    string strcommandText;

 

    protected void Page_Load(object sender, EventArgs e)

    {

        //strconnectString = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" + Server.MapPath("App_Data/Northwind.mdb");

        strconnectString = (string)(Session["StringConnection"]);

    

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        Response.Redirect("Main.aspx");

    }

    protected void btnAdd_Click(object sender, EventArgs e)

    {

        string strProductName;

        string strQuantityPerUnit;

        float dblUnitPrice;

        int intUnitsInStock;

        int intUnitsOnOrder;

 

        if (ValidateData() == true)

        {

            OleDbCommand cmd = new OleDbCommand();

            OleDbConnection cn = new OleDbConnection(strconnectString);

 

            try

            {

                cmd.Connection = cn;

                cmd.CommandTimeout = 120; // 2 minutes.

 

                strProductName = txtProductName.Text;

 

                if (txtQuantityPerUnit.Text.Length > 0)

                {

                    strQuantityPerUnit = txtQuantityPerUnit.Text;

                }

                else

                {

                    strQuantityPerUnit = " ";

                }

                if (txtUnitPrice.Text.Length > 0)

                {

                    dblUnitPrice = float.Parse(txtUnitPrice.Text);

                }

                else

                {

                    dblUnitPrice = 0;

                }

                if (txtUnitsInStock.Text.Length > 0)

                {

                    intUnitsInStock = int.Parse(txtUnitsInStock.Text);

                }

                else

                {

                    intUnitsInStock = 0;

                }

                if (txtUnitsOnOrder.Text.Length > 0)

                {

                    intUnitsOnOrder = int.Parse(txtUnitsOnOrder.Text);

                }

                else

                {

                    intUnitsOnOrder = 0;

                }

 

                strcommandText = "Insert into Products " +

                                 "(ProductName,QuantityPerUnit,UnitPrice," +

                                 "UnitsInStock,UnitsOnOrder) " +

                                 "Values('" + strProductName +

                                 "','" + strQuantityPerUnit +

                                 "'," + dblUnitPrice.ToString() +

                                 "," + intUnitsInStock.ToString() +

                                 "," + intUnitsOnOrder.ToString() + ")";

 

                cmd.CommandText = strcommandText;

 

                //open connection

                cn.Open();

 

                //Execute Query

                cmd.ExecuteNonQuery();

            }

                /*

            catch (sqlexception SQLexc)

            {

                Message.text = sqlexc.tostring();

            }

            catch (exception exc)

            {

                Message.text = Message.text + sqlexc.tostring();

            }

                 * */

            finally

            {

                //Close connection

                cn.Close();

                //Response.Write(strcommandText);

                Response.Redirect("Main.aspx");

            }

           

        }

       

    }

    protected bool ValidateData()

    {

        bool result;

        result = false;

        if ((RequiredFieldProductName.Text.Length > 0)

            && (RangePriceValidator.Text.Length > 0)

            && (RangeUnitStockValidator.Text.Length > 0)

            && (RangeUnitsOrderValidator.Text.Length > 0))

        {

            result = true;

        }

        return result;

    }

}

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.OleDb;

 

public partial class UpdateForm : System.Web.UI.Page

{

    string strconnectString;

    string strcommandText;

    string strProdIDTarget;

 

    protected void Page_Load(object sender, EventArgs e)

    {

        strconnectString = (string)(Session["StringConnection"]);

 

        //To get the selected primary key

        strProdIDTarget = Request.QueryString["ProdID"].ToString();

 

        if (!IsPostBack)

        {

            //To initialize the text boxes with the current field values

            txtProductName.Text = Request.QueryString["ProdName"].ToString();

            txtQuantityPerUnit.Text = Request.QueryString["QPU"].ToString();

            txtUnitPrice.Text = Request.QueryString["UPrice"].ToString();

            txtUnitsInStock.Text = Request.QueryString["UIS"].ToString();

            txtUnitsOnOrder.Text = Request.QueryString["UOO"].ToString();

        }

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        Response.Redirect("Main.aspx");

    }

    protected void btnUpdate_Click(object sender, EventArgs e)

    {

        string strProductName;

        string strQuantityPerUnit;

        float dblUnitPrice;

        int intUnitsInStock;

        int intUnitsOnOrder;

 

        if (ValidateData() == true)

        {

            OleDbCommand cmd = new OleDbCommand();

            OleDbConnection cn = new OleDbConnection(strconnectString);

 

            try

            {

                cmd.Connection = cn;

                cmd.CommandTimeout = 120; // 2 minutes.

 

                strProductName = txtProductName.Text;

 

                if (txtQuantityPerUnit.Text.Length > 0)

                {

                    strQuantityPerUnit = txtQuantityPerUnit.Text;

                }

                else

                {

                    strQuantityPerUnit = " ";

                }

                if (txtUnitPrice.Text.Length > 0)

                {

                    dblUnitPrice = float.Parse(txtUnitPrice.Text);

                }

                else

                {

                    dblUnitPrice = 0;

                }

                if (txtUnitsInStock.Text.Length > 0)

                {

                    intUnitsInStock = int.Parse(txtUnitsInStock.Text);

                }

                else

                {

                    intUnitsInStock = 0;

                }

                if (txtUnitsOnOrder.Text.Length > 0)

                {

                    intUnitsOnOrder = int.Parse(txtUnitsOnOrder.Text);

                }

                else

                {

                    intUnitsOnOrder = 0;

                }

 

                strcommandText = "Update Products " +

                                 "Set   ProductName = '" + strProductName +

                                 "',QuantityPerUnit = '" + strQuantityPerUnit +

                                 "',UnitPrice = " + dblUnitPrice.ToString() +

                                 ",UnitsInStock = " + intUnitsInStock.ToString() +

                                 ",UnitsOnOrder = " + intUnitsOnOrder.ToString() +

                                 " Where ProductID = " + strProdIDTarget;

                cmd.CommandText = strcommandText;

                 //open connection

                cn.Open();

                 //Execute Query

                cmd.ExecuteNonQuery();

            }

            /*

            catch (sqlexception SQLexc)

            {

                Message.text = sqlexc.tostring();

            }

            catch (exception exc)

            {

                Message.text = Message.text + sqlexc.tostring();

            }

             * */

            finally

            {

                //Close connection

                cn.Close();

                //Response.Write(strcommandText);

                Response.Redirect("Main.aspx");

            }

         }

    }

    protected bool ValidateData()

    {

        bool result;

        result = false;

        if ((RequiredFieldProductName.Text.Length > 0)

            && (RangePriceValidator.Text.Length > 0)

            && (RangeUnitStockValidator.Text.Length > 0)

            && (RangeUnitsOrderValidator.Text.Length > 0))

        {

            result = true;

        }

        return result;

    }

}

 View Output
//---------------------------------------------------------------------------

Back to Home Page



Copyright  © 2002                                   GJ  GUILLERMO JULCA