我正在使用 asp.net 和 visual basic 创建一个网站。当用户登录时,他们被从 login.aspx 页面定向到 user.aspx 页面,并且 session 拉出他们的用户名和一个网格(网格包含来自数据库的已登录用户药物,以及一个选择命令药品编号):
医生表中的 DoctorId 作为外键存储在患者表中
Pharmacy from 是下拉框选择的+sql conneaction string:
MedicineId来自网格
<asp:SqlDataSource ID="SqlPharm" runat="server" ConnectionString="<%$ ConnectionStrings:SurgeryConnectionString %>" SelectCommand="SELECT DISTINCT Pharmname FROM Pharmacy "></asp:SqlDataSource>
<asp:DropDownList ID="DropPharm" runat="server" DataSourceID="SqlPharm" DataTextField="Pharmname" DataValueField="Pharmname"></asp:DropDownList>
一旦在网格中选择了药物选项 已选择下拉列表药店 AND 按钮被点击我希望 Order_pres 更新下表:
这是我目前正在处理的代码,没有出现任何错误,当我测试它时没有任何更新,希望其他人的知识可以指导我如何更新表格 - 包含的标签有用于测试拉取值并在运行时显示在标签上:
Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
lbldrop.Text = e.CommandArgument.ToString()
If e.CommandName = "UpdateMedicine" Then
Session("MedicineID") = Integer.Parse(e.CommandArgument.ToString())
End If
End Sub
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 PharmacyId As Integer = Session("PharmacyId")
Dim MedicineId As Integer = Session("MedicineID")
Dim DateOrdered As Date
' Get DoctorId from the patient table
Dim DoctorId As Integer = "SELECT DoctorId FROM Patient "
'.Value = CInt(Session("DoctorId").ToString())
Dim query As String = String.Empty
query &= "INSERT INTO Order_pres (PatientId, PharmacyId, "
query &= " DoctorId, [Date Ordered]) "
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
End Sub
网格持有 medicineId:
<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>
登录时存储的 session :
Dim reader = cmd.ExecuteReader()
While reader.Read()
Session("PatientId") = CInt(reader.Item("PatientId"))
Session("Username") = CStr(reader.Item("Username"))
Session("DoctorId") = CStr(reader.Item("DoctorId"))
found = CInt(reader.Item("PatientId"))
End While
更新期间发生的步骤:
最佳答案
您的插入语句在列列表中缺少 MedicineId
:
Dim query As String = String.Empty
query &= "INSERT INTO Order_pres (PatientId, PharmacyId, "
query &= " DoctorId, [Date Ordered]) "
query &= "VALUES (@PatientId,@MedicineId, @PharmacyId, @DoctorId, @DateOrdered)"
应该是
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)"
关于mysql - 获取插入查询以在 . asp.net中VB点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38016746/