Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi Friends help me
 
In this code cannot bind the table information.
Thanks in Advance
<%@ 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>
 

 
 
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 15-Jan-13 19:17pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please try the following
 
1. Create the SP(parameter @TableName) for Pulling the table records
Example:
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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 95
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 29 May 2013
Copyright © CodeProject, 1999-2014
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