4 Replies - 1017 Views - Last Post: 18 June 2016 - 07:11 AM Rate Topic: -----

#1 lcodevb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-June 16

Get a select command from a grid to update/ perform insert on a table

Posted 17 June 2016 - 04:41 PM

When a user is logged in and authenticated they are redirected from a login.aspx page to a user.aspx page that holds a session to display their medicine on a grid (this is all dummy data by the way and is being used for my own learning of vb)

I have a select button command now inserted on the grid - what I want it to do is to update the order table with their requested medication however with much effort trying I cannot get this to work.

The tables involved in my website are (prescription table that is last in that row contains composite primary keys - this links patients to their selected medicines):

Attached Image


What I want is for the order table Order_pres to update the fields when the user selects their medicine ( I will also have a drop down box for pharmacyid for the user to choose between threee pharmacies):


Attached Image


The main trouble I am having to begin to get this working is my command button:

Grid:


<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>
 </asp:Content>




What it looks like when run:

Attached Image


code behind trying to get select command to work:

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

        System.Diagnostics.Trace.WriteLine(MedicineId.ToString())

    End If
End Sub





What happens when I am debugging and click on the select button:
Attached Image


I am a beginner to this - I was trying to run a test to being back the medicineId to see if it will appear on the output window -how do I get the select command to work properly and what are the best steps in getting the insert function to the table to work?

also, this is my session from the login page - these both work and return values on the user.aspx page:


Dim reader = cmd.ExecuteReader()

        While reader.Read()
            Session("PatientId") = CInt(reader.Item("PatientId"))
            Session("Username") = CStr(reader.Item("Username"))
            found = CInt(reader.Item("PatientId"))
        End While

        reader.Close()
    End Using
    Return (found)




Hopefully someone can help - thankyou

Is This A Good Question/Topic? 0
  • +

Replies To: Get a select command from a grid to update/ perform insert on a table

#2 andrewsw   User is offline

  • RequestedRangeNotSatisfiable
  • member icon

Reputation: 6552
  • View blog
  • Posts: 26,565
  • Joined: 12-December 12

Re: Get a select command from a grid to update/ perform insert on a table

Posted 18 June 2016 - 12:10 AM

Question moved to ASP.NET forum.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • RequestedRangeNotSatisfiable
  • member icon

Reputation: 6552
  • View blog
  • Posts: 26,565
  • Joined: 12-December 12

Re: Get a select command from a grid to update/ perform insert on a table

Posted 18 June 2016 - 12:28 AM

Quote

how do I get the select command to work properly

In what way does it not work properly? What happens, or doesn't happen? Are there errors?
Was This Post Helpful? 0
  • +
  • -

#4 lcodevb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-June 16

Re: Get a select command from a grid to update/ perform insert on a table

Posted 18 June 2016 - 07:01 AM

Andrew I was mistaken - the command button works I just need to get an update to work for this

This post has been edited by andrewsw: 18 June 2016 - 07:09 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • RequestedRangeNotSatisfiable
  • member icon

Reputation: 6552
  • View blog
  • Posts: 26,565
  • Joined: 12-December 12

Re: Get a select command from a grid to update/ perform insert on a table

Posted 18 June 2016 - 07:11 AM

There is no UPDATE command in your posted code, where is your attempt? Otherwise, what is your specific question? That is, what is preventing your attempt?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1