selvi subramanian

selvi subramanian

  • NA
  • 799
  • 574.8k

Dropdown list does not get the value to the text box

Apr 19 2012 2:44 AM
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










Answers (5)