Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i save bharath 9600637754
       vinoth  9600637754
if i select 9600637754 in dropdownlist only last save only display in textfield,
i want to all name,


What I have tried:

<pre>  protected void BindContrydropdown()
        {
            //conenction path for database
            using (SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=firstproject;Integrated Security=True"))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("Select ID,MOBILE FROM APPLICATION", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                DropDownList6.DataSource = ds;
                DropDownList6.DataTextField = "MOBILE";
                DropDownList6.DataValueField = "ID";
                DropDownList6.DataBind();
                DropDownList6.Items.Insert(0, new ListItem("--Select--", "0"));
                con.Close();
            }
        }



protected void DropDownList6_SelectedIndexChanged(object sender, EventArgs e)
       {
           String strQuery = "select * from APPLICATION where MOBILE = @MOBILE";
           SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=firstproject;Integrated Security=True");
           SqlCommand cmd = new SqlCommand(strQuery);
           cmd.Parameters.AddWithValue("@MOBILE", DropDownList6.SelectedItem.Text);
           cmd.CommandType = CommandType.Text;
           cmd.CommandText = strQuery;
           cmd.Connection = con;
           try
           {
               con.Open();
               SqlDataReader sdr = cmd.ExecuteReader();
               while (sdr.Read())
               {
                   //txtCity.Text = sdr[0].ToString();
                   TextBox1.Text = sdr["DOB"].ToString();
                   TextBox2.Text = sdr["DOA"].ToString();
                   TextBox3.Text = sdr["NAME"].ToString();
                   TextBox4.Text = sdr["CITY"].ToString();
                   TextBox5.Text = sdr["AREA_LOCATION"].ToString();
                   TextBox6.Text = sdr["MOBILE"].ToString();
                   TextBox7.Text = sdr["CONTACT_PERSON"].ToString();
                   TextBox8.Text = sdr["STATE"].ToString();
                   TextBox9.Text = sdr["EMAIL"].ToString();
                   TextBox10.Text = sdr["ALTER_EMAIL"].ToString();
                   TextBox11.Text = sdr["LANDLINE"].ToString();
                   TextBox12.Text = sdr["PINCODE"].ToString();
                   TextBox13.Text = sdr["NATIONALITY"].ToString();
               }
           }
           catch (Exception ex)
           {
               throw ex;
           }
           finally
           {
               con.Close();
               con.Dispose();
           }
       }


db

1	2019-08-01	2019-02-01	vinoth	B	Thanjavur	9600637754	rajappa	TN	sowndaryarajappa@gmail.com	meetvinoth87@gmail.com	044-1245678	613104	korean
4	2019-01-02	2019-02-02	bharath	Thanjavur	thirukkattupalli	9600637754	sowndarya	thanjavur	meetvinoth87@gmail.com	vinovenkat.mca@gmail.com	04412345678	613105	inidian
Posted
Updated 28-Jan-19 2:20am
Comments
OriginalGriff 27-Jan-19 3:05am    
And?
What is the problem - you haven't told us what it does that you didn't expect, or doesn't do that you did! And we can't help you if we do not know what the problem is.

Use the "Improve question" widget to edit your question and provide better information.

1 solution

Change your query to load the record by ID, which should be unique, rather than loading by mobile, which is not:
C#
const string strQuery = "select * from APPLICATION where ID = @ID";
...
cmd.Parameters.AddWithValue("@ID", DropDownList6.SelectedItem.Value);



Also:
C#
catch (Exception ex)
{
    throw ex;
}

Don't do that. You've just destroyed the stack trace of the exception, making it much harder to track down the line that caused it. If you must catch and re-throw an exception, use throw; instead of throw ex;, which will preserve the stack trace:
C#
catch (Exception ex)
{
    throw;
}

But in this case, since you're not doing anything with the exception, there's no need to catch it in the first place. Just remove the catch block.


And:
C#
finally
{
    con.Close();
    con.Dispose();
}

Wrap the connection, and any other objects which implement IDisposable, in a using (...) { ... } block. That way, they will automatically be disposed when the block ends, and you won't need to worry about the try..finally block at all.
C#
using (SqlConnection con = new SqlConnection("..."))
using (SqlCommand cmd = new SqlCommand(strQuery, con))
{
    cmd.Parameters.AddWithValue("@ID", DropDownList6.SelectedItem.Value);
    
    con.Open();
    
    using (SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
    {
        if (sdr.Read())
        {
            ...
        }
    }
}

using statement - C# Reference | Microsoft Docs[^]
 
Share this answer
 

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



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