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

Cast from type 'Field' to type 'String' is not valid.

Status
Not open for further replies.

henslecd

MIS
Apr 21, 2003
259
US
I am converting an asp page to asp.net and I am one section away from getting it to work.

Here is what I have...

Do Until rs.EOF

HttpContext.Current.Response.Write(&quot;<option value=&quot;&quot;&quot; & rs(0) & &quot;&quot;&quot;>&quot; & rs(1) & &quot;</option>&quot; & vbCrLf)

rs.MoveNext()

Loop


I get an error that says Cast from type 'Field' to type 'String' is not valid. Not sure what is wrong.

Thanks for the help.
 
I think that you need to use ADO.NET - using a DataReader.

Dim strConn As String = &quot;server=SERVERNAME;uid=Login;pwd=Password;Initial Catalog=Database&quot;
Dim cn As SqlConnection = New SqlConnection(strConn)
Dim cmd As New SqlCommand
Dim myReader As SqlDataReader
cmd.Connection = cn
cmd.CommandText = &quot;SELECT DISTINCT Name, ID FROM Table&quot;

Try
cn.Open()
myReader = cmd.ExecuteReader
Do While myReader.Read
Dim NewItem As New ListItem
NewItem.Text = myReader(&quot;Name&quot;)
NewItem.Value = myReader(&quot;ID&quot;)
Me.lstLIST.Items.Add(NewItem)
Loop
Catch ex As Exception
lblError.Text = &quot;A problem was found, please try again&quot;
Finally
If Not cn Is Nothing Then
cn.Close()
End If
If Not myReader Is Nothing Then
myReader.Close()
End If
End Try

Hope everyone is having a great day!

Thanks - Jennifer
 
Thanks Jennifer.

Actually all I needed was

rs.fields(0).value in place of rs(0).

 
You can simply bind your control without having to go into a loop:
Code:
<asp:DropDownList id=&quot;myList&quot; runat=server DataTextField=&quot;customerId&quot; DataValueField=&quot;customerName&quot; />

myReader = cmd.ExecuteReader
if myReader.HasRows then
  myList.DataSource = myReader
  myList.DataBind
else
  myList.Visible = false
end if
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top