简体   繁体   English

ASP.NET C#-无法从SQL数据库填充(预选择)下拉列表SelectedValue

[英]asp.net C# - unable to get populate (preselect) the dropdownlist SelectedValue from sql database

I am just beginning to learn programming. 我才刚刚开始学习编程。 I'm running into an error that I cannot figure out. 我遇到了无法解决的错误。 My code-behind is below. 我的后台代码如下。 I am receiving Error CS1001 (Identifier expected). 我收到错误CS1001(应提供标识符)。 I need to pull the value for the searched record and pre-select it on the dropdownlist: 我需要提取搜索记录的值并在下拉列表中预先选择它:

protected void DetailsView1_DataBound(object sender, EventArgs e)
{
    DataTable dvt = new DataTable();
    using (SqlConnection sqlconn = new SqlConnection(mainconn))
    {

        sqlconn.Open();
        SqlCommand sqlcomm = new SqlCommand();
        string sqlquery = "SELECT distinct [EncounterID], pd.[MRN], [VisitDate], FirstName, LastName, DOB, Gender,[BMI_Category], [Asthma_IDDate], [Tobacco_Counsel], [EC_ImmediateType], [BCM_ImplantDisp], [BCM_CondomDisp], [BCM_IUDDisp], [BCM_DepoDisp], [BCM_NuvaRingDisp], [BCM_PatchDisp], [Preg_TestToday], [Preg_TestResults], [EC_AdvanceType], [EC_Immediate], [EC_Advance], [BCM_FemaleCondomDisp], [BCM_LatexDamDisp], [NoBCM_PartnerMethod], [NoBCM_SeekingPreg], [NoBCM_Abst], [NoBCM_Preg], [NoBCM_SameSex], [NoBCM_Undecided], [NoBCM_Refused], [NoBCM_LarcAppt], [NoBCM_Other], [NoBCM_OtherReason], [NoBCM_AlreadyUsing], [BCMAlready_Patch], [BCMAlready_Depo], [BCMAlready_Condom], [BCMAlready_Implant], [BCMAlready_IUD], [BCMAlready_FemaleCondom], [BCMAlready_ECOnly] FROM [OSCRMedicalData] md INNER JOIN OSCRPersonData pd on md.MRN=pd.MRN WHERE ([EncounterID] = @EncounterID)";
        sqlcomm.CommandText = sqlquery;
        sqlcomm.Connection = sqlconn;
        sqlcomm.Parameters.AddWithValue("@EncounterId", Txtsearch.Text);
        SqlDataReader sdr = sqlcomm.ExecuteReader();



        DropDownList ddl = (DropDownList)DetailsView1.FindControl("Preg_TestToday");
        if (ddl != null)
        {
            ddl.DataSource = sdr;
            ddl.SelectedValue = sdr.["Preg_TestToday"].ToString();
            ddl.DataBind();

           ddl.SelectedValue = sdr.["Preg_TestToday"].ToString();

        }

    }    



}

You need to remove the . 您需要删除。 In both lines To be sdr["Preg_TestToday"]. 在两行中均为sdr [“ Preg_TestToday”]。 Plus you cant directly bind with DataReader you need to do as below : 另外,您不能直接与DataReader绑定,您需要执行以下操作:

DataTable dt = new DataTable();
dt.Load(sdr);

You need to convert to datatable to read the data using indexers or use first 您需要转换为数据表以使用索引器读取数据或先使用

If(sdr.Read())
{
    var selectedValue = sdr["YourColumnName"];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将数据库数据显示为SelectedValue asp.net C# - Unable to show database data as SelectedValue asp.net c# C#-ASP.NET-GridView中的DropDownList-SelectedValue错误 - C# - ASP.NET - DropDownList in GridView - SelectedValue error 如何使用/不使用ASP.net C#中的CodeBehind将DropDownList中的SelectedValue作为查询字符串传递给LinkBut​​ton - How to pass the SelectedValue from a DropDownList as a query string in a LinkButton with/without using CodeBehind in ASP.net C# 将数据从ASP.NET/C#中的SQL带入DropDownList? - Bring data into DropDownList from SQL in ASP.NET / C#? 在ASP.NET C#的dropdownlist值上填充多个文本框 - Populate multiple textboxes on dropdownlist value in asp.net c# 在ASP.NET C#上使用SQL填充基于DropDownList选择的文本框 - Textbox to populate base on DropDownList selection using sql on ASP.NET C# 尝试从单选按钮列表ASP.NET Web表单/ C#获取SelectedValue时出错 - Errors when trying to get SelectedValue from radio button list ASP.NET web form/C# 从数据库SQL Server 2012 asp.net C#填充Datalist ItemTemplate内的CheckBoxList - Populate CheckBoxList inside Datalist ItemTemplate from database SQL Server 2012 asp.net C# 从ASP.NET MVC中的数据库以表格形式填充下拉列表 - Populate dropdownlist from database in asp.net MVC in a form ASP.NET MVC 3-Javascript中断了DropDownList的SelectedValue - ASP.NET MVC 3 - Javascript breaks SelectedValue from DropDownList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM