Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi Friends help me

In this code cannot bind the table information.
Thanks in Advance
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <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>
        <br />
    </div>
    </form>
</body>
</html>


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];
    }
}
Posted

Please try the following

1. Create the SP(parameter @TableName) for Pulling the table records
Example:
SQL
CREATE  PROCEDURE [dbo].[GetTableData]
(
 @TableName VARCHAR(100)
)
AS

DECLARE @SQL VARCHAR(MAX)

--Always use column name, never use '*' in select.
--SET @SQL = 'SELECT * FROM ' + @TableName;
SET @SQL = 'SELECT [COLUMN NAME1],[COLUMN NAME2],.. FROM ' + @TableName;
PRINT(@SQL);
EXEC(@SQL);


2. Pass the Selected table Name As Parameter to the SP and pull the records bind it to gridview
 
Share this answer
 
v2
Hello,

You can perform this activity into two steps

1. Use the Below Query to pull out the Table Names for a Database and bind the result set to the
DropDown.

USE YourDBName
GO
SELECT Table_Name FROM INFORMATION_SCHEMA.TABLES
GO

2. On Selection of the Table name in the Dropdown, write a piece of code which fetches record from the Table selected and Bind it to the Grid.

Note : The AutoPostBack for the Drop Down must be set to True and you can write the code in the selectedIndexchanged event of the DropDown.

Hope this helps.

Regards
Himanshu
 
Share this answer
 

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