Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
ASP.NET
<asp:DropDownList ID="DropDownList1" runat="server" 
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem>--select table--</asp:ListItem>
            <asp:ListItem>Student</asp:ListItem>
            <asp:ListItem>category</asp:ListItem>
            <asp:ListItem>logindetails</asp:ListItem>
        </asp:DropDownList>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
<pre lang="c#">

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        SqlConnection con = new SqlConnection("Data Source=VICTORIN11-PC\\SQLEXPRESS;Initial Catalog=db;Integrated Security=True");
        SqlDataAdapter da;
        DataSet ds = new DataSet();
        con.Open();
        for (int i = 0; i < DropDownList1.Items.Count; i++)
        {

            string s = "select * from " + DropDownList1.Items[1].ToString();
            da = new SqlDataAdapter(s, con);
            da.Fill(ds, DropDownList1.Items[i].ToString());

        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataSet ds = new DataSet ();
        DropDownList1.DataSource = ds.Tables[DropDownList1.SelectedIndex];
    }
}


Hi Friends help me

In the above .cs code cannot bind the table information.

I'm developing web application like dropdownlist,gridview controls in ddl items like tables names and when i'm clicking the first item and it loads the first table information to the gridview control and second item clicking that table information binding to gridview control.Give me suggestions how to write the business logic code in c#.net.






Thanks in Advance.
Posted
Updated 15-Jan-13 17:56pm
v2

Try this
<asp:dropdownlist id="DropDownList1" runat="server" autopostback="true" onselectedindexchanged="DropDownList1_SelectedIndexChanged" >
            <asp:listitem>--select table--</asp:listitem>
            <asp:listitem>Student</asp:listitem>
            <asp:listitem>category</asp:listitem>
            <asp:listitem>logindetails</asp:listitem>
        </asp:dropdownlist>
        <asp:gridview id="GridView1" runat="server" >
        </asp:gridview>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Default2 : System.Web.UI.Page
{
    int iSIndex;
    DataSet ds = new DataSet();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection con = new SqlConnection(@"Data Source=VICTORIN11-PC\\SQLEXPRESS;Initial Catalog=db;Integrated Security=True");
            SqlDataAdapter da;
            con.Open();
            for (int i = 1; i < DropDownList1.Items.Count; i++)
            {
                string s = "select * from " + DropDownList1.Items[i].ToString();
                da = new SqlDataAdapter(s, con);
                da.Fill(ds, DropDownList1.Items[i].ToString());
            }
            ViewState["data"] = ds;
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        iSIndex = DropDownList1.SelectedIndex;
        if (iSIndex > 0)
        {
            ds = (DataSet)ViewState["data"];
            GridView1.DataSource = ds.Tables[iSIndex - 1];
            GridView1.DataBind();
        }
        else
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }
}
 
Share this answer
 
v3
Comments
Shekar Raja 16-Jan-13 1:50am    
Thank you
pryashrma 16-Jan-13 1:55am    
you're welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900