Click here to Skip to main content
14,664,450 members
Rate this:
Please Sign up or sign in to vote.
See more:
currently i'm designing application to retrieve the table and show it in my webpage from the sql server 2008 by getting table name from the user.. how can i do it... help me
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Good example for beginners:
datagridview-tutorial[^]
   
Comments
Ramkumar K 5-Mar-13 6:33am
   
Thanks,,.choudhary.sumit .. but i know about it.. my doubt is, i want to get table name from user and display it accordingly... i dont want to display particular table alone... table must be chosed by user only
choudhary.sumit 5-Mar-13 6:38am
   
USe parameterized query in that case. if u r passing table name with the textbox1, then replace the "animal" with the "+textbox1.text+" in the above example.
Ramkumar K 5-Mar-13 6:43am
   
what about the columns...? some table contains 3 columns and others may have different number of column?.. :(
Ramkumar K 5-Mar-13 6:45am
   
consider that in my grid if i included 3 columns only... then if user choses 4 column table means..how can i fit that in grid...
choudhary.sumit 5-Mar-13 6:45am
   
ok. post your code here in which you are binding your datasource.
Ramkumar K 5-Mar-13 6:58am
   
my aspx file:

<!--Show my table -->
<div id="tb" runat="server">
<table id="mytb" runat="server">
<tr>
<td>
Enter the table u wished to display here:
</td>
<td>
<asp:TextBox ID="gettbname" runat="server">
</td>
</tr>
<tr>
<td>
<asp:Button ID="sbt" Text="submit" runat="server"/>
</td>
</tr>
</table>
</div>


<div id="gridacc" class="gridacc" runat="server">
<asp:GridView ID="GridView1" DataKeyNames="UserId"
AutoGenerateColumns="false" BackColor="white"
BorderColor="#333300" runat="server"
Style="left: 239px; position: absolute;top: 40px"
AlternatingRowStyle-BackColor="#D8D8C5">
<columns> <asp:BoundField HeaderText="Id" DataField="UserId" />
<asp:BoundField HeaderText="Name" DataField="Fullname" />
<asp:BoundField HeaderText="Email" DataField="Email" />


</div>
<!-- End of Show my table -->

My ASPX.CS FILE:
protected void sbt_Click(object sender, EventArgs e)
{
SqlConnection con1 = new SqlConnection(connection);
con1.Open();
SqlCommand cmd = new SqlCommand("Select * from " + gettbname.Text + "", con1);
SqlDataAdapter data = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
data.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con1.Close();
}
Ramkumar K 5-Mar-13 7:10am
   
how can i bring the name of the columns of the user requested table into '
Asp:datafield '
Ramkumar K 5-Mar-13 7:11am
   
i think i should get the name of the table and columns from user .. via textbox.. is that the only way huh???

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




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