I have created two tables Mobile and Laptop in sql server. In Asp.net, I have one DropDownlistBox and DataList Control.In dropdownlistbox i did add mobileCategoris and pcCategories.when i select mobileCategories then data will bind Datalist and display on page. when i select pcCategories then data will bind Datalist and display on page...
Plz Solve my Question..
With Warm Regards
Bvenkat Jeewesh
Junior S/W
Loading

Satyapriya NayakPosted Dec 26, 2011, 10:22 AM
Try this...
Run the attachments.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Bind_two_tables_data_in_DataList._Default" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Bind_two_tables_data_in_DataList
{
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
DataSet ds2;
string str;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
b1();
b2();
}
}
void b1()
{
SqlConnection con = new SqlConnection(connStr);
DropDownList1.Items.Add("Choose");
con.Open();
str = "select * from Mobile";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
DropDownList1.Items.Add(reader["mname"].ToString());
}
reader.Close();
con.Close();
}
void b2()
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "select * from Laptop";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
DropDownList1.Items.Add(reader["lname"].ToString());
}
reader.Close();
con.Close();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
d1();
d2();
}
void d1()
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "select * from Mobile where mname='" + DropDownList1.SelectedItem.Text + "'";
com = new SqlCommand(str, con);
SqlDataReader reader;
reader = com.ExecuteReader();
DataList1.DataSource = reader;
DataList1.DataBind();
reader.Close();
con.Close();
}
void d2()
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "select * from Laptop where lname='" + DropDownList1.SelectedItem.Text + "'";
com = new SqlCommand(str, con);
SqlDataReader reader;
reader = com.ExecuteReader();
DataList2.DataSource = reader;
DataList2.DataBind();
reader.Close();
con.Close();
}
}
}
Thanks
If this post helps you mark it as answer
Satyapriya NayakPosted Dec 26, 2011, 12:00 PM
If this post helps you mark it as answer
Thanks
Bvenkat JeeweshPosted Dec 26, 2011, 11:50 AM