sqldatareader over dataset

hi,

im wondering which is the best to use sqldatareader or dataset

what im doing is that, im populating my dropdownlist only, please advise.

the second question i have is:

im getting sqldatareader as arraylist whcih means its returning the arrylist, how can i use the arrylist to populate my ddl?

thank you.
code1994Asked:
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.

mmarinovCommented:
Hi code1994,

it is better to use datareader instead of dataset, because the dataset is put in memory, but datareader is not
also you don't need to return it as arraylist just bind directly to the dropdownlist

Regards!
B..M
mmarinov
0
code1994Author Commented:
thanks BM

but we have global function that use everybody and the function is returns the arrylist instead of sqldatareader now its returning my data in arrylist but how can i grab my data from arrylist and populate ddl ?

thanks
0
mmarinovCommented:
code1994,

what is the  type of data in arraylist ?

Regards!
B..M
mmarinov
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

code1994Author Commented:
here is the function that returns arrylist:

 Public Overloads Shared Function ExecuteReader(ByVal connection As SqlConnection, _
                                                   ByVal commandType As CommandType, _
                                                   ByVal commandText As String, _
                                                   ByVal ParamArray commandParameters() As SqlParameter) As Object
        'create a command and prepare it for execution
        Dim cmd As New SqlCommand
        Dim dr As SqlDataReader
        Dim aryFileNames As New ArrayList
        Dim strFileName As String

        PrepareCommand(cmd, connection, CType(Nothing, SqlTransaction), commandType, commandText, commandParameters)

        dr = cmd.ExecuteReader

        'detach the SqlParameters from the command object, so they can be used again
        cmd.Parameters.Clear()

        'return the results
        Do While dr.Read()
            strFileName = dr(0)
            If Trim(strFileName) <> "" Then
                aryFileNames.Add(strFileName)
            End If
        Loop
        Return aryFileNames
    End Function 'ExecuteReader
0
code1994Author Commented:
or please let me knwo if i need to modify or something the above reader function or returns that as sqldatareader instead of array? may be?

or what is the best approach  - thanks
0
mmarinovCommented:
code1994,

i'm not quite sure but may be you can directly bind the arraylist to the dropdown - try it and tell what is the result. i think that because the contents of the arraylist is string object

Regards!
B..M
mmarinov
0
kolluCommented:
Try this if it can help u
public DropDownListCell(ref string [] stArray, string IDName, string SelectVal,EventHandler DropDownEvents)
            {
                  DropDownList DDLBuyerName = new DropDownList();
                  DDLBuyerName.ID = IDName;                  
                  ListItem LstSubInv;
                  for(int i = 0; i < stArray.Length; i++)
                  {
                        LstSubInv = new ListItem();
                        LstSubInv.Text = stArray[i];
                        LstSubInv.Value = stArray[i];
                        if (stArray[i].ToString().Trim().ToUpper() == SelectVal) LstSubInv.Selected = true;
                        DDLBuyerName.Items.Add(LstSubInv);
                  }
                  DDLBuyerName.Enabled = false;
                  DDLBuyerName.AutoPostBack = true;
                  DDLBuyerName.SelectedIndexChanged += new System.EventHandler(DropDownEvents);            
            }
0
code1994Author Commented:
>>directly bind the arraylist to the dropdown

how? can u give an example pls?

thanks
0
code1994Author Commented:
kollu :

i don't think i wanna make this more complicated to just populate the data into ddl
do u think  i should modify the above function of datareader ? if yes do you hae any suggestions or advise on the above function?

im a vb.net guy :)

thanks
0
mmarinovCommented:
code1994,

Dim dt as DropDownlist
dt.DataSource =   ExecuteReader ( connection object, command type, command text, param array );
dt.DataBind()

where in the executereader method you have to filled the appropriate values of parameters
also you have to put the class where executereader is based - i think it is SqlHelper.ExecuteReader

Regards!
B..M
mmarinov
0
code1994Author Commented:
im getting the error:

Object reference not set to an instance of an object.

here is my code:

     strSQL = "mysp"
            Dim ddl As DropDownList
           ddl.DataSource = SqlHelper.ExecuteReader(strConnString, strSQL, "mypar")
 
            lstReseller.DataValueField = "ID"
            lstReseller.DataTextField = "Name"
            lstReseller.DataBind()
   

0
mmarinovCommented:
the ddl has to be a dropdownlist control placed on your web form
also you don't need to specified the datavalue and textfield because in the arraylist you have only strings

B..M
mmarinov
0
code1994Author Commented:
still getting the same error message after i change what you said.....

0
mmarinovCommented:
can you please post your code so can check what is the problem ?

B..M
mmarinov
0
kolluCommented:
I think this link will be help ful to you

http://it.maconstate.edu/tutorials/ASPNET/ASPNET11/aspnet11-04.aspx

<td><asp:DropDownList id="ItemType" runat="server"
         DataSource='<%# ItemTypes %>'
         SelectedIndex='<%# SetIndex(Container.DataItem("ItemType")) %>'/>
    </td>
Function SetIndex(TheItem As String)
  Dim i As Integer
  For i = 0 To ItemTypes.Count - 1
    If TheItem = ItemTypes(i) Then
      Return i
    End If
  Next
End Function
0
code1994Author Commented:
here is my code BM:

dim getddl as dropdownlist

 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strSQL As String
 
        If Not Page.IsPostBack Then
            strSQL = "mysp"
 
            str1 = SqlHelper.ExecuteReader(strConnString, strSQL, "mypara")
ddl.source = getddl          
ddl.DataValueField = "ID"
            ddl.DataTextField = "Name"
            ddl.DataBind()
 
        End If
    End Sub
0
mmarinovCommented:
i think that your code should be

 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strSQL As String
 
        If Not Page.IsPostBack Then
            strSQL = "mysp"
 
            str1 = SqlHelper.ExecuteReader(strConnString, strSQL, "mypara")
ddl.source = str1
            ddl.DataBind()
 
        End If
    End Sub

B..M
mmarinov
0
code1994Author Commented:
B.M - thank you bu its returning the id's but i want to get the desc

in my sql statement i have select id,name from table

its poupulating all the ids in my listbox

i even debug like

?str1(0)
its giving me number

?str1(1)
its still giving me number

any thing im missing here?

thanks
 
0
code1994Author Commented:
here is the code:

   'return the results
        Do While dr.Read()
            strFileName = dr(0)   //do i have to change or something ?
            If Trim(strFileName) <> "" Then
                aryFileNames.Add(strFileName)
            End If
        Loop
        Return aryFileNames

0
mmarinovCommented:
try
dr("name") instead of dr(0)

B..M
mmarinov
0
code1994Author Commented:
this function is global function to everyone and i don't think i can change unless i have strong case

is there way i can change from my side like when i calling this function and manupulate and display?

0
mmarinovCommented:
unfortunatelly the arraylist is filled by this, so you can not do anything if you can not change it :(

B..M
mmarinov
0
code1994Author Commented:
hmm... i see what you mean now....

is there a way i can have one global datareader function? like i want to create my own?

PS: is it true that once the datareader gives you the recordset and if you close the db connection the datareader data lost ?

thanks
0
mmarinovCommented:
you can create your own function and use dataset and dataadapter and don't use the sqlhelper
for the second question:
when the dataconnection is closed you can not retrieve anymore data from db. this is the difference between dataset and datareader - dataset keeps the records in memory and user want can edit/delete/add new and finally post this to the db and do all work at once

B..M
mmarinov
0
code1994Author Commented:
i understand and thank you so much for your help.

do you have a datareader function handy? please :)

the datareader function that anybody can call and return the data

im using vb.net

thank you so much

0
mmarinovCommented:
Unfortunately i don't have it at home, but will try to manage something for you
you can use something like this

Public Function GetReader(mySelectQuery as String, connectionString as String, ByVal ParamArray commandParameters() As SqlParameter) as Object

    Dim aryFileNames as New ArrayList
    Dim strFileName as String
    Dim myConnection as SqlConnection = New SqlConnection(connectionString)
    Dim myCommand As SqlCommand = New SqlCommand(mySelectQuery, myConnection)
    myCommand.Parameters.Add(ParamArray)
    myConnection.Open()
    Dim myReader As SqlDataReader
    myReader = myCommand.ExecuteReader()
    Try
    Do While myReader .Read()
            strFileName = myReader ("name")
            If Trim(strFileName) <> "" Then
                aryFileNames.Add(strFileName)
            End If
        Loop
        Return aryFileNames

End Function

HTH
B..M
mmarinov
0
code1994Author Commented:
hmm if i want to return all the table instead of just "name" what should i do in other words whatever the sql statement i select instead of just "name"

the reason is that, for now i need "name" but for other form i may be need some other field u know so i just want to make this as a generic or global function the user will get anything they want.

i hope im not bugging ;)

thank you again.
0
mmarinovCommented:
you can do it simply by adding a parameter to the definition :-)
Public Function GetReader(mySelectQuery as String, connectionString as String, ByVal ParamArray commandParameters() As SqlParameter, ColumnName as string) as Object

    Dim aryFileNames as New ArrayList
    Dim strFileName as String
    Dim myConnection as SqlConnection = New SqlConnection(connectionString)
    Dim myCommand As SqlCommand = New SqlCommand(mySelectQuery, myConnection)
    myCommand.Parameters.Add(ParamArray)
    myConnection.Open()
    Dim myReader As SqlDataReader
    myReader = myCommand.ExecuteReader()
    Try
    Do While myReader .Read()
            strFileName = myReader (ColumnName)
            If Trim(strFileName) <> "" Then
                aryFileNames.Add(strFileName)
            End If
        Loop
        Return aryFileNames

End Function
0
code1994Author Commented:
BM:

I modify the function you paste above but still getting error:


    Public Function GetReader1(ByVal mySelectQuery As String, ByVal connectionString As String, ByVal ColumnName As String, ByVal ParamArray1 commandParameters() As SqlParameter) As Object

        Dim aryFileNames As New ArrayList
        Dim strFileName As String
        Dim myConnection As SqlConnection = New SqlConnection(connectionString)
        Dim myCommand As SqlCommand = New SqlCommand(mySelectQuery, myConnection)
        myCommand.Parameters.Add(ParamArray1)
        myConnection.Open()
        Dim myReader As SqlDataReader
        myReader = myCommand.ExecuteReader()
        Try
            Do While myReader.Read()
                strFileName = myReader(ColumnName)
                If Trim(strFileName) <> "" Then
                    aryFileNames.Add(strFileName)
                End If
            Loop
            Return aryFileNames  <---<<<< getting error can not be converted to sqlparameter

    End Function
0
mmarinovCommented:
how exaclty you cal this method ? because if it fails like this the problem is with the calling

B..M
0
code1994Author Commented:
im not calling yet i just copy ur code and paste on class.vb

and when i compile i get one more new error:

 Public Function GetReader1(ByVal mySelectQuery As String, ByVal connectionString As String, ByVal ColumnName As String, ByVal ParamArray1 commandParameters() As SqlParameter) As Object

class.vb(13): All parameters must be explicitly typed if any are.

and the error is pointing to ( ByVal ParamArray1 commandParameters())


0
code1994Author Commented:
next error im getting here:

  Return aryFileNames  <<<<<<<<--- here


Class.vb(30): Value of type 'System.Collections.ArrayList' cannot be converted to 'System.Data.SqlClient.SqlParameter'.
0
mmarinovCommented:
grrrrrr
this is incorrect declaration :( sorry for that
modify it to this

 Public Function GetReader1(ByVal mySelectQuery As String, ByVal connectionString As String, ByVal ColumnName As String, ByVal ParamArray1() As SqlParameter) As Object

B..M
mmarinov
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
code1994Author Commented:
thanks BM
0
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.