使用下拉框和按钮,我试图将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>
当从下拉列表中选择一个值并单击按钮时,我让它出现在一个标签上,我只是试图将它保存为变量,以便可以读入insery查询
希望有人能帮忙,谢谢
最佳答案
你需要按正确的顺序传递你的值
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)"
这会解决你的问题
关于mysql - 如何将数据库中的值存储为变量以在插入语句中使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38096259/