BC30311: Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.Label'.

I have a pretty basic vb.net page that includes a listview.  (Note, this is my first time working with listview, so I may have some of the terminology wrong).  In my list view, I display all the important data for each record, and ask the user to provide a status for each record by including a radiobutton list with various responses.  At the bottom of the page is one submit button, and when the user clicks submit, I'm saving each records value based upon which response is selected in the radiobutton list. 
My saving of data is working,  but I'm getting an error when i try to display a confirmation message that includes a label with each records selected value from the radiobutton list.  Any idea what I'm doing wrong and why I can't grab the value selected and display it ??  I'm bolding the line that throws the error..

My Code Behind..

Protected Sub updateResponse()
For Each Item As ListViewItem In lvAudits.Items
Dim rList As RadioButtonList = CType(Item.FindControl("rbAnswer"), RadioButtonList)
Dim lblAnswer As Label = rList.SelectedValue.ToString
lblAnswer.Visible = True

Dim tblAnswer As Table = CType(Item.FindControl("tblAnswerArea"), Table)
tblAnswer.Visible =
Dim conn As SqlConnection
Dim comm As SqlCommand
Dim reader As SqlDataReader
Dim connectionString As String = ConfigurationManager.ConnectionStrings("xxxxConnectionString").ConnectionString
conn =
New SqlConnection(connectionString)
comm =
New SqlCommand()
comm.Connection = conn
comm.CommandType = Data.CommandType.StoredProcedure
comm.CommandText =
Dim txtComments As TextBox = CType(Item.FindControl("txtMoreInfo"), TextBox)
Dim respID As Label = CType(Item.FindControl("lblResponseID"), Label)
Dim pnlRbAnswer As New Panel
pnlRbAnswer.Visible =
comm.Parameters.AddWithValue("ResponseID", respID.Text)
"Response", rList.SelectedItem.Value)
"Comments", txtComments.Text)
"DateSubmitted", System.DateTime.Now)
reader = comm.ExecuteReader()
End Sub


Never make important decisions on a Monday!
3/9/2009 1:12:15 PM
asp.net.getting-started 91979 articles. 4 followers. Follow

5 Replies

Similar Articles

[PageSpeed] 53
Get it on Google Play
Get it on Apple App Store


Label is a specific type of object, you can't cast a string to it, that just doesn't make any sense.

I suspect that you want to set the Text of the Label to be rList.SelectedValue.ToString() in which case you want to do this:

lblAnswer.Text = rList.SelectedValue.ToString

There's no need to Dim the lblAnswer, it's already been automatically created for you here:

<asp:Label ID="lblAnswer" runat="server" Text="test" Visible="false"></asp:Label>

3/9/2009 1:19:14 PM

Check this line:

Dim lblAnswer As Label = rList.SelectedValue.ToString
you are trying to set Label Control with String value

I think u trying to do this

Dim lblAnswer As Label =  CType(Item.FindControl("lblAnswer "), Label )

lblAnswer.Text = rList.SelectedValue.ToString



Mark "answer" if my post helped you find the answer.
"And say: My Lord increase me in knowledge."

Huthaifa Afanah
3/9/2009 1:22:40 PM

You're setting a Label control to a string value.  I think what you intended is to set the label's .Text property to the string:

Dim lblAnswer As Label
lblAnswer.Text = rList.SelectedValue.ToString

C# <---> VB.Net Translator
3/9/2009 1:22:41 PM

ther's an error in your code, you have to pass the text to the label control, lblAnswer.Text=rList.SelectedValue.ToString

but you are passing the value directly to the control itself, thats why its giving an error, and also when you have the label control in design view then its useless to have define it again in the code, just remove

Dim lblAnswer As Label = rList.SelectedValue.ToString

and add lblAnswer.Text=rList.SelectedValue.ToString in that place, it will run fine.

3/9/2009 1:23:03 PM

Oh good grief.. now I just feel silly.  Was too focused on this being because of the List View.  Thank you all for such quick & great responses!!

Never make important decisions on a Monday!
3/9/2009 1:33:58 PM

Similar Artilces:

Having an error: BC30311: Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.Label'.
Error Message: Compiler Error Message: BC30311: Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.Label'. Line 13: myGetRandom.RetrieveDetails(lblQuestion.text, lblAnswers.text, lblViewCount.text, lblLastUpdated.text) Sub Page_Load Dim myGetRandom as new GetRandom myGetRandom.RetrieveDetails(lblQuestion.text, lblAnswers.text, lblViewCount.text, lblLastUpdated.text) End Sub Code behind Sub RetrieveDetails(lblQuestion as label, _ lblAnswers as label, _ lblViewCount as label, _ lblLastUpdated as...

BC30311: Value of type 'System.Web.UI.WebControls.RadioButtonList' cannot be converted to 'String'.
I have a listview that I'm working with.  Each record returned by my sql query is displayed in my list view as text, with the exception of one field, which I need to display as a radiobutton list.  I tried to set the value of the radiobutton in my codebehind, but keep getting errors.  How do I tell this radiobutton list that the value that should be selected when the page loads is equal to the value returned from my sql query?    The line in bold throws the error... How am I supposed to indicate what value to have selected when the page loads? <asp:Radi...

BC30311: Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.TextBox'.
 Hello everyone, I receive the error in the subject when viewing this page, I can get it to work fine without the master page and only the textboxes, but that causes an error with line 11, even when the page contents do process to the form. I don`t understand the error and I would appreciate any guidance on this. Thanks for reading.1 <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="Untitled Page" %> 2 3 <script runat="server"> 4 5 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) 6 Dim D...

Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.TextBox'
I have a new page (wireframe) based on the User_Create page in the Time Tracker starter kit.  I have no code in the code behind page.  What might be causing this error?  This newbie needs help. I had a TextBox control named "Title" which appearantly conflicted with the page title. thanks!!! ...

Cannot implicitely convert type 'string' to 'System.Web.UI.WebControls.Label'
 I'm starting to make a website for users to login. Once they login, I'm showing their profile. So I populate some asp:label controls on Page_Load. My profile page (shown after the user logs in) contains: <%@ Page Language="C#" MasterPageFile="~/site.master" CodeFile="clientProfile.aspx.cs" Inherits="clientProfile" AutoEventWireup="true" Title="Car Look - Your profile" %> <asp:Content ID="Content1" ContentPlaceHolderID="mainContent" Runat="Server"> <div> &l...

Cannot implicitly convert type 'System.Web.UI.WebControls.Label' to 'string'
I have a datagridLabel ID1 = (Label)(e.Item.FindControl("ID1")); Label Name1 = (Label)(e.Item.FindControl("Name1")); Label TableName = (Label)(e.Item.FindControl("TableName"));   No0w Iam trying to TBTableName.Text = lblTableName;                  tbid.Text = lblImageTypeID1;                   tbnameordes.Text = lblImageName1; I have tried l lbltablename.text,.tostring,convert.t...

Unable to cast object of type 'System.Web.UI.WebControls.Label' to type 'System.Web.UI.WebControls.TextBox'
I am attempting to "Update" a field in a Datagrid, but no matter what I do (Using either Label or Text Boxes)  I keep getting the same error: Unable to cast object of type 'System.Web.UI.WebControls.Label' to type 'System.Web.UI.WebControls.TextBox' The Error is highlighted below in the Sub... <==THE FIELD THAT CALLS THE ERROR  Any help would be greatly appreciated !!! Thanks !!   The code is as follows... <table border=0 height=210 bordercolor=olive ><tr valign=top><td> <asp:datagrid id="FacilityDataGrid"...

Error: Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.Label
Here is the code:Dim dato_start_2 as DateTime Dim varighet_str as string Dim dato_differanse as string dato_start_2 = tabell_2.Rows(DataGrid1.SelectedIndex).Item("dato_tid_sendt") dato_differanse = DateDiff(DateInterval.Day, dato_start_2, now) Label12 = "" & dato_differanse & ""  ........and theCompiler Error Message: BC30311: Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.Label'.Why and how?RegardsLars/Norway To set a text for a Label you need to use its Text property, in your code you try to set the Label to a Text and that is the reason why you g...

Cannot implicitly convert type 'System.Web.UI.WebControls.Label' to 'string' #2
Hi i am getting the following error "Cannot implicitly convert type 'System.Web.UI.WebControls.Label' to 'string'", what am i doing wrong, my code looks like this;protected void lbtnSubmit_Click(object sender, EventArgs e) {foreach(DataGridItem dgi in dgAvailability.Items) {AvailabilityBLC availabilityBLC = new AvailabilityBLC(); availabilityBLC.Availability.AvailabilityDay =(Label)dgi.FindControl("lblDay");availabilityBLC.Availability.AvailabilityPrice = (TextBox)dgi.FindControl("priceTxt"); availabilityBLC.Availability.BBooked = (CheckB...

BC30311: Value of type 'Control' cannot be converted to 'System.Web.UI.WebControls.CheckBox'
 Hi, can somebody help me on this problem. It works at first, but all of a sudden, it fails, and I can't find out why.    1 Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) 2 3 If Not CheckRequiredField() Then 4 MessageBox("Please complete the input data!") 5 Exit Sub 6 End If 7 8 Dim nRow As Integer 9 Dim Ctrl As Control 10 Dim chkDelete As Object 11 Dim LookupID, Name, Desc, Type As String 12 13 nRow = 0 14 1...

Error Serializing Value 'System.Web.UI.WebCOntrols.TreeNode' of type 'System.Web.UI.WebCOntrols.TreeNode'
Hi, I am  getting the error stated in the subject when i am storing a TreeNode in a ViewState and then using it as follows if (ViewState["ProductCounty"]!=null) { TreeNode _productCountyNode = DataTree.FindNode(((TreeNode)ViewState["ProductCounty"]).ValuePath); _productCountyNode.Expand(); } what could be causing this error? Hi, I believe TreeNode isn't serializable in a way its required for objects which can be put to ViewState - as reference - (TreeNode implements IStateManager and is meant to work together with TreeView itself saving it...

Error: Cannot convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.ButtonField'
Hello, I am getting the following error during compiliation. Any suggestions to fix it. Thanks. Cannot convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.ButtonField' protected void gvEvents_RowCommand(Object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Select") { int index = Convert.ToInt32(e.CommandArgument); GridViewRow selectedRow = gvEvents.Rows[index]; string sID; ButtonField btn; if (selectedRow.RowType == DataControlRowType.DataRow) { sID = ((ButtonField)selectedRow.FindControl("ID")).DataTextFi...

Unable to cast object of type 'System.Web.UI.WebControls.FormView' to type 'System.Web.UI.WebControls.DropDownList'.
Hi,  I have a formview with a dropdownlist created dynamically. i want to determine whether the dropdownlist was clicked. the code is written in the FormView1_DataBound method. I get the following error in the line below that is in italics and underlined: Unable to cast object of type 'System.Web.UI.WebControls.FormView' to type 'System.Web.UI.WebControls.DropDownList'.   Here is my code: ddl_iFullname = New DropDownList ddl_iFullname.ID = "ddl_iFullname" ddl_iFullname.DataSource = ObjectDataSource3 ddl_iFullname.DataTextField = "fullname" ddl_iFullname.DataValueField = "...

Value of type 'System.Web.UI.Control' cannot be converted to 'System.Web.SiteMap'.
 Hello,         I receive this error "System.Web.UI.Control' cannot be converted to 'System.Web.SiteMap" when I try to find the Sitemap control.  ...            <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />            <asp:ListView ID="ListView1" runat="server">            <LayoutTemplate>    ...

Web resources about - BC30311: Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.Label'. - asp.net.getting-started

Resources last updated: 11/22/2015 4:04:28 AM