Thursday, 25 October 2012

how to retrieve data from database in asp.net using c#

For this, just follow some simple steps which is given bellow.
  1. Open Visual studio, goto file --> click New Web site --> select lcocation --> select C# as your language..
  2. Open Default.aspx and Drag Gridview from ToolBar menu (Standard Control)
  3. Or copy bellow code on to your .aspx form between <DIV></DIV> tag.


    <asp:GridView ID="GridView1" runat="server" DataKeyNames="ACODE"
    AutoGenerateColumns = "False" onrowdatabound="GridView1_RowDataBound"
    ShowFooter="True" BackColor="White" BorderColor="#336666"
    BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal">

    <FooterStyle BackColor ="White" ForeColor="#333333" />
    <RowStyle BackColor="White" ForeColor="#333333" />
    <Columns>
    <asp:BoundField DataField="ACODE" HeaderText="Product ID" />
    <asp:BoundField DataField="PRD_NAME" HeaderText="Product Name" />
    <%
    -- <asp:BoundField DataField="QTY" HeaderText="Quantity" />--%>
    <asp:TemplateField HeaderText = "Quantity">
    <ItemTemplate>
    <asp:Label ID="LblQty" runat="server" Text='<%# Bind("QTY") %>'></asp:Label>
    </ItemTemplate>
    <FooterTemplate >
    <asp:Label ID="LblSumQty" runat="server" Text="1"></asp:Label>
    </FooterTemplate>
    </asp:TemplateField>
    </Columns>
    <PagerStyle BorderStyle="None" BackColor="#336666" ForeColor="White"
    HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
    </asp:GridView>

  4. Now, Open your Default.aspx.cs page and paste bellow code and change credentials according to your system and Sql Server.. 
public static int SQty = 0;
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
viewData();
}
}
public void viewData()
{

string strconn = "data source = INTEL-PC\\SQLEXPRESS; database = JagritiA; user id = sa; password = sa;"; // change this according to your sytem database name and password.
SqlConnection sqlconn = new SqlConnection(strconn);
sqlconn.Open();
try
{

string strQuery = "select top 10 ACODE, PRD_NAME, MATCODE as QTY from tblmst_product where Matcode = 02"; //change this select query according to your databse table
SqlDataAdapter sqlDA = new SqlDataAdapter(strQuery, sqlconn);
DataSet ds = new DataSet();
       sqlDA.Fill(ds, "Product");
       GridView1.DataSource = ds;
       GridView1.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{

sqlconn.Close();
} 
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

DataRowView dtview = e.Row.DataItem as DataRowView;
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl1 = (Label)e.Row.Cells[2].FindControl("LblQty");
SQty = SQty +
Convert.ToInt32(lbl1.Text);
}
if (e.Row.RowType == DataControlRowType.Footer)
{

e.Row.Cells[1].Text =
"Total : ";
e.Row.Cells[2].Text =
Convert.ToString(SQty);
}
}

    Now if u run this, it will look like..













No comments:

Post a Comment