Simple example of populating Grid View SQL Query On Button Click in VISUAL BASIC/ASP>NET

I need a simple example of the syntax for an on_click event that will populate a asp.net gridview based on a sql query

the sql query will take multiple selections from an asp.net listbox (the values are populated with a sql query themselves - I know how to do this)

PLEASE HELP!!!!
theclassicAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

theclassicAuthor Commented:
I need it in pure Visual Basic?  This is C# and script right?
 
 
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

sm394Commented:
you can do somthing like this in vb.net  for details ref:http://www.aspnettutorials.com/tutorials/controls/dropdownlist-gridview-vb.aspx
==========
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
gridview1.DataSource = getdataset().Tables(0)
gridview1.DataBind()
End Sub
===============
Private Function getdataset() As DataSet
Dim connectionstring As String = "Data Source=localhost;Initial Catalog=northwind;User ID=sa;password="
Dim query As String = "select p.categoryid,p.productid, p.productname,c.categoryid,c.categoryname
from products p,categories c where p.categoryid=c.categoryid and c.categoryid<3"
Dim myconnection As New SqlConnection(connectionstring)
Dim ad As New SqlDataAdapter(query, myconnection)
Dim ds As New Data.DataSet()
ad.Fill(ds)
Return ds
End Function
0
theclassicAuthor Commented:
I will try your example, but I was working on this - using the connection string as a class but I think it is way over my head and i am not sure of the syntax....
 
 

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
 
Public Class MyConnection
Private _sqlConnection As New SqlConnection()
Private _connectionString As String = "" 
Public Sub New()
Me._connectionString = System.Configuration.ConfigurationManager.ConnectionStrings("InternalConnection").ConnectionString
End Sub
Public Sub New(ByVal connectionString As String)
Me._connectionString = connectionString
End Sub
 
Public Property ConnectionString() As String
Get
Return Me._connectionString
End Get
Set(ByVal value As String)
Me._connectionString = value
End Set
End Property
Public Property Connection() As SqlConnection
Get
Return Me._sqlConnection
End Get
Set(ByVal value As SqlConnection)
Me._sqlConnection = value
End Set
End Property
Public Sub OpenConnection()
Try
Me._sqlConnection.ConnectionString = Me._connectionString
If (Me._sqlConnection.State = ConnectionState.Closed) Then
Me._sqlConnection.Open()
End If
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub CloseConnection()
Try
Me._sqlConnection.ConnectionString = Me._connectionString
If (Me._sqlConnection.State = ConnectionState.Open) Then
Me._sqlConnection.Close()
End If
Catch ex As Exception
Throw ex
End Try
End Sub
End Class
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
 
Dim Str As String = "SELECT [image] ,[expDate] ,[Photodim] ,[Photosize] ,[photoName],[Shoot] FROM [plaintalk].[Library]"
Dim myCon As New MyConnection()
Dim cmd As SqlCommand = New SqlCommand(Str,WHATWOULDGOHERE??? )
Try
myCon.OpenConnection()
GridView1.DataSource = cmd.ExecuteReader()
GridView1.DataBind()
myCon.CloseConnection()
Catch ex As Exception
lblStatus.Text = ex.Message
End Try
End Sub
 
End Class
0
theclassicAuthor Commented:
I want to use the web.config file for my connection string...ARGH, so frustrated!!!
0
theclassicAuthor Commented:
I tried your example - Visual Studio says connection string not initiallized
0
sm394Commented:
try that
Dim Str As String = "SELECT [image] ,[expDate] ,[Photodim] ,[Photosize] ,[photoName],[Shoot] FROM [plaintalk].[Library]"
Dim myCon As New MyConnection()
Dim cmd As SqlCommand = New SqlCommand(Str,myCon.ConnectionString)

----------------------------------------------------------------------------------------------------
i am not sure about your web.config connection string settings but you can select one below for reference and change the values
if Integrated Security
"Data Source=localhost;Initial Catalog=DatabaseName; Integrated Security=True"
if Login setup
"Data Source=localhost;Initial Catalog=DatabaseName;User ID=sa;password=YourPassword"

       
0
theclassicAuthor Commented:
Got it - totally different method - now I just need to add an iteration for another listbox, as well as figure out why the button click is only returning results on the first click

Imports System.Data
Imports System.Web
 
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
 
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
SearchResults.SelectParameters.Clear()
Dim SelectCommand = "SELECT [image],[expDate],[Photodim],[Photosize],[photoName],[Shoot]FROM[plaintalk].[Library]"
Dim strWr As String = "WHERE [search] LIKE '%"
Dim i As Integer = 0
Do While i < lstSegment.Items.Count
If lstSegment.Items(i).Selected Then
strWr &= "" & lstSegment.Items(i).Value & ","
End If
i = i + 1
Loop
strWr = strWr.TrimEnd(",") & "%';"
SearchResults.SelectCommand = SelectCommand + strWr
 
End Sub

 

End Class
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.