473,503 Members | 1,910 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[Error] 'ListItem' to type 'String'

I am looping through a listbox collection to build a SQL string that
will be used to delete items from a database. I have tried many
variances of the code below but have had no luck. The code below gives
an error:

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

When i do a response.write(s) the item at the top row displays
correctly. Here is the code I am using:

Private Sub btnDeleteDrawings_Click(ByVal ...)

Dim i As Integer = 0
Dim s As String = lbColumn2.Items(0).Text.ToString

Dim strSQL As String = "DELETE FROM tbl X Y Z Where "

For Each s In lbColumn2.Items '<- Error thrown here

strSQL += "Text = '" & s & "'"
lbColumn2.SelectedIndex = -1

If c > 1 Then
strSQL += " AND "
Else
strSQL += ";"
End If
lbColumn2.Items.Remove(s)

Next

End Sub

Nov 19 '05 #1
4 1824
's' is declared as a string. Each thing in the Items collection returns a
ListItem. You can't assign a ListItem to a variable of type String.

Additionally, your code doesn't make sense. You are assigning 's' a value,
but then presumably ignoring it, because you are trying to reuse the
variable in the loop.

And lastly, you should not be modifying the contents of a collection in a
For Each loop. You can use a While loop, or a For loop counting down
backwards, if you want to remove items as you loop.

"Sparky Arbuckle" <tw*@secureroot.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I am looping through a listbox collection to build a SQL string that
will be used to delete items from a database. I have tried many
variances of the code below but have had no luck. The code below gives
an error:

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

When i do a response.write(s) the item at the top row displays
correctly. Here is the code I am using:

Private Sub btnDeleteDrawings_Click(ByVal ...)

Dim i As Integer = 0
Dim s As String = lbColumn2.Items(0).Text.ToString

Dim strSQL As String = "DELETE FROM tbl X Y Z Where "

For Each s In lbColumn2.Items '<- Error thrown here

strSQL += "Text = '" & s & "'"
lbColumn2.SelectedIndex = -1

If c > 1 Then
strSQL += " AND "
Else
strSQL += ";"
End If
lbColumn2.Items.Remove(s)

Next

End Sub

Nov 19 '05 #2
This is my first project using a listbox in this manner. Nice attitude
Marina. Very helpful.

Nov 19 '05 #3
Sparky,

Here's what you need:

Dim lbColumn2 As ListBox

Private Sub btnDeleteDrawings_Click(ByVal ...)

Dim i As Integer = 0

Dim s As String = lbColumn2.Items(0).Text.ToString

Dim strSQL As System.Text.StringBuilder

strSQL.Append("DELETE FROM tbl X Y Z Where ")

For Each ListItem As ListItem In lbColumn2.Items

strSQL.Append("Text = '" & ListItem.Text & "'")

lbColumn2.SelectedIndex = -1

If c > 1 Then

strSQL.Append(" AND ")

Else

strSQL.Append(";")

End If

lbColumn2.Items.Remove(s)

Next

End Sub

I changed the strSQL object to a string builder. It's much more efficient
for lengthy concatenations like you're doing here. Get the string out of it
after the loop finishes like this: strSQL.ToString


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Sparky Arbuckle" <tw*@secureroot.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I am looping through a listbox collection to build a SQL string that
will be used to delete items from a database. I have tried many
variances of the code below but have had no luck. The code below gives
an error:

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

When i do a response.write(s) the item at the top row displays
correctly. Here is the code I am using:

Private Sub btnDeleteDrawings_Click(ByVal ...)

Dim i As Integer = 0
Dim s As String = lbColumn2.Items(0).Text.ToString

Dim strSQL As String = "DELETE FROM tbl X Y Z Where "

For Each s In lbColumn2.Items '<- Error thrown here

strSQL += "Text = '" & s & "'"
lbColumn2.SelectedIndex = -1

If c > 1 Then
strSQL += " AND "
Else
strSQL += ";"
End If
lbColumn2.Items.Remove(s)

Next

End Sub

Nov 19 '05 #4
Thank you Justin. I got it to work earlier but will implement this
string builder for efficiency. Once again you come through in the
clutch. :-)

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1659
by: Darren | last post by:
Hi, I get the above error message when I try to run my page on the remote server. As I am a newbie with .net, I chose Project | Copy to copy the files needed to a seperate local directory then...
3
4775
by: Joey | last post by:
Hi, I'm trying to add a default item to my listbox but when I do it tells me that it's not defined, could someone tell me the syntax I need to use to get the listbox control to display a default...
0
3310
by: John Smith | last post by:
This is what I am trying to do: <asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundColumn Visible="False" DataField="id" ReadOnly="True"...
0
1586
by: pabloazorin | last post by:
I developed a Date Picker web control using C# and .net framework 1.1 I added my control to Visual Studio 2003 IDE toolbar. When I drag and drop my control to design web page, the control renders...
4
2019
by: dancer | last post by:
I get this error if an item in a radiobutton list is not chosen when filling in a form.. I added a RequiredFieldValidator, but I still get the message. Object reference not set to an instance of...
3
2235
by: dancer | last post by:
I am using Framework 1.1.4322. Who can tell me why I'm getting this error? My code follows Compilation Error Description: An error occurred during the compilation of a resource required to...
1
2551
by: KRISHNA PRAVI | last post by:
the error is "runtime error object expected" here is the code....................................................................................... <script language="javascript"...
8
8919
by: =?Utf-8?B?Q2hyaXMgSGFsY3Jvdw==?= | last post by:
Hi there I've successfully added some .NET validation controls to a page (using <asp:RequiredFieldValidator ...), however when I try to set the 'display' property to 'dynamic', my page then...
6
4254
by: Ahmedhussain | last post by:
Hi there, I m doing work on a gridview and Im getting an error: A potentially dangerous Request.Form value was detected from the client (ctl00$Content$GridView1$ctl03$TextBox1="<span...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7006
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5592
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5021
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1519
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.