Miank
Miank

Reputation: 105

Gridview is not visible on button click

I am trying to display the same grid on three button click events by passing different queries in sql command.

                <table style="width:100%;" cellspacing="2" class="Text">
                    <tr>
                        <td align="left" colspan ="2">
                            <asp:Button ID="btnreport1" runat="server"
                                     Text="Report1" Width="104px"
                                     OnClick ="btnreport1_Click1"/>
                        </td>
                    </tr>                         
                </table>
                            <tr>
                        <td>
                            <asp:GridView ID="GridView1" runat="server" 
                            AutoGenerateColumns="True" AllowPaging="True"
                                 AllowSorting="True">
        </asp:GridView>

VB Code

Protected Sub btnreport1_Click1(sender As Object,
                        e As EventArgs) Handles btnreport1.Click
        Using conn1 As New SqlConnection(ConfigurationManager.ConnectionStrings(
                         "TERAMSConnectionString").ConnectionString)
            conn1.Open()
            Dim cmd2 As SqlCommand =
               New SqlCommand("select * from AssetsHardware_Master ,
                               Desktop where Serial_no !=SerialNo", conn1)
            Dim dr As SqlDataReader = cmd2.ExecuteReader
            Dim dt As DataTable = New DataTable
            dt.Load(dr)
            GridView1.DataSource = dt
            GridView1.DataBind()
        End Using
    End Sub

Upvotes: 1

Views: 1610

Answers (4)

Mikhail Timofeev
Mikhail Timofeev

Reputation: 2169

copypaste error?

your Button:

<asp:Button ID="btnreport1" runat="server" Text="Report1" Width="104px" 
            OnClick ="btnreport1_Click1"/>

your method declaration:

 Protected Sub Button1_Click(sender As Object, e As EventArgs) _ 
     Handles Button1.Click

Your method will be never used, try this:

<asp:Button ID="Button1" runat="server" Text="Report1" Width="104px"/>

or this:

Protected Sub btnreport1_Click1(sender As Object, e As EventArgs)

Upvotes: 0

laylarenee
laylarenee

Reputation: 3284

I observed a problem in your code sample. You Button control ID is "btnreport1", but your button click event handler handles a non-existed control ID named "Button1.Click". In order for your event handler to handle the click event from your button, its handles clause must be changed to:


Protected Sub Button1_Click(sender As Object, e As EventArgs) _
    Handles btnreport1.Click

You can also make a single handler that handles many button clicks...


Protected Sub Button1_Click(sender As Object, e As EventArgs) _
    Handles btnreport1.Click, _
    Handles btnreport2.Click, _
    Handles btnreport3.Click, _
    Handles btnreport4.Click

About the feature known as "AutoEventWireup"... At the top of every ASPX page, in the Page Declaration, there is an attribute called "AutoEventWireup". This is set to true or false. When using the true settings, ASP.NET automatically connects buttons and other controls to event handlers. For example, a control named "MyButton" click event would automatically get bound to an event handler names "MyButton_Click". When using the false setting, you must explicitly "wire up" buttons to event handlers using the "... Handles MyButton.Click" clause.

Whether you are using AutoEventWireup or not, your code example will never work becuase the control IDs and Handles declarations do not match.

Upvotes: 0

Manoj
Manoj

Reputation: 631

Here You Can not calling method which bind the Grid View

In Design View OnClick ="btnreport1_Click1"

and in Code Behind Button1_Click()

thats why it will not showing result in Grid view

Upvotes: 2

Tim Schmelter
Tim Schmelter

Reputation: 460108

You have an incorrect condition in your WHERE-clause:

where Serial_no != SerialNo

This will at least return nothing because no SerialNo is != itself (it's the same as where 1=2).

I asssume that you want to use parametrized queries:

Dim cmd2 As SqlCommand = New SqlCommand("select * from AssetsHardware_Master ,Desktop where Serial_no != @Serial_no", conn1)
cmd2.Parameters.AddWithValue("@Serial_no", SerialNo) 

Here SerialNo is a variable, if it's a static string SerialNo:

cmd2.Parameters.AddWithValue("@Serial_no", "SerialNo") ' presuming varchar column

Upvotes: 0

Related Questions