Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2008-11-11
10
Medium Priority
?
1,070 Views
Last Modified: 2012-05-05
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!!!!
0
Comment
Question by:theclassic
  • 5
  • 4
9 Comments
 
LVL 13

Expert Comment

by:sm394
ID: 22934978
0
 
LVL 13

Expert Comment

by:sm394
ID: 22934991
0
 

Author Comment

by:theclassic
ID: 22939868
I need it in pure Visual Basic?  This is C# and script right?
 
 
0
Industry Leaders: 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!

 
LVL 13

Expert Comment

by:sm394
ID: 22940074
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
 

Author Comment

by:theclassic
ID: 22940263
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
 

Author Comment

by:theclassic
ID: 22940279
I want to use the web.config file for my connection string...ARGH, so frustrated!!!
0
 

Author Comment

by:theclassic
ID: 22941552
I tried your example - Visual Studio says connection string not initiallized
0
 
LVL 13

Expert Comment

by:sm394
ID: 22943957
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
 

Accepted Solution

by:
theclassic earned 0 total points
ID: 22960321
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

564 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