如何将数据库中的值存储为要在insert语句中使用的变量

时间:2016-06-29 09:57:53

标签: mysql asp.net vb.net

使用下拉框和按钮我试图将PharmacyId插入订单表但是我不知道如何将pharmacyid存储为变量来读取它,就像我得到的其他值一样。我将其余的存储为会话,如下:

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

        ' Dropdown for pharmacy  someVariable = DropPharm.SelectedItem.Value  


        Dim PatientId As Integer = Session("PatientId")
        Dim DoctorId As Integer = Session("DoctorId")
        Dim PharmacyId As Integer ' pharmacy must be stored(DropPharm.SelectedValue  must be the selected value)
        Dim MedicineId As Integer = Session("MedicineId")
        Dim DateOrdered As Date


         ' How to get pharmacy id stored

               'select pharmacy_id from Pharmacy where Pharmname = '';  ??


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



        Dim sqlCs As String = ConfigurationManager.ConnectionStrings("SurgeryConnectionString").ConnectionString

        Using conn As New SqlConnection(sqlCs),
              comm As New SqlCommand(query, conn)
            With comm.Parameters



                .Add("@PatientId", SqlDbType.Int).Value = Session("PatientId")
                .Add("@DoctorId", SqlDbType.Int).Value = Session("DoctorId")
                .Add("@MedicineId", SqlDbType.Int).Value = Session("MedicineId")
                .Add("@PharmacyId", SqlDbType.Int).Value = Session("PharmacyId")
                .Add("@DateOrdered", SqlDbType.DateTime).Value = DateTime.Parse(DateOrdered)



            End With


            Try
                conn.Open()
                Dim rowInserted = comm.ExecuteNonQuery()
                If rowInserted = 1 Then
                    lblconfirm.Text = "Order Placed"
                Else
                    lblnoconfirm.Text = "Order not placed"
                End If
            Catch ex As SqlException
                lblnoconfirm.Text = "Unexpectd error: " & ex.Message
            End Try

        End Using

pharmacyId出现在.aspx页面上,带有此下拉列表和sql数据源:

<asp:SqlDataSource ID="SqlPharm" runat="server" ConnectionString="<%$ ConnectionStrings:SurgeryConnectionString %>" SelectCommand="SELECT DISTINCT PharmacyId, Pharmname FROM Pharmacy "></asp:SqlDataSource>



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

当从下拉列表中选择一个值并单击该按钮时,我现在将它显示在标签上,我只是想将其保存为变量,以便可以将其读入内容查询

enter image description here

希望有人可以提供帮助,谢谢

1 个答案:

答案 0 :(得分:1)

您需要以正确的顺序传递您的值

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

将是

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

这将解决您的问题