laurajs laurajs - 4 months ago 20 Question

How to get a select button in command field working in grid view

I am creating a medication ordering website that is a prototype and holds made up data for a project using and vb. Patients, doctors and medicines are already recorded on the database. The patient logs in, sees their own medication (through a session - this is displayed on a grid) and I have added a select button - When the button is selected I want the medicine to update on the order table (also recording the date ordered).

There is a patient table and also a medicine table - the patient is linked to their medicine through a link table (prescription table) two composite keys. Once the user is logged in they are redirected to the user.aspx page -

The code to display the logged user's medicine from the database when logged in:

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.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())

GridView1.DataSource = dt
End If

End Sub

This shows the following:

logged in

I have added a new column to the grid in aspx in order to get the select buttons to appear - these are the buttons I want to correspond with the row and selected medication:

<asp:GridView ID="GridView1" runat="server" >
<asp:CommandField ShowSelectButton="True" />


The selected information should update here once the medicine from the table is selected (I will then display this table on another page once it is updated):


If I need to provide any more information to help with the question please ask. thankyou

error experienced:

enter image description here


If you add the field MedicineId to the data source, you can include the LinkButton in a TemplateField and set its CommandArgument to the MedicineID value:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand" >
                <asp:LinkButton runat="server" Text="Select" CommandName="UpdateMedicine" CommandArgument='<%# Eval("MedicineId") %>' />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Purpose" HeaderText="Purpose" />
        <asp:BoundField DataField="Instrcutions" HeaderText="Instructions" />

Then you process the RowCommand event in code-behind:

Protected Sub GridView1_RowCommand(sender As Object, e as GridViewCommandEventArgs)
    If e.CommandName = "UpdateMedicine" Then
        Dim medecineID As Integer = Integer.Parse(e.CommandArgument.ToString())
        // Update the data in the database
    End If
End Sub