Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to read data based on selected id but it reads only 1st id data and when i select 2nd id from dropdownlist it select 1st id data. where is the problem to read data from 2nd id

my code is
C#
protected void Page_Load(object sender, EventArgs e)
{
    //SqlConnection con = new SqlConnection(str);
    SqlConnection con = new SqlConnection(ConfigurationManager .ConnectionStrings["cn"].ConnectionString);  
    
    string com = "select * from abouts";
    SqlDataAdapter adpt = new SqlDataAdapter(com, con);
    DataTable dt = new DataTable();
    adpt.Fill(dt);
    DropDownList1.DataSource = dt;
    DropDownList1.DataBind();
    DropDownList1.DataTextField = "title";
    DropDownList1.DataValueField = "id";
    DropDownList1.DataBind();
}

C#
protected void Button1_Click1(object sender, EventArgs e)
{
   // SqlConnection con = new SqlConnection(str);
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);  
    SqlCommand cmd = new SqlCommand("select * from abouts where id = '" + DropDownList1.SelectedValue + "'", con);
    SqlDataAdapter Adpt = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    Adpt.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
    Label1.Text = "record found";
}

tell me where is the problem to read data based on 2nd id.
Posted
Updated 24-Jul-12 0:02am
v2
Comments
dimpledevani 24-Jul-12 6:00am    
You hav not used isposback method so, your dropdownlist is reloding and bydefalut first item is selected

When you click the Button1, at first page load event fires, so your dropdown list will get reset by fetching its values from the database.

Try to put a checking, whether its postback or not.


C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
            {
//SqlConnection con = new SqlConnection(str);
SqlConnection con = new SqlConnection(ConfigurationManager .ConnectionStrings["cn"].ConnectionString);
 
string com = "select * from abouts";
SqlDataAdapter adpt = new SqlDataAdapter(com, con);
DataTable dt = new DataTable();
adpt.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
DropDownList1.DataTextField = "title";
DropDownList1.DataValueField = "id";
DropDownList1.DataBind();
}
}
 
Share this answer
 
Hi,
Try the following:
C#
protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack){
       fnBindDDL();
    }
}
private void fnBindDDL()
{
    //SqlConnection con = new SqlConnection(str);
    SqlConnection con = new SqlConnection(ConfigurationManager .ConnectionStrings["cn"].ConnectionString);  
    
    string com = "select * from abouts";
    SqlDataAdapter adpt = new SqlDataAdapter(com, con);
    DataTable dt = new DataTable();
    adpt.Fill(dt);
    DropDownList1.DataSource = dt;
    DropDownList1.DataBind();
    DropDownList1.DataTextField = "title";
    DropDownList1.DataValueField = "id";
    DropDownList1.DataBind();
}
protected void Button1_Click1(object sender, EventArgs e)
{
   // SqlConnection con = new SqlConnection(str);
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);  
    SqlCommand cmd = new SqlCommand("select * from abouts where id = '" + DropDownList1.SelectedValue + "'", con);
    SqlDataAdapter Adpt = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    Adpt.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
    Label1.Text = "record found";
}


---Amit
 
Share this answer
 
Corrected code

C#
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostback)//I ADDED THIS LINE
{
//SqlConnection con = new SqlConnection(str);
SqlConnection con = new SqlConnection(ConfigurationManager .ConnectionStrings["cn"].ConnectionString); 
 
string com = "select * from abouts";
SqlDataAdapter adpt = new SqlDataAdapter(com, con);
DataTable dt = new DataTable();
adpt.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
DropDownList1.DataTextField = "title";
DropDownList1.DataValueField = "id";
DropDownList1.DataBind();
}
}
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900