473,569 Members | 2,737 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 btnDeleteDrawin gs_Click(ByVal ...)

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

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

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

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

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

Next

End Sub

Nov 19 '05 #1
4 1831
'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.goo glegroups.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 btnDeleteDrawin gs_Click(ByVal ...)

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

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

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

strSQL += "Text = '" & s & "'"
lbColumn2.Selec tedIndex = -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 btnDeleteDrawin gs_Click(ByVal ...)

Dim i As Integer = 0

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

Dim strSQL As System.Text.Str ingBuilder

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

For Each ListItem As ListItem In lbColumn2.Items

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

lbColumn2.Selec tedIndex = -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.goo glegroups.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 btnDeleteDrawin gs_Click(ByVal ...)

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

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

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

strSQL += "Text = '" & s & "'"
lbColumn2.Selec tedIndex = -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
1666
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 copied these to the remote server via FTP. The page works fine on my local machine (WinXP Pro, VS.Net etc...) Page code is:...
3
4778
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 item? I have tried the following: lstStates.Items.Insert(0, new ListItem("By State","0"))
0
3315
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" HeaderText="ID"></asp:BoundColumn> <asp:TemplateColumn HeaderText="O"> <ItemTemplate> <asp:DropDownList id="cmbObdelaj" runat="server" AutoPostBack="True"...
0
1595
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 correctly. If I change to HTML the generated html is <cc1:IT24DateTime id="IT24DateTime1" runat="server" Type="Date"></cc1:IT24DateTime> .......
4
2034
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 an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more...
3
2242
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 service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: BC30456:...
1
2561
by: KRISHNA PRAVI | last post by:
the error is "runtime error object expected" here is the code....................................................................................... <script language="javascript" src="../Scripts/calender.js"></script> </HEAD> <body bgColor="white" MS_POSITIONING="GridLayout"> <form id="Form1" style="LEFT: 0px; POSITION: absolute; TOP:...
8
8927
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 throws up the following error in the browser: CS1061: 'System.Web.UI.WebControls.TextBox' does not contain a definition for 'Web' and no extension...
6
4260
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 class='txtColo...") Im using an access database. and when ever I try to update this it doesnt work. <%@ Page Language="C#" MasterPageFile="Mysite.master"...
0
7700
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8125
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7974
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
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 we have to send another system
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.