使用SQL数据填充Dropbox

时间:2012-09-20 18:28:03

标签: asp.net vb.net visual-studio-2010 sql-server-2008

我还有一些关于我最新项目的问题。我觉得在过去的几天里我已经取得了一些相当不错的进步,但我仍然在努力研究SQL库的一些核心概念,即从特定列读取并删除整行。

在过去的一周里,我能够构建一个webform,将excel文件保存到服务器,打开这些文件并将数据导出到特定的SQL表中,并根据用户通过下拉列表选择的内容将数据绑定到特定的数据网格。

我想要实现的是:根据用户从第一次下拉菜单中选择的内容,动态填充另一个下拉列表。更具体地说,我有4个表,在每个表的第一列中我有序列号,如果用户在第一个下拉列表中选择Table2,我希望第二个下拉列表显示Table2的column1中的所有序列号。然后,如果用户从第二个淹没中选择一个特定的序列号,它将使用该相关行的第1-5列填充数据网格。

第二部分是创建一个删除按钮,用户可以在datagrid中显示信息后推送该按钮,删除该表中序列号条目的整行。

这是我从其他例子中与弗兰肯斯坦一起设法的:

 Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As System.EventArgs)
    DropDownList2.Enabled = True 'its remains disabled until the user selects something from the first box
    Using con As New SqlClient.SqlConnection
        con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=" & AppPath & "App_Data\DeviceDatabase.MDF;Integrated Security=True;User Instance=True;"
        Using cmd As New SqlClient.SqlCommand
            cmd.Connection = con
        End Using
        Dim cmdSQL As New SqlCommand()
        cmdSQL.CommandType = Data.CommandType.Text
        cmdSQL.CommandText = "SELECT Fieldname1 FROM " & """" & DropDownList1.SelectedItem.ToString & """" 'Im pretty sure this isnt right, and the reason I use """"" is because some of the items in the dropdown have spaced words.

        Dim adptSQL As New SqlClient.SqlDataAdapter(cmdSQL)
        Dim myDataSet As New DataSet()
        adptSQL.Fill(myDataSet)

        With myDataSet.Tables(DropDownList1.SelectedIndex) 'I think this is right
            For rowNumber As Integer = 0 To .Rows.Count - 1
                With .Rows(rowNumber)
                    DropDownList2.Items.Add(col1.rowNumber) 'This is obviously not working 
                End With
            Next
        End With
    End Using
End Sub

然后,我不太确定如何使用所选行填充数据表,尽管目前我能够使用以下内容来完成整个表:

Private Sub GenTables(ByVal DropList As Object)
    If DropList.SelectedIndex = 0 Then
        GridView1.DataSourceID = Nothing
    ElseIf DropList.SelectedIndex = 1 Then
        GridView1.DataSourceID = "SqlDataSource1"
    ElseIf DropList.SelectedIndex = 2 Then
        GridView1.DataSourceID = "SqlDataSource2"
    ElseIf DropList.SelectedIndex = 3 Then
        GridView1.DataSourceID = "SqlDataSource3"
    ElseIf DropList.SelectedIndex = 4 Then
        GridView1.DataSourceID = "SqlDataSource4"
    End If
    GridView1.DataBind()
End Sub

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DeviceDatabaseConnectionString1 %>" 
        ProviderName="<%$ ConnectionStrings:DeviceDatabaseConnectionString1.ProviderName %>" 
        SelectCommand="SELECT [Device:] AS column1, [SWversion:] AS column2, [Date:] AS column3, [Tester:] AS column4, [Wifi Preferred InCov:] AS column5 FROM [Galaxy Nexus]">
    </asp:SqlDataSource>

'there are 3 more of these.

但我将这些表“硬编码”到应用程序中,显然我不能对每一个表行执行此操作。那么如何在不在asp中提前设置SQLDataSource的情况下填充数据网格呢?

最后删除与单击按钮时数据网格中显示的信息相关的行。如果能在第一部分得到一点帮助,我相信我能找到第二部分。

所以我要问的是:如何使用Coloumn1中的所有项目填充下拉列表?以及如何从特定行填充数据网格?

总是非常感谢任何和所有帮助。谢谢大家

扎克

修改

嗯,我想我现在要把它变得更难了,现在我正在努力:

      Protected Sub BindDrop_Click(sender As Object, e As System.EventArgs)
        DropDownList2.DataSourceID = "SqlDataSource5"
        DropDownList2.DataBind()      
      End Sub

<asp:SqlDataSource ID="SqlDataSource5" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DeviceDatabaseConnectionString1 %>" 
        ProviderName="<%$ ConnectionStrings:DeviceDatabaseConnectionString1.ProviderName %>" 
        SelectCommand="SELECT [Device:] AS column1 FROM [Galaxy Nexus]">

它不太正确,但它更接近并且在1/10行

1 个答案:

答案 0 :(得分:0)

好吧我们想通了,我需要使用ExecuteReader函数(我疯狂地找不到使用此方法的自动填充中的一篇文章)。希望在写作/回答这个问题时,我会让某人的生活更轻松。

 Protected Sub DropDownList2_SelectedIndexChanged(sender As Object, e As System.EventArgs)
    DropDownList3.Enabled = True
    DropDownList3.Items.Clear()
    Dim newsqlcommand As String = "Select [SWversion:] FROM " & """" & DropDownList2.SelectedItem.ToString & """"       
    Using con As New System.Data.SqlClient.SqlConnection(connexstring)
        con.Open()
        Using cmd As New SqlCommand(newsqlcommand, con)
            Dim myReader As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            While myReader.Read()            
                DropDownList3.Items.Add(myReader.GetString(0))
            End While
            myReader.Close()
            cmd.Dispose()
        End Using
        con.Close()
        con.Dispose()
    End Using
    Dbind()    
End Sub   

这会成功读取“SWVersion”列中的所有项目,并将它们添加到dropdown3的下拉列表中。享受!