如何使用所选属性来选择带有下拉列表的“插入”查询的参数

时间:2016-06-21 00:42:32

标签: asp.net vb.net visual-studio-2013

我正在使用网格,下拉框和按钮将订单信息插入sqlserver上的订单表中(在visual studio中 - 所有表都有除订单表之外的记录) - 我不确定如何合并所有三个为了更新表格。当登录用户点击他们选择的项目时 - 选择一个药房并单击确认按钮,这应该更新。

网格:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand"  >
<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton runat="server" Text="Select" CommandName="UpdateMedicine" CommandArgument='<%# Eval("MedicineId") %>' />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="Name" HeaderText="Name" />
    <asp:BoundField DataField="Purpose" HeaderText="Purpose" />
    <asp:BoundField DataField="Instrcutions" HeaderText="Instructions" />
</Columns>
</asp:GridView>

网格代码:

Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
    If e.CommandName = "UpdateMedicine" Then


        Dim MedicineID As Integer = Integer.Parse(e.CommandArgument.ToString())

    End If

End Sub

下拉 - 按钮.click

<asp:DropDownList ID="DropPharm" runat="server" DataSourceID="SqlPharm" DataTextField="Pharmname" DataValueField="Pharmname"></asp:DropDownList>

   <br />
   <br />


  <asp:Button ID="btnconfirm" runat="server" Text="Confirm" />

按钮代码:

Protected Sub btnconfirm_Click(sender As Object, e As EventArgs) Handles btnconfirm.Click

    Dim strPatientId As Integer = Session("PatientId").ToString
    Dim strMedicineId As Integer
    Dim strDoctorId As Integer
    Dim strPharmacyId As Integer
    Dim strDateOrdered As Date



    Dim query As String = String.Empty
    query &= "INSERT INTO Order_pres (PatientId, MedicineId, PharmacyId, "
    query &= "                     DoctorId, [Date Ordered])  "
    query &= "VALUES (@PatientId,@MedicineId, @PharmacyId, @DoctorId, @DateOrdered)"

    Using conn As New SqlConnection("SurgeryConnectionString"), _
          comm As New SqlCommand(query, conn)
        With comm.Parameters

            .Add("@PatientId", SqlDbType.Int).Value = CInt(strPatientId)
            .Add("@MedicineId", SqlDbType.Int).Value = CInt(strMedicineId)
            .Add("@PharmacyId", SqlDbType.Int).Value = CInt(strPharmacyId)
            .Add("@DoctorId", SqlDbType.Int).Value = CInt(strDoctorId)
            .Add("@DateOrdered", SqlDbType.DateTime).Value = DateTime.Parse(strDateOrdered)
        End With

        Try
            conn.Open()
            comm.ExecuteNonQuery()
            lblconfirm.Text() = "Order Placed"
        Catch ex As SqlException
            lblnoconfirm.Text() = "Order not placed"
        End Try
    End Using

End Sub

如果有人可以给我一些方向或示例我会非常感谢,我已经坚持了很长时间:) - 谢谢你

1 个答案:

答案 0 :(得分:1)

我希望这就是你想要的。

要从下拉列表中获取值或文本,请使用:

a.item_id   str
sku.0   str
sku.1   str

要获取所选行的单元格值,请使用:

someVariable = DropPharm.SelectedItem.Text
someVariable = DropPharm.SelectedItem.Value

使用此选项在btnconfirm_Click子中设置变量。

使someVariable = GridView1.Rows(GridView1.SelectedIndex).Cells(yourColumnIndex).Value 成为一个类级别变量,因此将其声明在所有子类之外但在类内部,以便您可以在另一个子类中访问它的值。然后在你的btn点击子:

MedicineID

然后在查询中执行Dim strMedicineId As Integer = MedicineID ,以便在数据库中已存在以下内容时对其进行更新:

IF EXISTS