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"
runat="server"
Font-Names="Arial"
Font-Size="11px"
HorizontalAlign="Left"
ItemStyle-Font-Size="x-small"
HeaderStyle-Font-Bold="True"
HeaderStyle-Font-Size="x-small"
AlternatingItemStyle-BackColor="#FFFFFF"
Cellpadding="0"
BorderWidth="0"
AutoGenerateColumns="False"
BorderStyle="None" GridLines="None" BorderColor="#404040" Width="75%"
DataKeyField="FacilityID"
OnUpdateCommand="MyDataGrid_UpdateCommand"
OnCancelCommand="MyDataGrid_CancelCommand"
OnEditCommand="DG_Combo_Edit">

<AlternatingItemStyle BackColor="#FFFFFF"></AlternatingItemStyle>
<ItemStyle Font-Size="X-Small"></ItemStyle>
<HeaderStyle Font-Size="X-Small" Font-Bold="True" ForeColor="White" BackColor="White"></HeaderStyle>
<Columns>

<asp:TemplateColumn HeaderStyle-VerticalAlign="Top">

<HeaderTemplate></HeaderTemplate>

<ItemTemplate>

<table Align=Left border="0" bordercolor=red Cellpadding="0" Cellspacing="0" Width="850" style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Verdana, Arial, sans-serif">
<tr><td align=left colspan="9"></td>
</tr>

<tr valign="top">
<td align=left colspan="9"><asp:label id="Label1" runat="server"></asp:label></td>
</tr>

 

<tr valign="top">
<td width= 25 align=left><asp:LinkButton runat="server" width= 20 CommandName='Edit' id="Button5" Text="Edit" /></td>
<td width= 25 align=left><b><asp:label Runat="server" width= 25 ID="FacilityID" Text='<%# Container.DataItem("FacilityID") %>' ReadOnly="true" ForeColor="#000000" Font-Bold="true"/></b></td>
<td width= 250 align=left><b><asp:label Runat="server" width= 250 ID="Laboratory" Text='<%# Container.DataItem("Laboratory") %>' ReadOnly="true" ForeColor="#000000" Font-Bold="true"/></b></td>
<td width= 140 align=left><b><asp:label Runat="server" width= 140 ID="Lab_Contact" Text='<%# Container.DataItem("Lab_Contact") %>' ReadOnly="true" ForeColor="#000000" Font-Bold="true"/></b></td>
<td width= 120 align=left><b><asp:label Runat="server" width= 120 ID="Contact_Telephone" Text='<%# Container.DataItem("Contact_Telephone") %>' ReadOnly="true" ForeColor="#000000" Font-Bold="true"/></b></td>
<td width= 120 align=left><b><asp:label Runat="server" width= 120 ID="Lab_Fax" Text='<%# Container.DataItem("Lab_Fax") %>' ReadOnly="true" ForeColor="#000000" Font-Bold="true"/></b></td>
</tr>
</table>

</ItemTemplate>

<EditItemTemplate>

<table Align=Left border="0" bordercolor=orange Cellpadding="0" Cellspacing="0" Width="600" style="FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Verdana, Arial, sans-serif">
<tr>
<td></td>
</tr>

<tr valign="top">
<td align=left colspan="9"><asp:label id="Label1" runat="server"></asp:label></td>
</tr>

<tr><td></td>
</
tr>

<tr valign="top">
<td width= 60 align=left></td><asp:LinkButton runat="server" width= 60 CommandName='Update' id="Button6" Text="Update" /></td>
<td width= 1 align=left>&nbsp;|&nbsp;</td>
<td width= 60 align=left><asp:LinkButton runat="server" width= 60 CommandName='Cancel' id="Button7" Text="Cancel" ForeColor="Red" /></td>
<td width= 3 align=left>&nbsp;</td>
<td width= 50 align=left><b><asp:TextBox Runat="server" Width="20" ID="FacilityID" Text='<%# Container.DataItem("FacilityID") %>' ReadOnly="true" Font-Bold="true"/></b></td>
<td width= 250 align=left><b><asp:TextBox Runat="server" Width="250" ID="Laboratory" Text='<%# Container.DataItem("Laboratory") %>' Font-Bold="true"/></b></td>
<td width= 140 align=left><b><asp:TextBox Runat="server" Width="150" ID="Lab_Contact" Text='<%# Container.DataItem("Lab_Contact") %>' Font-Bold="true"/></b></td>
<td width= 100 align=left><b><asp:TextBox Runat="server" Width="100" ID="Contact_Telephone" Text='<%# Container.DataItem("Contact_Telephone") %>' Font-Bold="true"/></b></td>
<td width= 100 align=left><b><asp:TextBox Runat="server" Width="100" ID="Lab_Fax" Text='<%# Container.DataItem("Lab_Fax") %>' Font-Bold="true"/></b></td>
</tr>
</table>

</EditItemTemplate>

</asp:TemplateColumn>

</Columns>

</asp:datagrid>

</td></tr></table>
</td></tr></table>

 

The Code Behind Looks like:

Sub myDataGrid_UpdateCommand(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)

If Session("UserName") = "" Then

Response.Redirect("LogOnSessionExpired.aspx")

Else

Dim objConn99 As SqlConnectionDim mySettings99 As New NameValueCollection

mySettings99 = AppSettings

Dim strConn As String

strConn = mySettings99("connString")

objConn99 = New SqlConnection(strConn)

'Dim objCmd As SqlCommand

Dim dgi As DataGridItem

For Each dgi In FacilityDataGrid.Items

Dim FacilityID As String = CType(dgi.FindControl("FacilityID"), TextBox).Text <===== THE FIELD THAT CALLS THE ERROR

Dim Laboratory As String = CType(dgi.FindControl("Laboratory"), TextBox).Text

Dim Lab_Contact As String = CType(dgi.FindControl("Lab_Contact"), TextBox).Text

Dim Contact_Telephone As String = CType(dgi.FindControl("Contact_Telephone"), TextBox).Text

Dim Lab_Fax As String = CType(dgi.FindControl("Lab_Fax"), TextBox).Text

Dim strSQL As String = "Update ClinicalFacilityName SET " & _

"Laboratory = @Laboratory, " & _

"Lab_Contact = @Lab_Contact, " & _

"Contact_Telephone = @Contact_Telephone, " & _

"Lab_Fax = @Lab_Fax " & _

"WHERE FacilityID = @FacilityID"

Dim myCommand As New SqlCommand(strSQL, objConn99)

myCommand.Parameters.AddWithValue("@FacilityID", FacilityID)

myCommand.Parameters.AddWithValue("@Laboratory", Laboratory)

myCommand.Parameters.AddWithValue("@Contact_Telephone", Contact_Telephone)

myCommand.Parameters.AddWithValue("@Lab_Contact", Lab_Contact)myCommand.Parameters.AddWithValue("@Lab_Fax", Lab_Fax)

objConn99.Open()

myCommand.ExecuteNonQuery()

Next

objConn99.Close()

ErrorLabel1.Text = "Facility Record Updated!"

End If

End Sub

 

1
SmokinJoe
2/25/2008 1:35:55 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

3 Replies
3999 Views

Similar Articles

[PageSpeed] 8

you have two controls with the same id...you have the textbox and label

<asp:label Runat="server" width= 25 ID="FacilityID" Text='<%# Container.DataItem("FacilityID") %>' ReadOnly="true" ForeColor="#000000" Font-Bold="true"/>

<asp:TextBox Runat="server" Width="20" ID="FacilityID" Text='<%# Container.DataItem("FacilityID") %>' ReadOnly="true" Font-Bold="true"/>


0
Curt_C
2/25/2008 1:43:25 PM

That must have happend when I was playing around.. but even if I change and make them both the same... TextBox or Label.. I still get the same error, just reverse!!

Really strange?

0
SmokinJoe
2/25/2008 2:00:50 PM

the point is that they both have the same id.... change one of them.

You can't have the same id used twice..... (with the exception of within a repeater, etc)....

Just change one of them and you are fine...


0
Curt_C
2/25/2008 2:40:10 PM
Reply:

Similar Artilces:

Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'.
Hi, any idea why I'm getting the following error?Line 45: protected void CartGrid_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e) Line 46: { Line 47: TextBox QuantityTextBox = (TextBox)CartGrid.Rows[e.RowIndex].Cells[2].Controls[0]; Line 48: int Quantity = Convert.ToInt32(QuantityTextBox.Text); Line 49: Are you sure that the control at CartGrid.Rows[e.RowIndex].Cells[2].Controls[0] isn't a LiteralControl?  How are you creating code this in your code?Brent JenkinsASP.NET, C#, Web Developerwww.valewebdesign.co.uk  ...

Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'
Hello, Goal: enter a number in two fields HoursPerUnit and PeoplePerUnit and the value for the third field, TotalHoursPerUnit will populate after the the number in the two fields have been caculated.  Using the code below I receive the error message above.  Please tell me what am I doing wrong? Do I need to cast the text as a literal?  Please help? Thank you in advance, <%@ Page Language="VB" MasterPageFile="Default.master" AutoEventWireup="false" CodeFile="frmHR.aspx.vb" Inherits="frmHR"...

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 = "...

Error showing up: Unable to cast object of type 'System.Web.UI.WebControls.TextBox' to type 'System.IConvertible'.
Hi, I am trying to pass date values from two textboxes in a stored procedure from ASP.net page. However, it seems it is not passing the date properly. I am getting the following error at the following line: cmd_r.Parameters("@initial_est_date").Value = Convert.ToDateTime(Textbox1)  and the error is as stated in the title. I would appreciate any help to resolve this. Thanks  CODE:<%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %><%@ Import Namespace="System.Data.Common" %> <%@ I...

Unable to cast object of type 'System.Int32' to type 'System.Web.UI.WebControls.DataKey'
hi , i have a datalist with an itemtemplate section that contains an image and a delete button to delete the corresponding image . <asp:DataList ID="DataList1" CellSpacing="25" runat="server" DataSourceID="sqldatasource3" DataKeyField="ID" repeatColumns="4" repeatdirection="Horizontal" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="0px" CellPadding="3" > <ItemTemplate> <table border="0" cellpadding="0" cellspacing="0" > <tr> <a href='<%# DataBinder.Eval(Container.DataItem, "image", "ImageViewer.aspx?id={0}") %>' > <img a...

Unable to cast object of type 'System.EventArgs' to type 'System.Web.UI.WebControls.GridViewRowEventArgs'
hi this is really really bugging me please help. I have a web appliction that uses a Master Page. On one of the content pages I am presenting a gridview control bound to a spreadsheet. The gridview is contained within an ajax update panel. I want to add attributes to the rows of the gridview control once databound (mouseover effect). However I am getting the following error:- Unable to cast object of type 'System.EventArgs' to type 'System.Web.UI.WebControls.GridViewRowEventArgs' Can anyone help please. Here is the aspx code:-  <%@ Page Language="v...

Unable to cast object of type 'System.String' to type 'System.Web.UI.WebControls.Parameter'.
I'm getting this error on a vb.net page the needs to execute two separate stored procedures.  The first one, is the main insert, and returns the identity value for the ClientID.  The second stored procedure inserts data, but needs to insert the ClientID returned in the first stored procedure.  What am I doing wrong with including the identity value "ClientID" in the second stored procedure?  Unable to cast object of type 'System.String' to type 'System.Web.UI.WebControls.Parameter'. Description: An unhandled exception occurred during t...

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...

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...

Unable to cast object of type 'System.Web.UI.WebControls.ContentPlaceHolder' to type 'CustomerDB._Default'.
If have  a problem regarding the error message: Unable to cast object of type 'System.Web.UI.WebControls.ContentPlaceHolder' to type 'CustomerDB._Default'. I am using MaterPages with two ContentPlaceHolders in my default.aspx In the fist ContentPlaceHolder I have added a ordinary placeholder where I loads a Usercontrol. So far so good.The usercontrols search for customers and creates a gridview with a link that  the user can click on. When the user clicks it runs a class (located in default.aspx) that should present data in the second ContentPlaceholder, ...

ASPNET2 HELP! CS0266: Cannot implicitly convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.FileUpload'.
Server Error in '/start-movieposters' Application. 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: CS0266: Cannot implicitly convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.FileUpload'. An explicit conversion exists (are you missing a cast?) Source Error: ...

The base class includes the field 'MyList', but its type (System.Web.UI.WebControls.DataList) is not compatible with the type of control (System.Web.UI.WebControls.DataGrid).
Hi guys,I am very new to asp.net and have been playing with the commerce starter kit at - http://asp.net/CommerceStarterKit/ one of the first pages that lists the catalogue items "ProductsList.aspx" uses a datalist, I need to use a datagrid as I need to use pagination as my catalogue is so huge, I have changed the necessary attributes in the ProductsList.aspx page to datagrid and also in the codebehind file ProductsList.aspx.vb, when I run the page I get the Error message below. Description: An error occurred during the parsing of a resource required to ser...

The base class includes the field 'MyList', but its type (System.Web.UI.WebControls.DataList) is not compatible with the type of control (System.Web.UI.WebControls.DataGrid).
Hi guys,I am very new to asp.net and have been playing with the commerce starter kit at - http://asp.net/CommerceStarterKit/ one of the first pages that lists the catalogue items "ProductsList.aspx" uses a datalist, I need to use a datagrid as I need to use pagination as my catalogue is so huge, I have changed the necessary attributes in the ProductsList.aspx page to datagrid and also in the codebehind file ProductsList.aspx.vb, when I run the page I get the Error message below. Description: An error occurred during the parsing of a resource required to ser...

Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'x_it.Controls.NewsDetail'.
I have dynamicly filled a Panel with my usercontrols, I have no problem. But when I try to get in a for loop to read the data of my usercontrol, I get an error. Can somebody explain me what I'am doing wrong ?   for (int i = 0; i < PnlNewsDetails.Controls.Count; i++) { x_it.Controls.NewsDetail nd = (x_it.Controls.NewsDetail)(PnlNewsDetails.Controls[i]); if (Request.RequestType == "POST" && !nd.ErrorInput) { tbNews tn = new tbNews(); tn.Date = nd.Date; tn.Title = nd.Title; tn.SeoUrl = nd.SeoUrl; ...

Error 1 The type 'System.Web.UI.WebControls.LinqDataSource' exists in both 'c:\WINDOWS\assembly\GAC_MSIL\System.Web.Extensions\3.5.0.0__31bf3856ad364e35\System.Web.Extensions.dll' and 'c:\WINDOWS\asse
Once I build project. I encounter this error. Does someone experience this error?Error    1    The type 'System.Web.UI.WebControls.LinqDataSource' exists in both 'c:\WINDOWS\assembly\GAC_MSIL\System.Web.Extensions\3.5.0.0__31bf3856ad364e35\System.Web.Extensions.dll' and 'c:\WINDOWS\assembly\GAC_MSIL\System.Web.Extensions\3.6.0.0__31bf3856ad364e35\System.Web.Extensions.dll'    D:\Source Code\Linq_Lesson5\Default.aspx    1    1    D:\Source Code\Linq_Lesson5\  It appears tha...

Operator '&' is not defined for types 'String' and 'System.Web.UI.WebControls.TextBox'
Can anyone tell me what this error message means, and what to do to correct it.  Here is the code that has the error:  Dim sql As Stringsql = "INSERT INTO tblConnecting (Date, Member, Name, Company, Phone, Fax, Address, NamesofAttendees, PaymentType, Email) " _ & "Values('" & Today() & "', '" & blnMember & "', '" & Me.txtName.Text & "', '" & Me.txtCompany.Text & "', '" _& Me.txtPhone.Text & "', '" & Me.txtFax.Text &...

Unable to cast object of type 'System.Web.Compilation.BuildResultCompiledAssembly' to type 'System.Web.Compilation.BuildResultCompiledGlobalAsaxType'
I always get the below exception if I make a configuration change to the web.config file then request a page without re-building the project. Exception: Unable to cast object of type 'System.Web.Compilation.BuildResultCompiledAssembly' to type 'System.Web.Compilation.BuildResultCompiledGlobalAsaxType'. Stack Trace: [InvalidCastException]: Unable to cast object of type 'System.Web.Compilation.BuildResultCompiledAssembly' to type 'System.Web.Compilation.BuildResultCompiledGlobalAsaxType'.   at System.Web.Compilation.ApplicationBuildProvider.GetGlobalAsaxBuildResult(Boolean isPrec...

Unable to cast object of type 'System.Web.Configuration.ScriptingScriptResourceHandlerSection' to type 'System.Web.Configuration.ScriptingScriptResourceHandlerSection'.
Hello, I get this lovely error whenever I attempt to load an AJAXToolKit ScriptManager. It is the only one I have on the page. If I use the regulare script manager, my extenders don't detect it. I'm placing the ScriptManager just after the <Form> tag. The frustrating part is that it works when I preview it in the browser from my development machine it works, but once I place it the live server, it crashes on me . I've installed all of the .dlls in the proper places. Also, I'm trying to attempt to do this when creating a new site with the AJAX ToolKit enabled templat...

Unable to cast object of type 'System.Web.Configuration.ScriptingScriptResourceHandlerSection' to type 'System.Web.Configuration.ScriptingScriptResourceHandlerSection'.
Hello, I get this lovely error whenever I attempt to load an AJAXToolKit ScriptManager. It is the only one I have on the page. If I use the regulare script manager, my extenders don't detect it. I'm placing the ScriptManager just after the <Form> tag. The frustrating part is that it works when I preview it in the browser from my development machine it works, but once I place it the live server, it crashes on me . I've installed all of the .dlls in the proper places. Also, I'm trying to attempt to do this when creating a new site with the AJAX ToolKit enabled templat...

Unable to cast object of type 'System.Web.Configuration.ScriptingAuthenticationServiceSection' to type 'System.Web.Configuration.ScriptingAuthenticationServiceSection'.
I have tried to upgrade an existing application from VS2005 to VS2008.  I downloaded the latest AjaxControlToolkit and referenced it in my project.  I'm getting the below error when I try to deploy to our QA servers.  I'm NOT seeing the issue on our local development machines that have VS2008 installed.   Unable to cast object of type 'System.Web.Configuration.ScriptingAuthenticationServiceSection' to type 'System.Web.Configuration.ScriptingAuthenticationServiceSection'.  What piece am I missing in the deployment?  All the w...

Unable to cast object of type 'System.Web.UI.Page' to type 'CareAlways.UserControls.PageBase'
I have a project that has custom controls developed under ASP.NET 1.1, andI've successfully migrated to ASP.NET 2.0.  The format of the folder structure is as follows: /Core    /Business    /UserControls       MyTextBox.cs       PageBase.cs/WebApps   /MyWebApp  I have resolved all the problems to get the project to compile and run. My problem is that there are a few of my user controls that throw an error in the design mode of VS.NET 2005.  I get the following mess...

Help Required
Server Error in '/' Application.--------------------------------------------------------------------------------Unable to cast object of type 'System.Web.Compilation.BuildResultCompiledAssembly' to type 'System.Web.Util.ITypedWebObjectFactory'. [InvalidCastException: Unable to cast object of type 'System.Web.Compilation.BuildResultCompiledAssembly' to type 'System.Web.Util.ITypedWebObjectFactory'.] System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean noAssert) +123 System.Web.Compilation.BuildMa...

Error 60 Operator '&' is not defined for types 'System.Web.UI.WebControls.TextBox' and 'String'.
The comment control is a textbox:<td align="right" style="width: 228px"><asp:TextBox ID="comment" runat="server" TextMode="multiLine" Rows="10" Columns="50" onkeyup="return ismaxlength(this)" Width="260px" Height="100px" Font-Overline="false" /></td> and I'm being told by VS (underlined in blue ) that the for the comment field: Error 60 Operator '&' is not defined for types 'System.Web.UI.WebControls.TextBox' and 'String...

System.InvalidCastException: Unable to cast object of type 'System.Web.HttpInputStream' to type 'System.IO.FileStream'
I am trying to create a webpage that takes a file that someone want to upload and chop the file into smaller pieces, and then put back together again.  I have been trying to convert some code I found on Codeproject (FileSplitMerge).  They did it with VB which is what I want to use but it is a project not a web site so...  The problem that I am running into is I keep getting an error when I try to set: FileStream = HttpPostedFile.InputStream (I am using variables in my code) but I keep getting an error of: System.InvalidCastException: Unable to cast object of type 'Sys...

Web resources about - Unable to cast object of type 'System.Web.UI.WebControls.Label' to type 'System.Web.UI.WebControls.TextBox' - asp.net.presentation-controls

Resources last updated: 2/16/2016 8:48:38 AM