[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem Getting GridView Button to Work in Code Behind

Posted on 2012-09-12
10
Medium Priority
?
474 Views
Last Modified: 2012-09-13
I have a gridview which populates pending membership applications.  I want the button "Approve" to run the stored procedure in the code behind which inserts the application record into a client table and then deletes the record in the application table.  (I've already tested and verified that the procedure works).  Below is the code I am using in the Code Behind, and it's throwing the error "Object reference not set to an instance of an object". (on line 18 below)

I am including the relevant code below.  I know there's something that I'm missing, but I just can't seem to see it.

Thanks for taking the time to look at it and help me out.

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
        If e.CommandName = "approve" Then
            Dim con As New SqlConnection()
            con.ConnectionString = ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString

            Dim cmd As New SqlCommand
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = con
            cmd.CommandTimeout = 10000


            Dim appID As Label = CType(GridView1.FindControl("id"), Label)



            cmd.CommandText = "appAprove"

            cmd.Parameters.Add("@id", SqlDbType.Int).Value = appID.Text

            con.Open()
            Try

                cmd.ExecuteNonQuery()

                MsgBox("Record Updated")

            Catch exc As System.Exception

                MsgBox(exc.Message())

            Finally
            End Try

        End If
    End Sub

Open in new window


   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        CellPadding="4" CssClass="dataTablesXSm" DataKeyNames="id" 
        DataSourceID="adminApps" ForeColor="#333333" GridLines="None" Width="1000px" 
            HorizontalAlign="Center" OnRowCommand="GridView1_RowCommand">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="Button2" Text="APPROVE"  runat="server" CommandName="approve" />
                </ItemTemplate>
            </asp:TemplateField>

Open in new window

0
Comment
Question by:pposton
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 38393483
<asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="Button2" Text="APPROVE"  runat="server" CommandName="approve" CommandArgument="id" />
                </ItemTemplate>
            </asp:TemplateField>

Open in new window


cmd.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32(e.CommandArgument)

Open in new window

0
 

Author Comment

by:pposton
ID: 38393498
Thanks for the quick response.   When I tried the suggestion I received the execption "Failed to convert parameter value from a String to a Int32"
0
 
LVL 20

Accepted Solution

by:
informaniac earned 2000 total points
ID: 38393575
e.CommandArgument.ToString()
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:pposton
ID: 38394788
I get the same error with that statement (cmd.Parameters.Add("@id", SqlDbType.Int).Value = (e.CommandArgument.ToString())) as well.
0
 
LVL 35

Expert Comment

by:YZlat
ID: 38394938
0
 
LVL 35

Expert Comment

by:YZlat
ID: 38394956
cmd.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32(e.CommandArgument)

or


cmd.Parameters.Add("@id", SqlDbType.Int).Value = int.Parse(e.CommandArgument)
0
 

Author Comment

by:pposton
ID: 38395291
2 errors as below:

cmd.Parameters.Add("@id", SqlDbType.Int).Value = int.Parse(e.CommandArgument)
resulted in:
Overload resolution failed because no accessible 'Int' accepts this number of arguments

AND

cmd.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32(e.CommandArgument)
resulted in:
Input string was not in a correct format
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38395626
Int is for C#. What are you getting in e.CommandArgument? I guess its empty string.
0
 

Author Comment

by:pposton
ID: 38395798
I'm in vb
0
 

Author Closing Comment

by:pposton
ID: 38396190
This works after all.  I had a parameter misnamed in my stored procedure.  Fixed that and it works perfectly.

Thanks!!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month19 days, 6 hours left to enroll

834 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