Click here to Skip to main content
15,921,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone!

I try to fill a dataset in order to have datas in a gridview.

The following code works for a WindowsApplication but not for a website.

Can you tell me where i am wrong or what is missing?

Thank you!

VB.NET
Imports System.Data
Imports System.Data.OleDb

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub ButtonSearch_Click(sender As Object, e As EventArgs) Handles ButtonSearch.Click

        Dim cnfd As New OleDbConnection(ConfigurationManager.ConnectionStrings("FoodmartConnectionString").ConnectionString)

        Dim ssql As String = "SELECT [customer_id], [lname], [fname] FROM [Customers] WHERE lname like '" & TextBoxCustomer.Text & "%' ORDER BY [lname]"

        Dim cmdcust As New OleDbCommand(ssql, cnfd)

        Dim ds As New DataSet

        Dim da As New OleDbDataAdapter(cmdcust)

        da.Fill(ds, "C")

        GridViewCustomer.DataSource = ds.Tables("C")

    End Sub
End Class


What I have tried:

I tried the same code for a WindowsApplication and it works.
Posted
Updated 23-Jul-16 2:53am
Comments
Karthik_Mahalingam 23-Jul-16 5:48am    
what is the error you are facing?
Member 12649673 23-Jul-16 6:51am    
I have a textbox and a button. When i click on the button i would like to have all the customers (in the gridview) whose lname begins with what i write in the textbox.

The gridview is filled for a Windows Application but not for a website, nothing happens.
Richard MacCutchan 23-Jul-16 5:59am    
You need to render the content back to your web page. Web applications are completely different from Windows Forms.
Member 12649673 23-Jul-16 6:24am    
Thanks for your answer. I haven't worked on websites lately.
How do you render the content back to the webpage?
Richard MacCutchan 23-Jul-16 6:41am    
Well you need to learn all about websites and how to program them. This is not a question that can be answered in a Quick Answers forum.

1 solution

call DataBind after assigning the datasource

VB
GridViewCustomer.DataSource = ds.Tables("C") 
        GridViewCustomer.DataBind()
 
Share this answer
 
Comments
Member 12649673 23-Jul-16 13:30pm    
Thank you! I added gridviewcustomer.databind() before but at that time my query was not good, i had for the "like" statement * instead of %. So i thought the VB.Net code was wrong.

Everything works!
Karthik_Mahalingam 24-Jul-16 1:49am    
cool
by the way you code is vulnerable to SQL injection [^] attacks,
always use Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]
Member 12649673 24-Jul-16 3:45am    
Thanks a lot!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900