使用vb将数据从网格和下拉菜单插入SQL Server数据库

时间:2016-06-19 22:49:22

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

我正在尝试插入登录用户的药物和详细信息(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

希望有人可以帮助我一直在努力让这个工作

亲切的问候

1 个答案:

答案 0 :(得分:3)

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

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