Click here to Skip to main content
12,360,217 members (55,659 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL-Server
<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 15-Jan-13 6:15am
Updated 15-Jan-13 17:56pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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();
        }
    }
}
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100