Thursday 18 July 2013

Display combobox values from database

hi friends in this section i cover read data from database and display it on combobox using ext.net.
if you dont know session statement read our session tuts.









The procedure shown in the below ie.
first insert combobox and SqlDataSource into the project

 <form id="form1" runat="server">
    <div>
             <ext:ComboBox ID="dreamtheweb" runat="server" AnchorHorizontal="100%" DisplayField="name"
                                         ValueField="id" FieldLabel="Country">
                                    <Store>
                                        <ext:Store ID="sestore1" runat="server" DataSourceID="SqlDataSource1">
                                            <Model>
                                                <ext:Model ID="Model1" runat="server">
                                                    <Fields>
                                                        <ext:ModelField Name="id" />
                                                        <ext:ModelField Name="name">
                                                        </ext:ModelField>
                                                    </Fields>
                                                </ext:Model>
                                            </Model>
                                        </ext:Store>
                                    </Store>
                                  
                                </ext:ComboBox>
       
    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:testing %>"
            SelectCommand="SELECT * FROM [country] ">
        </asp:SqlDataSource>

    </form>
Insert connection string testing in web.config file
<connectionStrings>
        <add name="testing" connectionString="Data Source=jishnu\sqlexpress;Initial Catalog=gatikktechno;Integrated Security=True;Pooling=False"/>
    </connectionStrings>
Database definition
 id-integer type
name-varchar(50)

Asp.net Session With Example

Here i explain asp.net session using c#.
Session state is mainly used to transfer values from one page to other without storing/retrieving  values from database.
In this tutorial two files one is first.aspx and other is second.aspx.

first.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="first.aspx.cs" Inherits="dreamtheweb.first" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
   <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="TextBox1" runat="server">Fname</asp:TextBox>
 <br />  <br />        <asp:TextBox ID="TextBox2" runat="server">lanme</asp:TextBox>
        <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <br />
  <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_click" />        <br />
   </div>
    </form>
</body>
</html>
first.aspx.cs page
Add  session statement inside button click ie,
  protected void Button1_click(object sender, EventArgs e)
        {
            Session["fname"] = TextBox1.Text;
            Session["lname"] = TextBox2.Text;
            Response.Redirect("second.aspx");
        }

The second.aspx page 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="second.aspx.cs" Inherits="dreamtheweb.second" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

For accept session values insert the below code in pageload section ie
protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = Session["fname"].ToString();
            Label2.Text = Session["lname"].ToString();
          
             
            }
If you enjoy and work this post please support this blog and visit again