从数据库中搜索记录并在文本框和下拉列表中显示值 [英] search record from database and display value in textbox and drop downlist

查看:67
本文介绍了从数据库中搜索记录并在文本框和下拉列表中显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨每一个;

这里是搜索按钮的代码。当我搜索学生的记录时,值会显示在文本字段中,但不会显示在下拉列表中。是什么原因?我是asp.net的初学者。

请尽快指导我..

提前感谢..

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..

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...!";
              }

推荐答案

你有两个选择,你要保留你做的但不是分配文本通过循环下拉项来分配所选索引。
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.
foreach(ListItem ltItem in DropDownList1.Items)
{
 if(ltItem.Value == yourValue)
   DropDownList1.SelectedIndex = ltItem.Index;
}



-OR-



使用<$ c的数据源绑定您的下拉列表$ c> txtID.Text 作为参数。请参阅链接中的示例:

Link1 [ ^ ]

Link2 [ ^ ]



祝你好运,

OI


-OR-

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

Good luck,
OI


重写你的代码

drpclassno.Text = reader。 GetValue(9).ToString();



as



rewrite ur code
drpclassno.Text = reader.GetValue(9).ToString();

as

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







drpgender.Text = reader.GetValue(10).ToString();

as

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





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

as



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





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

as



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





drpyear.Text = reader.GetValue(13).ToString();

as

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







确保你的al l dropdown之前有一些文本绑定到它们,以便你可以通过文本属性下拉项目。





快乐编码
:)


首先你需要将dropdownlist与数据库记录绑定

样本

First you need to bind dropdownlist with database records
Sample
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

DropDownList1.Items.FindByText(reader.GetValue(9).ToString());



如果这有助于你,请告诉我


Please let me know if this helps you


这篇关于从数据库中搜索记录并在文本框和下拉列表中显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆