Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 显示SQL值的DropDownList_Asp.net_Sql - Fatal编程技术网

Asp.net 显示SQL值的DropDownList

Asp.net 显示SQL值的DropDownList,asp.net,sql,Asp.net,Sql,我用SQL语句中的值填充了DropDownList,它可以正常工作 Try cmdStr = "SELECT [idt],[col1] FROM [test];" Using conn As New SqlConnection(connStr) Using cmd As New SqlCommand(cmdStr, conn) conn.Open() cmd.Execut

我用SQL语句中的值填充了DropDownList,它可以正常工作

   Try
        cmdStr = "SELECT [idt],[col1] FROM [test];"
        Using conn As New SqlConnection(connStr)
            Using cmd As New SqlCommand(cmdStr, conn)
                conn.Open()
                cmd.ExecuteNonQuery()
                Using da As New SqlDataAdapter(cmd)
                    da.Fill(ds)
                    DropDownList1.DataSource = ds.Tables(0)
                    DropDownList1.DataTextField = "idt"
                    DropDownList1.DataValueField = "col1"
                    DropDownList1.DataBind()
                End Using
                cmd.Dispose()
                conn.Close()
                conn.Dispose()
            End Using
        End Using
    Catch ex As Exception
        TextBox1.Text = ex.Message
    End Try
问题是SQL列表以idt=68开始,它在DropDownList中显示,但DropDownList 1.DataTextField=“”,而不是下面的68

     Try
        cmdStr = "SELECT [datetime],[col1],[col2],[col3] FROM [test] WHERE [idt]=@idt;"
        Using conn As New SqlConnection(connStr)
            Using cmd As New SqlCommand(cmdStr, conn)
                conn.Open()
                cmd.Parameters.AddWithValue("@idt", DropDownList1.DataTextField)
                cmd.ExecuteNonQuery()
      ...

将其更新为使用DropDownList1.SelectedItem.Text

Try
        cmdStr = "SELECT [datetime],[col1],[col2],[col3] FROM [test] WHERE [idt]=@idt;"
        Using conn As New SqlConnection(connStr)
            Using cmd As New SqlCommand(cmdStr, conn)
                conn.Open()
                cmd.Parameters.AddWithValue("@idt", DropDownList1.SelectedItem.Text)
                cmd.ExecuteNonQuery()
您不需要选择“col1”列。因为你不用它

然后

使用SelectedValue属性

 cmd.Parameters.AddWithValue("@idt", DropDownList1.SelectedValue);

检查您的DataValueField、DataTextField

DataValueField=will be id for that field/id whenever your search or do any operation
DataTextField= text for that field/id to show on UI. 
我想这会管用的

      DropDownList1.DataTextField = "col1"
      DropDownList1.DataValueField = "idt"
      DropDownList1.DataTextField = "col1"
      DropDownList1.DataValueField = "idt"