Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Fetching the Primary Key from from the selected row in Gridview

Posted on 2013-02-06
13
Medium Priority
?
300 Views
Last Modified: 2013-02-06
I have a scenario where I conditionally need to determine whether the insert or update statement needs to be fired based on wheter Event_Key already exists in database.

The error occurs at        
selectedID = GVViewEvents.SelectedRow.Cells(0).Text
Error: Object reference not set to an instance of an object.


Gridview code:
<asp:GridView ID="GVViewEvents" DataSourceID="sdsEvents" DataKeyNames="Linea,Event_Key" runat="server" Height="186px" Width="713px" CellPadding="4" ForeColor="#333333" GridLines="None" BorderStyle="Solid" BorderWidth="2px" BorderColor="#D8D8D8" PageSize="4" AutoGenerateColumns="False">
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"   />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <Columns>                                     
                        <asp:CommandField ShowEditButton="True" EditText="Update"   />                        
                        <asp:BoundField DataField="Event_Key" HeaderText="Event_Key" InsertVisible="true" SortExpression="Event_Key" Visible="true" ReadOnly="true" />
                        <asp:BoundField DataField="Linea" HeaderText="Linea" SortExpression="Linea" ReadOnly="true" />
                        <asp:TemplateField HeaderText="QTY de Lineas de Ensamble" SortExpression="Total_Qty">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Total_Qty") %>' Width="25px"></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Total_Qty") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
                    <EditRowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                </asp:GridView>

Open in new window


VB Code:
Private Sub gridview1_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles GVViewEvents.RowUpdated

        Dim determineSP As String
        Dim sqlStr As String

        Dim sConn As String = ConfigurationManager.ConnectionStrings("BusDevConnectionString").ToString
        Dim myConnection As SqlConnection = New SqlConnection(sConn)
 
        sqlStr = ""
        Dim selectedID As String
        selectedID = GVViewEvents.SelectedRow.Cells(0).Text

        determineSP = "SELECT * FROM Lenovo_events WHERE Event_Key = '" & selectedID & "'"

        If selectedID Is Nothing Then
            sqlStr = "insert into lenovo_events values (@event_date,@line_key, @shift_key, @total_qty, 0,0)"
        Else
            sqlStr = "update lenovo_events set total_qty = @total_qty where event_key = @event_key"
        End If

        GVViewEvents.DataBind()
    End Sub

Open in new window


Thanks for your time!
0
Comment
Question by:JessyRobinson1234
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
13 Comments
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 38858854
Hi,

You have to reference it through the sender argument. I think it goes something like :

selectedID = CType(sender,GridView).SelectedRow.Cells(0).Text

Open in new window


Giannis
0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 38858869
Since you are going to refernce it later in your code again, you could do the following:

Private Sub gridview1_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles GVViewEvents.RowUpdated

        Dim determineSP As String
        Dim sqlStr As String

        Dim sConn As String = ConfigurationManager.ConnectionStrings("BusDevConnectionString").ToString
        Dim myConnection As SqlConnection = New SqlConnection(sConn)
 
        sqlStr = ""
        Dim selectedID As String

        Dim gv As GridView = CType(sender, GridView)
        selectedID = gv.SelectedRow.Cells(0).Text

        determineSP = "SELECT * FROM Lenovo_events WHERE Event_Key = '" & selectedID & "'"

        If selectedID Is Nothing Then
            sqlStr = "insert into lenovo_events values (@event_date,@line_key, @shift_key, @total_qty, 0,0)"
        Else
            sqlStr = "update lenovo_events set total_qty = @total_qty where event_key = @event_key"
        End If

        gv.DataBind()
    End Sub

Open in new window


Giannis
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38858978
Change this

selectedID = GVViewEvents.SelectedRow.Cells(0).Text

to

selectedID = e.OldValues(0)

or

selectedID = e.NewValues(0)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:JessyRobinson1234
ID: 38858983
Hi, thak you. My error:

selectedID = gv.SelectedRow.Cells(0).Text
Object reference not set to an instance of an object.
0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 38859138
Hi,

Change to this:

selectedID = gv.Rows(e.RowIndex).Cells(0).Text

Open in new window


Thanks,
Giannis
0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 38859152
And it seems it should work without having the gv, so you could have it your way:

selectedID = GVViewEvents.Rows(e.RowIndex).Cells(0).Text

Open in new window


Giannis
0
 

Author Comment

by:JessyRobinson1234
ID: 38859203
Rowindex is not a member of System.Web.UI.Webcontrols.GridViewUpdatedEventArgs
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38859633
Did you have a look at http:#a38858978 ?
0
 

Author Comment

by:JessyRobinson1234
ID: 38860596
CodeCruiser, thanks for pointing it out, I did not. I tried your suggestion selectedID = e.OldValues(0) and based on the attached screenshot it's assigning "15" as selectedID. I would like to fetch the event_key which is in this case 17.

selectedIDCan you assist?
0
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 2000 total points
ID: 38860753
Can you check the value of e. keys(0) . It might be what you want.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38861322
By the way, you just had to adjust the columnindex with my technique.
0
 

Author Comment

by:JessyRobinson1234
ID: 38861351
I agree, I should give you half the points. How can I do that?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38862043
Don't worry :-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question