【问题标题】:Insert data from a grid and dropdown menu into SQL Server database with vb使用 vb 将网格和下拉菜单中的数据插入 SQL Server 数据库
【发布时间】:2025-12-23 22:25:17
【问题描述】:

我正在尝试插入已登录用户的药物和详细信息(PateintId、MedicineId、PharmacyId、医生 ID、订购日期),稍后其他用户将编辑批准日期列。

当用户单击选择命令并从下拉框中选择药房并单击订单时,这将更新Order_pres 表。我目前正在处理的代码不起作用 - 任何人都可以帮忙吗?

我需要网格中的e.command 来选择药物、会话中的患者、他们的医生(即医生表中的外键)。药房将从下拉列表中选择,我还需要在单击 btnconfirm 时更新日期。

目前正在处理的代码:

Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
    If e.CommandName = "UpdateMedicine" Then
        Dim MedicineID As Integer = Integer.Parse(e.CommandArgument.ToString())

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


        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
                'It's good practice to explicitly declare your parameter types 
                'I'm assuming "Id" fields are really integers. If that's wrong, adjust the types here to match the database
                .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 If

      End Sub

网格:

 <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>

下拉菜单、btnconfirm 和标签:

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

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

<asp:Label ID="lblconfirm" runat="server" Text="your order has been placed"></asp:Label>

网格代码:

    Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)

    If Not IsPostBack Then
        Dim conn As New System.Data.SqlClient.SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Laura\Final_proj\App_Data\surgerydb.mdf;Integrated Security=True;Connect Timeout=30")
        Dim cmdstring As String = "SELECT md.MedicineId, md.Name, md.Purpose, md.Instrcutions  " +
                                    "FROM Patient pt INNER JOIN prescription pr ON pt.PatientId = pr.PatientId  " +
                                    "INNER JOIN medicine md ON md.MedicineId = pr.MedicineId Where pt.PatientId  = @PatientId"
        Dim dt As New System.Data.DataTable()
        Dim da As New System.Data.SqlClient.SqlDataAdapter(cmdstring, conn)
        da.SelectCommand.Parameters.Add("@PatientId", System.Data.SqlDbType.Int).Value = CInt(Session("PatientId").ToString())
        conn.Open()
        da.Fill(dt)
        conn.Close()

        GridView1.DataSource = dt
        GridView1.DataBind()
    End If

End Sub

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

希望有人可以帮助我一直在拼命尝试使其正常工作

亲切的问候

【问题讨论】:

  • 当您说“不起作用”时,是否有任何错误消息?这种“不起作用”的症状是什么?您是否确保在单步执行代码时您实际上进入了Protected Sub GridView1_RowCommand... 子目录?
  • @Jeroen 我对 vb 还很陌生,我有一些构建错误我不知道如何添加下拉和 .click 函数,希望有人有一个例子
  • RE: @laurajs “我有很多构建错误”,以小块进行更改并经常编译,这样你就知道哪里出了问题,并且可以逆转更改 (CTRL+Z) 或修复他们。一次性编写一个大型(ish)程序,然后要求人们修复编译错误是很懒惰的。
  • 对不起,如果没有办法修复,我会删除这个帖子,但我真的很努力
  • Catch ex As SqlException 在 VB 中不需要括号(或对此有效)

标签: asp.net sql-server vb.net


【解决方案1】:

这里有很多错误。您需要一次解决这些问题。我注意到的第一个问题是您需要考虑列名中的空格:

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
        'It's good practice to explicitly declare your parameter types 
        'I'm assuming "Id" fields are really integers. If that's wrong, adjust the types here to match the database
        .Add("@PatientId",  SqlDbType.Integer).Value = CInt(strPatientId)
        .Add("@MedicineId", SqlDbType.Integer).Value = CInt(strMedicineId)
        .Add("@PharmacyId", SqlDbType.Integer).Value = CInt(strPharmacyId)
        .Add("@DoctorId",   SqlDbType.Integer).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)
        MessageBox.Show(ex.Message.ToString(), "Error Message")
    End Try
End Using

【讨论】:

  • 非常感谢您花时间帮助我编写代码。我是一个初学者,非常感谢您对我的学习所做的贡献,我只是有点困惑,因为我还有一个下拉菜单可以选择药房 - 这会在 e.command 还是 btn.click 上进行?非常感谢:)