Display the tables in gridview depends on selected table in droopdownlist?

Posted by gurramsra1-10894 under ASP.NET on 1/28/2012 | Points: 10 | Views : 3046 | Status : [Member] | Replies : 4
When we select a table Name in Dropdownlist that table data should be displayed in gridview.
The problem is tha following code is working in WindowsForms but not in Asp.net. I want to do the same thing in Asp.net? What can i do


namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection("user id=sa;password=hari;database=emp;server=.");
SqlDataAdapter da;
DataSet ds;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
string s = "select * from empdetails";
da = new SqlDataAdapter(s, con);
ds=new DataSet();
da.Fill(ds,"empdetails");
string s1 = "select * from std";
da = new SqlDataAdapter(s1, con);
da.Fill(ds, "std");
cmbTable.Items.Add("empdetails");
cmbTable.Items.Add("std");
}

private void cmbTable_SelectedIndexChanged(object sender, EventArgs e)
{
DGVSample.DataSource = ds;
DGVSample.DataMember = cmbTable.SelectedItem.ToString();
}
}
}

Sra1


Responses

Posted by: Mittaikariss on: 1/28/2012 [Member] Bronze | Points: 25

Up
0
Down
SqlCommand cd = new SqlCommand("select id as Sno,mainproduct as MainProduct,productnumber as ItemNumber,color as Color,meter as Meter from dbo.master_productnumber where  mainproduct='" + getproduct.SelectedItem.ToString() + "'", cn);

SqlDataReader dr = cd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
dr.Close();


http://www.invomtech.com/

....sundarsowmia....

gurramsra1-10894, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: gurramsra1-10894 on: 1/29/2012 [Member] Starter | Points: 25

Up
0
Down
I Cant Understand. Please tell me in details.
I want to add all the tables names to the dropdownlist dynamically in PageLoad Event.
The gridview should display the data depends on selected table in DropDownList.

Thank You

Sra1

gurramsra1-10894, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Blessyjees on: 1/30/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi,

Please try this

 public partial class _Default : System.Web.UI.Page

{
SqlConnection con = new SqlConnection("data source=SEZ-WS-149\\MSSQLSERVER2K8;initial catalog=port80upload;user id=port80web;password=r5a8m!!!");
SqlDataAdapter da;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{

con.Open();
string s = "select * from priority";
da = new SqlDataAdapter(s, con);
ds = new DataSet();
da.Fill(ds, "priority");
string s1 = "select * from status";
da = new SqlDataAdapter(s1, con);
da.Fill(ds, "status");
if (!IsPostBack)
{
ddlTable.Items.Add("priority");
ddlTable.Items.Add("status");
}
}

protected void ddlTable_SelectedIndexChanged(object sender, EventArgs e)
{
grdData.DataSource=ds;
grdData.DataMember=ddlTable.SelectedItem.Text;
grdData.DataBind();
}
}


Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

gurramsra1-10894, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: gurramsra1-10894 on: 1/30/2012 [Member] Starter | Points: 25

Up
0
Down
Sorry. It is also not displaying the table data in gridview.



Thanks & Regards
SRA1

Sra1

gurramsra1-10894, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response