【问题标题】:Get an insert query to run after a . click event with VB in asp.net获取插入查询以在 .在asp.net中使用VB点击事件
【发布时间】:2016-06-24 15:16:10
【问题描述】:

我正在使用 asp.net 和 Visual Basic 创建一个网站。当用户登录时,他们会从 login.aspx 页面定向到 user.aspx 页面,并且会话会调出他们的用户名和一个网格(网格包含数据库中的登录用户药物,并带有一个用于选择的命令来选择药物 ID):

医生表中的DoctorId作为外键存储在患者表中

Pharmacy from 下拉框选+sql连接字符串:

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>

登录时存储的会话:

 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

更新过程中发生的步骤:

【问题讨论】:

  • 4个参数传入,然后你给5个。在插入。身份证

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


【解决方案1】:

您的插入语句在列列表中缺少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)"

【讨论】:

  • 嗨 Zohar - 还有一些问题 - 如何从患者表中获取医生 ID,从下拉列表中获取药房 ID,并在插入时存储日期。我将更新问题以显示此
  • 我已经更新了这个问题,你需要更多关于我想要做什么的信息
  • 我认为问题出在**获取pharmacyId**(但下拉菜单必须显示药房名称-&lt;asp:SqlDataSource ID="SqlPharm" runat="server" ConnectionString="&lt;%$ ConnectionStrings:SurgeryConnectionString %&gt;" SelectCommand="SELECT DISTINCT Pharmname FROM Pharmacy "&gt;&lt;/asp:SqlDataSource&gt;
  • 其实这篇文章信息量太大,问题也太多了。 SO中的帖子应该针对特定问题,您的问题现在太长了,很难理解您遇到的问题。请尝试更加专注,也许就您遇到的其他问题提出更多问题。
  • 还从患者表中获取医生 ID ` Dim DoctorId As Integer = "SELECT DoctorId FROM Patient" `
【解决方案2】:

正如 Zohar 指出的那样,您在 INSERT INTO 子句中缺少 MedicineId 字段。那是一个。其次,您误解了会话变量背后的概念。注意这个sn-p:

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

While 循环的第一次迭代中,您有效地在会话中创建了三个变量。但是在随后的迭代中,这些变量会得到更新;不会为每条记录创建新变量。因此,只有读取器提供的最后一条记录被存储。

稍后,当您从网格中的任何行点击逻辑时,它会执行 T-SQL 语句,提供登录时加载的最后一条记录中的最后三个 PatientIdUsernameDoctorId。这样您就看不到更新 - 它们出现在您不希望更新的记录上。

现在我不明白你为什么要搞砸Session 来做这件事。而是循环遍历GridView 中的单元格并从那里输入命令参数;不要乱用Session。 这里是good tutorial,了解如何使用GridView

【讨论】:

    猜你喜欢
    • 2016-03-04
    • 1970-01-01
    • 2019-06-02
    • 2014-04-06
    • 2015-05-11
    • 2012-07-29
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    相关资源
    最近更新 更多