Asp.net 我的代码中有Bug或者我做得不对

Asp.net 我的代码中有Bug或者我做得不对,asp.net,webforms,Asp.net,Webforms,我已经为这个用例苦苦挣扎了两天。我所需要的只是从患者就诊表中读取患者的就诊编号,并根据患者编号填充dropdownlist。经过大量研究,这两个链接解决了几乎相同的问题。我按照这里描述的做了,但我的链接仍然不起作用,并且没有显示错误! 这是我的代码:HTML <li> <asp:Label runat="server" ID ="lblVisits">Visit Number</asp:Label> <

我已经为这个用例苦苦挣扎了两天。我所需要的只是从患者就诊表中读取患者的就诊编号,并根据患者编号填充dropdownlist。经过大量研究,这两个链接解决了几乎相同的问题。我按照这里描述的做了,但我的链接仍然不起作用,并且没有显示错误! 这是我的代码:HTML

  <li>
            <asp:Label runat="server" ID ="lblVisits">Visit Number</asp:Label>
            <asp:DropDownList ID="DropDownList2" runat="server" Height="16px" Width="174px" style="margin-left: 46px">
                <asp:ListItem Text=""></asp:ListItem>
                <asp:ListItem Value=""></asp:ListItem>
            </asp:DropDownList>
  </li>    
  • 访问号码
  • 代码隐藏:

    protected void btn_search_Click(object sender, EventArgs e)
        {
            string connect = System.Configuration.ConfigurationManager.ConnectionStrings["db_connection"].ToString();
            string num = txtPatientNumber.ToString();
            SqlConnection con = new SqlConnection(connect);
            string Statement = "SELECT Visit_Number FROM Visit "
              + "WHERE Patient_Number=@Patient_Number";
            SqlCommand cmd = new SqlCommand(Statement, con);
            cmd.Parameters.AddWithValue("@Patient_Number", num);
    
            SqlDataReader reader;
    
            try
            {
                con.Open();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    DropDownList2.DataSource = reader;
                    DropDownList2.DataTextField = reader["Visit_Number"].ToString();
                    DropDownList2.DataValueField = reader["Visit_Number"].ToString();
                    DropDownList2.DataBind();
                }
                reader.Close();
            }
            catch (SqlException excep)
            {
                //Response.Write("<script language=javascript>alert('Patient Number not Found.');</script>");
                ErrorMessage.Text = "Error Occurred" + excep.Message.ToString();
            }
            finally
            {
                con.Close();
            }
    
        }
    
    protectedvoidbtn\u搜索\u单击(对象发送者,事件参数e)
    {
    string connect=System.Configuration.ConfigurationManager.ConnectionStrings[“db_connection”].ToString();
    字符串num=txtPatientNumber.ToString();
    SqlConnection con=新的SqlConnection(connect);
    string语句=“从就诊中选择就诊号码”
    +“其中Patient_Number=@Patient_Number”;
    SqlCommand cmd=新的SqlCommand(语句,con);
    cmd.Parameters.AddWithValue(“@Patient_Number”,num);
    SqlDataReader;
    尝试
    {
    con.Open();
    reader=cmd.ExecuteReader();
    while(reader.Read())
    {
    DropDownList2.DataSource=读取器;
    DropDownList2.DataTextField=读卡器[“访问号”].ToString();
    DropDownList2.DataValueField=读卡器[“访问号”].ToString();
    DropDownList2.DataBind();
    }
    reader.Close();
    }
    捕获(SqlException例外)
    {
    //回答。写(“警报(‘未找到患者编号’);”;
    ErrorMessage.Text=“出现错误”+例外消息.ToString();
    }
    最后
    {
    con.Close();
    }
    }
    
    问题在于:

    while (reader.Read())
    {
       DropDownList2.DataSource = reader;
       DropDownList2.DataTextField = reader["Visit_Number"].ToString();
       DropDownList2.DataValueField = reader["Visit_Number"].ToString();
       DropDownList2.DataBind();
    }
    
    您使用的
    .DataTextField
    .DataValueField
    错误。它应该是这样的:

    DropDownList2.DataTextField = "Visit_Number";
    DropDownList2.DataValueField ="Visit_Number";
    
    另外,您不应该为从db返回的每一行重新绑定DropDownList。因此,在(reader.Read())时删除

    最终代码:

    DropDownList2.DataSource = reader;
    DropDownList2.DataTextField = "Visit_Number";
    DropDownList2.DataValueField = "Visit_Number";
    DropDownList2.DataBind();
    

    所有两个示例URL都指向example.com上的假页面。您可能需要修复该问题。而不是DropDownList2.DataTextField=reader[“Visit_Number”]。ToString();使用DropDownList2.DataTextField=“访问号码”;我这样做了,同样的问题。问题仍然存在@gzaxx。您的示例也适用于我,但肯定不适用于我,我的sql经过测试,运行良好。这是一个很长的目标,但
    txtPatientNumber
    是TextBoxControl吗?如果是,则此行
    txtPatientNumber.ToString()
    错误,应为
    txtPatientNumber.Text。如果没有,您能否尝试从HTML中删除
    DropDownList
    内部的这两个
    ,看看它现在是否工作?您是对的,我尝试过调试,但while循环根本没有执行。它没有执行,因为读卡器没有返回任何值。所以这个SQL语句是错误的,或者您向它传递了错误的值。