Click here to Skip to main content
15,895,370 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi every one ;
here is code of search button. when i search the record of student then value are displayed in text fields but not in dropdownnlist. what is the reason? i am beginner in asp.net.
please guide me as soon as possible..
thanks in advance..
C#
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Final Project\FinalProject\FinalProject\App_Data\Record.mdf;Integrated Security=True;User Instance=True");
           try
           {
               string query = "select * from student where StdId='" + txtID.Text + "'";
               SqlCommand com = new SqlCommand(query, con);
               SqlDataReader reader = null;

               con.Open();
               reader = com.ExecuteReader();
              if (reader.Read())
               {
                 txtfirstname.Text = reader.GetValue(3).ToString();
                 txtlastname.Text = reader.GetValue(4).ToString();
                 txtfaname.Text = reader.GetValue(5).ToString();
                 txtfcellno.Text = reader.GetValue(6).ToString();
                 txtfnic.Text = reader.GetValue(7).ToString();
                 txthome.Text = reader.GetValue(8).ToString();

                 drpclassno.Text = reader.GetValue(9).ToString();
                 drpgender.Text = reader.GetValue(10).ToString();
                 drpday.Text = reader.GetValue(11).ToString();
                 drpmonth.Text = reader.GetValue(12).ToString();
                 drpyear.Text = reader.GetValue(13).ToString();
               }
              else
              {
                  lblmsg.Text = "Record was not Found...!";
              }
Posted
Updated 30-Jan-20 9:13am
v2

Well you have two options either you keep what you do but instead of assigning the text assign the selected index by looping through the dropdown items.
C#
foreach(ListItem ltItem in DropDownList1.Items)
{
 if(ltItem.Value == yourValue)
   DropDownList1.SelectedIndex = ltItem.Index;
}

-OR-

Bind your dropdown with a datasource which takes txtID.Text as a parameter. See examples from the links:
Link1[^]
Link2[^]

Good luck,
OI
 
Share this answer
 
rewrite ur code
drpclassno.Text = reader.GetValue(9).ToString();

as

SQL
drpclassno.SelectedIndex = drpclassno.Items.IndexOf(drpclassno.Items.FindByText(reader.GetValue(9).ToString()));


and
drpgender.Text = reader.GetValue(10).ToString();
as
drpgender.SelectedIndex = drpgender.Items.IndexOf(drpgender.Items.FindByText(reader.GetValue(10).ToString()));

and
drpday.Text = reader.GetValue(11).ToString();
as

drpday.SelectedIndex = drpday.Items.IndexOf(drpday.Items.FindByText(reader.GetValue(11).ToString()));

and
drpmonth.Text = reader.GetValue(12).ToString();
as

drpmonth.SelectedIndex = drpmonth.Items.IndexOf(drpmonth.Items.FindByText(reader.GetValue(12).ToString()));

and
drpyear.Text = reader.GetValue(13).ToString();
as
drpyear.SelectedIndex = drpyear.Items.IndexOf(drpyear.Items.FindByText(reader.GetValue(13).ToString()));



Make sure that your all dropdown have some text binded to them earlier so that you can items in dropdown by their text property.


happy coding
:)
 
Share this answer
 
First you need to bind dropdownlist with database records
Sample
C#
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Final Project\FinalProject\FinalProject\App_Data\Record.mdf;Integrated Security=True;User Instance=True");
            try
            {
            string query = "select Value1,Value2 from Table";
            SqlDataAdapter adp= new SqlDataAdapter (query, con);
           con.Open();
           DataSet dt=new DataSet();
           adp.Fill(ds);
           drpclassno.DataSource=ds;
           drpclassno.DataTextField="Value1"
           drpclassno.DataValueField="Value2"
           drpclassno.DataBind();

After binding to select record from database you need to write this
C#
DropDownList1.Items.FindByText(reader.GetValue(9).ToString());

Please let me know if this helps you
 
Share this answer
 
v2

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