Monday, March 26, 2012

Strings and parsing and errors with split

Hi

I am fairly new to .NET. However, I am very experienced with ASP. Here is the error I am getting
Value of type '1-dimensional array of String' cannot be converted to 'String'.
I am trying the following:
Dim tot As String
tot=cmd3.executescalar()
tot = split(tot,",")
response.write (tot)
Does anyone have any ideas of how I can get the values "1,2,3" from the database (as a string), parse those into an array, and then have the database look up each value and bid the results to the page. I have the datagrid in place. At the moment, my code works with 1 variable from the database (like 12), but not multiples like "1,2,3". The numbers could be anything too - "12,17" or "1,4,5,7,33,46"
Any ideas are appreciated - I am stuck as of now.
Thanks
HBHey,

You are on the right track, but you need to declare tot as an array, as such:

Dim nbrs As String = cmd3.ExecuteScalar()
if (nbrs <> String.Empty) Then
Dim tot() As String = nbrs.split(","c)
'Loops through the entries in the array
for each strID As String in tot
Response.Write(strID & "<br>")
next
end if

Brian
Wow - thanks for the quick response - I will try it out today.
HB
There is still and error:
<code>
Dim tot() As String = nbrs.split(","c)
<code
The error is - Variable 'tot' hides a variable in an enclosing block.
Is this "c" suppose to be there? I also tried nbrs.split(tot,",") and it gave back the same error.
I think I am getting close. I also need to use each of the numbers from the array in a SQL statement to get information from the DB and bind it. So, there will be one column with 3 results from the array containing 3 elements.

Thanks
HB
c means convert "," to a char. In C#, you denote chars with ', which obviously you can't do in VB.NET.

I think it means you are using tot outside of the if statement block, which the object wouldn't exist. Could you post your entire code so I can verify?

Brian
Sure - here is the code - it's messy at the moment. What it is suppose to do is select a product from the main page and then publish the 3 chemicals which are used for the product. So, I am using a one to many relationship. The data is stored in the database as 1,2,3 and I must take the 1,2,3 and get product 1, then product 2, then product 3. I also need to get away from the scalar - it should be Reader after closer inspection.
<code>
<%@. Page Language="VB" Debug="True" %>
<%@. Import Namespace="System.Data" %>
<%@. Import Namespace="System.Collections" %>
<%@. Import Namespace="System.Data.SqlClient" %>
<%@. Import Namespace="System.Configuration.ConfigurationSettings" %>
<%@. Register TagPrefix="header" TagName="top" src="http://pics.10026.com/?src=header.ascx" %>
<%@. Register TagPrefix="footer" TagName="bottom" src="http://pics.10026.com/?src=footer.ascx" %>
<html>
<head>
<title></title>
<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
Dim Conn As SQLConnection
Dim Rdr,Rdr2 As SQLDataReader
Dim Q1,Q2,Q3,total,number,Names,stringSQL2,Word As string
Dim x As integer

Q2 = Request.QueryString("product")
Dim strConn As string = AppSettings("connect")

Dim stringSQL As string = "Select " & Q2 & " from menu_green"

Dim SQL as string="Select names1,names2,names3 from menu_green where C1='" & Q2 & "'"
SQL = SQL & "OR C2='" & Q2 & "'"
SQL = SQL & "OR C3='" & Q2 & "'"
Dim Conn2 as New SQLConnection(strConn)
Dim Cmd3 as New SQLCommand(SQL,Conn2)
Conn2.Open()
Dim tot As String
'litHowMany.text=cmd3.executescalar()
tot=cmd3.executescalar()
'Response.write ("" & tot)
'Conn2.Close()

Dim nbrs As String = cmd3.ExecuteScalar()
if (nbrs <> String.Empty) Then
Dim tot() As String = nbrs.split(","c)
'Loops through the entries in the array
for each strID As String in tot
Response.Write(strID & "<br>")
next
end if

stringSQL2 = "Select id,names from products where id='" & tot & "'"
Conn = New SQLConnection(strConn)
Dim Cmd2 As New SQLCommand(stringSQL2,Conn)
Conn.Open

'Dim myname As string
'myname = cmd2.executescalar()
'response.write ("" & myname & "<br>")

Rdr2 = Cmd2.ExecuteReader()
myproducts.datasource = Rdr2
myproducts.databind()
Conn.close

Conn = New SQLConnection(strConn)

Dim Cmd As New SQLCommand(stringSQL,Conn)
'Dim Cmd2 As New SQLCommand(stringSQL2,Conn)

Conn.Open
Rdr = Cmd.ExecuteReader()
mylist.datasource = Rdr
mylist.databind()
Conn.close

End Sub
</script>
<code
Thanks for any input

HB
One thing: You declared tot twice, once as a variable, once as an array.

0 comments:

Post a Comment