Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Data Sources 1

Status
Not open for further replies.

M8KWR

Programmer
Aug 18, 2004
864
GB
I am trying to create a web page that display a list of sql databases that we hold internally (150+).

When the user select the one they require, i've got an button wish i can to use to update several grid view on the form.

What is the easiest way to change the data source that these are looking at - as in a windows application i would use the adpater etc in the code, but the code for the sql data sources is in the web config file.

Sorry if this seems a stupid questions but i am very new to asp.net development.

many thanks in advance,
 
in a windows application i would use the adpater etc in the code
You can do exactly the same in an ASP.NET application.

the code for the sql data sources is in the web config file
It is only in the web.config file if you tell it to be. Can you provide an example of your current code that you use to connect to the database and we will be able to tell you how to modify it?


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
I would use something like this to update a gridview in a windows applications

Code:
            Dim ConnectionString As String = "data source=xx.xx.xx.x; DATABASE=" & db_path.ToString & ";user id=xxxxxxx"

            Dim SQL As String = "Select * from CONTHIST"
            'Create the connection

            Dim Connection As New SqlConnection(ConnectionString)
            Connection.Open()

            Dim adapter As New SqlDataAdapter(SQL, Connection)
            Dim dtset As New DataSet
            adapter.Fill(dtset)
            adapter.Dispose()
            Connection.Close()
            Dim table As DataTable = dtset.Tables(0)

In the windows app where in the code you have the db_path.ToString this represents the database that i wish everything to connect to.

What i think i'm trying to ask, is if you are able to create a connection to an sql datasource on the fly without having to bind anything to a form, and then select the sql query you wish to pass through it and then get this information to display on the form in a datagridview or a listbox etc etc etc...

Many thanks
 
The code that you've written above will work exactly as it is now in an ASP.NET application so whatever you would do in a windows application, so the same thing here.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
sorry to be a pain, but i have copied and pasted the code that i used in my windows app....

It does not like my sqlconnection statement - i know i had to add the reference in the windows app, but i have tried in the asp.net app but still no joy???
 
Hopefully the last question, but many thanks for your help

i have this code

Code:
    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim ConnectionString As String = "data source=xx.xx.xx.x; DATABASE=BID_GMBASE;user id=xxx;password=xxxxxxxxxxxxxxx"

        Dim DBLISTSQL As String = "SELECT DBNAME DBNAME  FROM [Combined View] GROUP BY DBNAME"
        Dim DBLISTconn As New SqlConnection(ConnectionString)
        Dim DBLISTadapter As New SqlDataAdapter(DBLISTSQL, DBLISTconn)
        Dim DBLISTdtset As New Data.DataSet
        DBLISTadapter.Fill(DBLISTdtset)
        DBLISTadapter.Dispose()
        DBLISTconn.Close()
        Dim DBLISTtable As Data.DataTable = DBLISTdtset.Tables(0)
        ' Me.GridView2.DataSourceID = ""
        Me.GridView2.DataSource = DBLISTtable

    End Sub


I have a gridview2 on the form, but when i click the button the gridview does not show..... i know this is going to be something simple and i am going to shot myself asking this question.


 
Unlike windows applications, the DataSource method doesn't automatically bind the grid as well. So, you will have to add "Me.GridView2.DataBind" after you have set the DataSource.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
many thanks for you patience with me, excellent :)
 
No problem, what you've come across is something that I find an annoying difference between similar controls in windows and web controls. To me, either they should both automatically bind or neither should as this can catch out developers.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
I have found another way of doing what i did in the windows app by using the followiing

Me.SqlDataSource1.ConnectionString = "Data Source=xxxxxx;Initial Catalog=" & ListBox1.SelectedValue.ToString & ";User ID=xxxxx"
Me.SqlDataSource1.SelectCommand = "SELECT USERID, RESULTCODE, COUNT(RESULTCODE) as Count FROM CONTHIST GROUP BY USERID, RESULTCODE"

Unsure which code would be quicker???? But this saves you having to use the databind, it just updates the gridview.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top