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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

looping through a datareader or converting reader to a datatable?

Status
Not open for further replies.

davikokar

Technical User
Joined
May 13, 2004
Messages
523
Location
IT
hello,

I need to apply a function to some data before they are displayed in a dropdownlist. A solution could be to loop through the datareader and apply the function there but I don't know how to define the parameter for the funtion.

Code:
Sub BindArchive()
 dim myConnection as new OleDBConnection ("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\inetpub\[URL unfurl="true"]wwwroot\new1\blog.mdb")[/URL]
   dim objCmd_archive as OleDbCommand = new OleDbCommand ("Query1", myConnection)
      objCmd_archive.CommandType = CommandType.StoredProcedure 
	  
   myConnection.Open()
   cbo_archive.DataSource= objCmd_archive.ExecuteReader()
   while cbo_archive.DataSource.read()
   convertDate()
   end while
   cbo_archive.DataBind()
   myConnection.Close() 
End Sub  




Private function ConvertDate(mes_monthyear as string) as string
	mes_monthyear = InStr(1, mes_monthyear, " ", CompareMethod.Text)
	return mes_monthyear
End function
 
Does the dropdownlist have an ItemDataBound event like a datagrid? If so, you can probably get at it that way.

Otherwise you can loop through the dropdown list yourself I think?
Code:
Dim strSomeString as String

For i = 0 to MyDropDown.Items.Count - 1
   SomeString = ConvertDate(MyDropDown.Items(i).Value)
' or maybe this is more what you're after?
   MyDropDown.Items(i).Value = ConvertDate(MyDropDown.Items(i).Value)
Next

Not sure if that works or not....at home with no visual Studio.

hth,
FD
 
You can also format the date the way you want in your stored procedure using the Conver() function.
 
I tried different solutions but none works. I cannot convert the date in the stored procedure because access functions dont work through oledb.

trying to loop through the datareader:
Code:
Sub BindArchive()
   dim objConnect as new OleDBConnection ("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\inetpub\[URL unfurl="true"]wwwroot\new1\blog.mdb")[/URL]
   dim objCmd_archive as OleDbCommand
   dim objDataReader as OleDbDataReader
   dim i as integer
   objCmd_archive = new OledbCommand
   
   with objCmd_archive
      .Connection = objConnect
	  .CommandType = CommandType.StoredProcedure
	  .CommandText = "Query1"
	  .Connection.Open()
	  objDataReader = .ExecuteReader()
         while objDataReader.read()
             convertDate(objDataReader.getstring(0))
         end while	  	  
	  cbo_archive.Datasource = objDataReader
	  cbo_archive.DataBind()
	  .Connection.Close()

  End with
End Sub  



Private function ConvertDate(mes_monthyear as string) as string
	mes_monthyear = StrReverse(mes_monthyear)
	mes_monthyear = InStr(1, mes_monthyear, " ", CompareMethod.Text)
	return mes_monthyear
End function

trying to loop through the dropdown items:

Code:
Sub BindArchive()
   dim objConnect as new OleDBConnection ("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\inetpub\[URL unfurl="true"]wwwroot\new1\blog.mdb")[/URL]
   dim objCmd_archive as OleDbCommand
   dim objDataReader as OleDbDataReader
   dim i as integer
   objCmd_archive = new OledbCommand
   
   with objCmd_archive
      .Connection = objConnect
	  .CommandType = CommandType.StoredProcedure
	  .CommandText = "Query1"
	  .Connection.Open()
	  objDataReader = .ExecuteReader()	  
      For i = 0 to cbo_archive.Items.Count - 1
      cbo_archive.Items(i).Value = ConvertDate(cbo_archive.Items(i).Value)
      Next	  
	  cbo_archive.Datasource = objDataReader
	  cbo_archive.DataBind()
	  .Connection.Close()

  End with
End Sub  



Private function ConvertDate(mes_monthyear as string) as string
	mes_monthyear = StrReverse(mes_monthyear)
	mes_monthyear = InStr(1, mes_monthyear, " ", CompareMethod.Text)
	return mes_monthyear
End function

any ideas? thanks
 
It should be simple enough. Modify the first example in faq855-5662 to use a stored procedure rather than a SQL Statement and simply call the function in the While loop.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top