Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Page to select data from a list of tables

Status
Not open for further replies.

Fahy

MIS
May 30, 2001
14
0
0
IE
I have a list of 300 tables saved in a table called fieldspecifics
I have an aspx page on which I have a listbox where the user can select a table
When a table is selected I want the page to display the data from fieldnames which are specified in the fieldspecifics table
The fieldspecifics table has Tablename field, tableid field, showfield1, showfield2.
The tables I want displayed are saved in this one table where showfield1 is the first fieldname I want displayed for that table.

I wanted to do it like this so that I didn't have to write seperate aspx pages for each table.
I have included my first attempt at it which just displays the fieldname for the table selected
I want it to display all records in the table with these two field names

This is the aspx page

<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SqlClient" %>

<html>
<head>
<title>Archives - Select a Table To Search</title>
</head>

<body>
<form runat="server" method="post">
<asp:ListBox id="lbxTables"
runat="server" AutoPostBack="True"
Rows="5"
OnSelectedIndexChanged="subListChange"/>
<br/><br/>
<asp:Datagrid id= "dgtabledata" runat="server" />
</form>
</body>
</html>



<script language="VB" runat="server">

Sub Page_Load(Source As Object, E As EventArgs)
If Not IsPostback then
Dim strConnection As String = Configurationsettings.appsettings("Archives")
Dim strSQLforListbox as string = "SELECT nameoftable,TABLEID FROM fieldspecifics ORDER BY nameoftable"
Dim objConnection As New SqlConnection(strConnection)

Dim objCommand As New SqlCommand(strSQLforListbox, objConnection)
objconnection.open()
lbxTables.Datasource = objcommand.executeReader()
lbxTables.DataTextField = "nameoftable"
lbxTables.datavaluefield = "TABLEID"
lbxTables.DataBind()
objconnection.Close()
End If
End Sub

Sub subListChange(S as object, E As EventArgs)
'Response.Write("subListchange triggered")
Dim strConnection as String = Configurationsettings.appsettings("Archives")
Dim objConnection as new sqlconnection(strconnection)
Dim objcommand as new sqlcommand("sp_Settable", objconnection)
objcommand.commandtype =commandtype.storedprocedure

Dim objparameter as new sqlparameter("@tableid" ,sqldbtype.int )
objcommand.parameters.add(objparameter)
objParameter.Direction =ParameterDirection.Input
objparameter.value =lbxTables.Selecteditem.Value

objconnection.Open()
dgtabledata.datasource = objcommand.executereader()
dgtabledata.databind()
objconnection.close()
End Sub
</script>

The storeprocedure 'sp_Settable' is
CREATE PROCEDURE archivesuser.sp_Settable @tableid int
AS SELECT ShowField1, ShowField2 FROM fieldspecifics WHERE TABLEID = @tableid
GO

 
I eventually got there myself
This procedure worked for me.

CREATE PROCEDURE archivesuser.sp_Settables @tablename nvarchar(255)
AS
DECLARE @showfield1 nvarchar(255), @showfield2 nvarchar(255),@a nvarchar(255)
SELECT @ShowField1= ShowField1, @ShowField2 = Showfield2 FROM fieldspecifics
WHERE nameoftable = @tablename
SELECT @a ='SELECT ' + "[" + @ShowField1 + "]" +", ["+ @ShowField2 + "]"
+ ' FROM ' + "[" + @tablename + "]"
Execute (@a)
GO
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top