Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
Private Function SelectSqlRows(ByVal connectionString As String,
    ByVal queryString As String, ByVal tableName As String) As DataSet


        Using connection As New SqlConnection(connectionString)



            Dim adapter As New SqlDataAdapter()
            adapter.SelectCommand = New SqlCommand(queryString, connection)
            Dim builder As SqlCommandBuilder = New SqlCommandBuilder(adapter)

            connection.Open()

            Dim DS As DataSet = New DataSet
            adapter.Fill(DS, tableName)

            ' Code to modify data in DataSet here 

            builder.GetUpdateCommand()

            ' Without the SqlCommandBuilder this line would fail.
            adapter.Update(DS, tableName)

            Return DS

        End Using
    End Function


What I have tried:

At Form1_Load :

SelectSqlRows(connectionString, queryString, tblCustomers)

It shows error at connectionString : slqconnection cannot be converted to string
Posted
Updated 28-Jun-16 17:22pm

1 solution

Your statement
C#
Using connection As New SqlConnection(connectionString)

is not right.

What you are trying to do set an string object to a sqlconnection object rather then to the SQLConnect.ConnectString object (which is a string)

The link below gives an good example to follow.

See SqlConnection.ConnectionString Property[^]
 
Share this answer
 
Comments
Member 12575668 29-Jun-16 9:27am    
Your right it works like that but my question is what is the code function from Private Function SelectSqlRows(ByVal connectionString As String,
ByVal queryString As String, ByVal tableName As String) As DataSet
Member 12575668 29-Jun-16 9:28am    
Correction is Return not resurn. Sorry.
RossMW 29-Jun-16 15:19pm    
Correct me if I'm wrong but is your question on How to call the SelectSqlRows function.

If this is the case then you will see from the first line of the function that

Private Function SelectSqlRows(ByVal connectionString As String,
ByVal queryString As String, ByVal tableName As String) As DataSet

has three string values passed to it and returns a dataset object. Hence to call it is essentially as you have above but you will need to set the returned object to something
ie
Dim ReturnDataset as Dataset = SelectSqlRows(connectionString, queryString, tblCustomers)
Member 12575668 2-Jul-16 0:34am    
No the correcto way is like I am doing it , calling at Form1_Load like this :

SelectSqlRows(connectionString, queryString, tblCustomers)

If I make an instance of the Function like you say I will be re writing the code, like SqlConnection, SqlDataAdapter,etc. (instance) of the Functios in the calling Form1_Load. What I did was to add all the code I need in this Funtion to work with the table Customers ,its rows , columns, queries, etc and call it with Form1_Load.

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