Thursday, March 22, 2012

looping through a datareader

hallo, I cannot make this working. I would like to loop through the datareader but the while end while doesn't work. What am I doing wrong?

.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()

The GetString method returns a new string object of the current DataReader item. Seethis page in the MSDN. This means that you are converting the new string object, and NOT the DataReader item. Therefore the changes won't be reflected outside of the convertDate method/function.

You should pass in the DataReader item (in this caseobjDataReader(0)) and then change that.


Here I used a stored procedure just like you so try to do the following:
dataReader = SqlDataAdapter3.SelectCommand.ExecuteReader()

While dataReader.Read

txtNewDescription.Text = dataReader.GetString(3).ToString

ddlStartMonth.SelectedValue = dataReader.GetInt32(4)

ddlEndMonth.SelectedValue = dataReader.GetInt32(5)

txtEffectDate.Text = dataReader.GetDateTime(7)

EndWhile


onewisehobbit wrote:

Here I used a stored procedure just like you so try to do the following:
dataReader = SqlDataAdapter3.SelectCommand.ExecuteReader()

While dataReader.Read

txtNewDescription.Text = dataReader.GetString(3).ToString

ddlStartMonth.SelectedValue = dataReader.GetInt32(4)

ddlEndMonth.SelectedValue = dataReader.GetInt32(5)

txtEffectDate.Text = dataReader.GetDateTime(7)

EndWhile

Unfortunately this won't work, as the DataReader is being assigned to the DropDownList's DataSource. My previous post highlights the error.


read the question wrong -- thought he was having problems with the actually reader reading the data. You are right.

0 comments:

Post a Comment