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;
public partial class Example : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DDLDataSource();
}
}
private void DDLDataSource()
{
string connString = "data source=SOFT14-PC;Initial Catalog=selvi;User ID=sa;Password=sa";
using (SqlConnection conn = new SqlConnection(connString))
{
string queryString = "SELECT * FROM Products";
SqlCommand comm = new SqlCommand(queryString, conn);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "ProductName";
DropDownList1.DataValueField = "ProductID";
DropDownList1.DataBind();
}
}
private void FetchProductData()
{
string connString = "data source=SOFT14-PC;Initial Catalog=selvi;User ID=sa;Password=sa";
using (SqlConnection conn = new SqlConnection(connString))
{
string queryString = "SELECT * FROM Products WHERE ProductID=@ProductID";
SqlCommand comm = new SqlCommand(queryString, conn);
comm.Parameters.AddWithValue("ProductID", DropDownList1.SelectedItem.Value);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt != null && dt.Rows.Count > 0)
{
TextBox1.Text = dt.Rows[0]["ProductID"].ToString();
TextBox2.Text = dt.Rows[0]["ProductName"].ToString();
TextBox3.Text = dt.Rows[0]["ProductCategoryName"].ToString();
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex != -1)
{
FetchProductData();
}
}
}
Output
s g b
corect my codings wer i hav done mistake
Loading


SenthilkumarRank #387 · 6.5K reputationPosted Apr 19, 2012, 3:21 AM EDT
Hope this will help you.
SenthilkumarRank #387 · 6.5K reputationPosted Apr 19, 2012, 12:25 AM EDT
I debugged your code with the same database and table name. It is working fine and there are no errors.
I suggest you to check the few things.
Check1:
Make sure than you have enabled the auto post back property.
Check2:
Make sure that you have used the try catch block in your code
Always its better to put the break point and run your application, where it breaks.
Hope this will help you.
selvi subramanianRank #2,007 · 723 reputationPosted Apr 19, 2012, 2:58 AM EDT
selvi subramanianRank #2,007 · 723 reputationPosted Apr 19, 2012, 3:07 AM EDT
selvi subramanianRank #2,007 · 723 reputationPosted Apr 19, 2012, 3:23 AM EDT