What Is The Best Method Of Casting

May 20, 2010

Whats the best method of casting. I've been using syntax like this:

Button btnSubmit = (Button)item.FindControl("Btn_Submit")

but is this better or just different:

Button btnSubmit = item.FindControl("Btn_Submit") as Button

View 3 Replies


Similar Messages:

C# - Casting A Tiny Int From SQL Server?

Feb 17, 2011

I'm using linq to sql to populate a list of objects. One of the fields I'm interested in is stored as a tinyint. How should I declare this property type in my object definition? As a Short? Byte? Int16?

View 4 Replies

Casting Values SQL IN Function?

Nov 7, 2010

I have this problem where I am passing NVARCHAR parameter to a stored procedure, and the field type in the table for that parameter is INT.

I am doing that because I am using IN function, and for example if I want to send multiple parameters, that is the only possible way to do through .NET. Or not? The SP doesn't get executed because of the wrong type, and I am trying to cast the value, but I get an error. Here is how I am trying:
Example:

@Parameter nvarchar OrderStatusID = ('30, 40')

(o.OrderStatusID IN (CAST(@OrderStatusID as int)) OR @OrderStatusID IS NULL)

Is there a way to cast each value separately, or what will be the best way to cast whole value as INT.

View 2 Replies

Asp.net - Casting An Object To A Long In C#?

Jun 16, 2010

I am pulling a 'long' value from Session, using a generic property, and it is crashing. so I have:

public static T Get<T>(string key)
{
if(...)
return (T)System.Web.HttpContext.Current.Session[key];
...
}


When debugging, the value is 4, and it crashes.

View 3 Replies

C# - How To Handle ViewData Type Casting In MVC

Apr 21, 2010

i have a xml file and i am retrieving its value using Linq to xml and assigning it to ViewData.

[code]...

but foreach (var item in ViewData["Persons"] is giving type casting error..what should be the exact type csting so that i can retrive values in the format item.Fname.

View 3 Replies

Casting Request.Forms To String?

Mar 29, 2010

I'm calling a method like this:

DataTable dtModifiedData = detailsGrid.GenerateRowColumnDataTable(Request.Form)

The GenerateRowColumnDataTable method retrieves all form variables with <input> tags. My problem is that my columns are named in a strange format like this:

#X#_G1_NAME_1 so when I look at what's being rendered I see %23X%23_G1_NAME_1 so I thought I could use Server.URLEncode() around Request.Form but I'm getting an errot that it can't be converted to a string. What would you do in this case?

View 7 Replies

DataSource Controls :: Casting Bit Value From Table To Int?

Jul 28, 2010

I have a bit column that is saved as 1 or 0 in the database. I want to check the value in the table and then do an if statement depending if its 1 or 0. How can I convert it to an int?

i had

lockValue = (int)sqlCommand1.ExecuteScalar();


I want to assign it to the lockValue int variable. I then want to go on and check

if (lockValue == 1) {
// stuff
}

View 1 Replies

VS 2010 - Casting SiteMapNodeCollection To IEnumerable

Feb 19, 2013

Why it's not possible to cast a SiteMapNodeCollection (eg. SiteMap.CurrentNode.ChildNodes) to an IEnumerable<SiteMapNode>? I was wanting to do some filtering of the collection via LINQ, but this might not be possible?

The MSDN page says it implements IList, ICollection, and IEnumerable, so I don't see what the problem is.

View 4 Replies

Generic Casting To Access Text Property?

Sep 23, 2010

I have a situation where I'm iterating through all the controls on a form, specifically looking out for any of these three types of controls: Labels, TextBoxes, CheckBoxes

I can, of course, get the type of the control and then use a Switch-Case construct to cast labels as Labels, textboxes as TextBoxes, and checkboxes as CheckBoxes.

But I'm wondering if there's a way to do something with one or two lines of code to gain access to the Text property of each of these controls?

View 4 Replies

MVC :: IQueryable Casting To IOrderedQueryable Doesn't Work?

Nov 10, 2010

I am passed an IQueryable that might be already be ordered and that therefore might be an OrderedQueryable. I need to apply it a new ordering via reflection, anb before choosing if calling either OrderBy or ThenBy I need to now if the Queryable is IOrderedQueryable.

Now comes the problem! To see if may variable say query is an IOrderedQueryable I do:

IOrderedQueryable orderedQuery = query as IOrderedQueryable;

One might expect orderedQuery be null in case query is not an IOrderedQueryable ! INSTEAD orderedQuery is NEVER NULL. It takes the value of a different type!

I understand that this might happen because of deferred execution...

I tried also: query is IOrderedQueryable and it is always true!

How can I verify if query is actually an IOrderedQueryable before getting an exception when calling ThenBy?

The IQueryable comes from the Entity Framework 4.0

I am able to see it if I access the object that is behind the interface, but this way I would loose the benefif of using interface, and my class might not work with a different implementation of the IQueryable.

View 2 Replies

Casting A Request.Form Control From Code Behind?

Oct 15, 2010

Im dynamically generating an HTML Select Box, lets call it myselect. Im creating this select box based on some database values Im generating an HTML Select Box.. almost like a string that Im just spitting out to the page. So it's never a control in the codebehind, just part of a string thats rendered as HTML by the browser. If I submit my form, and in my codebehind I perform:

Dim myVal as String = Request.Form("myselect")

That code will give me the VALUE of the myselect select box. How can I refer to this control to cast it as a System.Web.UI.HtmlControls.HtmlSelect control? Request.Form seems to give me the value, but I want to reference the object itself..

View 3 Replies

Casting - Cast MembershipUser To Custom Class (ASP.NET)?

Oct 15, 2010

I'm using the default SqlMembershipProvider,but I've created a custom MembershipUser class (SoeMembershipUser) because I needed a "DisplayName" property.All the DisplayName does is look at the UserName and format it differently.When I try to cast a MembershipUser to a SoeMembershipUser user I get an InvalidCastException.

Exact error is:
"Unable to cast object of type 'System.Web.Security.MembershipUser' to type 'Soe.Tracker.SoeMembershipUser'."

Here is the code that fails:

SoeMembershipUser user = (SoeMembershipUser)Membership.GetUser(username); // invalid cast

I have also tried casting later like so:

MembershipUser user = Membershipship.GetUser(username); // ok
...
string displayName = ((SoeMembershipUser)user).DisplayName; // invalid cast

Here is the SoeMembershipUser class:

NOTE: I left off the constructors at first, but added them in later when I started having problems. Adding them made no difference.

public class SoeMembershipUser : MembershipUser
{
public SoeMembershipUser()
: base()

[code]...

View 2 Replies

C# - Storing Session Id As A String And Casting It Back To GUID?

Jul 19, 2010

I´m trying to use session to store a value (id). The problem is that I have to store it as a string. When trying to use the instance of the id I get the error:

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:

Line 156:
Line 157: Nemanet_Navigation newFile = new Nemanet_Navigation();
Line 158: newFile.Nav_pID = (Guid)Session["id"];
Line 159:
Line 160:

This is where I get the id and that seems to work fine. Session["id"] gets the value.

[code].....

View 3 Replies

VS 2010 - Find Control And Casting Results In NullReferenceException

Apr 16, 2012

I'm using a third party control called an Accordion from Juice UI. Within that control, I have a table with labels.

VB.NET Code:
<juice:Accordion ID="accSwrlHddRepl" runat="server"> 
<juice:AccordionPanel runat="server" Title="HDD Information"> 
<PanelContent> <p> <table style="width: 99%;">
<tr> <td class="style2">
<asp:Label ID="Label2111111" runat="server" Text="Original HDD:"></asp:Label>   

[Code] ....

I can't access the label directly, so I'm trying to use the FindControl method to get it and set it's text property.

VB.NET Code:

CType(Me.accSwrlHddRepl.FindControl("lblFirstHddReplacement"), System.Web.UI.WebControls.Label).Text = "string"

But I get a NullReferenceException. So I moved the label outside of the table and put it just inside the accordion. The same behavior occurs. Then I gave the accordion panel an ID and did FindControl there, but the same issue occurs.

View 9 Replies

How To Enable/disable Controls Against A User's Permission Via Type Casting

Jun 2, 2010

I want to enable/disable controls of a asp.net page against a logged in user's permission. say 'admin' & 'hr' can change user's birth date text-box in a page but others will see the text-box as disabled.

so i wrote a function like this

[code]....

View 3 Replies

Forms Data Controls :: Casting Gridview Datasource To Datatable?

Mar 9, 2011

i try casting the gridview datasource to datatable using the following code but the datatable always nothing.

Dim DT As New DataTable
DT = DirectCast(gv.DataSource, DataTable)

the gridview is bound using dataset at code behind without using sqldatasource.

i m trying to filter the gridview without geting the data from database, is there is another way to do this?

View 4 Replies

State Management :: Saving And Casting A List Of Date In Session?

May 18, 2010

I am trying to save the every date clicked in a List of Date using a session variable. I am getting a casting error ..."when casting a number the number must be less than infinity".

[Code]....

View 1 Replies

Forms Data Controls :: Casting From Generic IList To Object During DataBind?

Feb 18, 2010

Okay, so I have a Generic IList of custom objects that I want to use as a datasource for a repeater.

[Code]....

In the WebForm inside the repeater, I place this code:

[Code]....

when I try to run it, I get the error "Unable to cast object of type 'System.Collections.Generic.List`1[MyCustomObject]' to type'MyCustomObject'. ". Which makes sense, really.

What's the best way round this? Is there any way I can get the direct cast working? I'm loath to have to create a custom list item that implements IBinding or something and bind that directly, as that'll mean a lot of legacy changes which may create further bugs. Or is there a better way?

View 3 Replies

Forms Data Controls :: ListView - ItemDataBound Event - Casting To DataRowView?

Oct 25, 2010

System.InvalidCastException was unhandled by user code

View 4 Replies

State Management :: Casting Session Object To Generic List Fails?

Feb 27, 2011

i'm having a gridview with textboxes and i'm allowing user to enter all the textboxes and when submit button is clicked, I'm constructing the Object and adding all the rows to LIST class.,After that i'm putting that LIST elements in a session. Everything is fine, But when i try to cast the session object to LIST type and pass it to XML serialization, I cannot see the elements in the list and it is showing LIST as NULL. I can see the generated XML string with ROOT and my classname as attributes without no data.

Here is the below code:

[Code]....

View 16 Replies

Forms Data Controls :: Casting Object In DetailsView Template Table?

Jul 22, 2010

I use the DetailsView control to display selected employee's data for HR. The list of data elements has gotten pretty long so I thought I'd put some tables & panels inside the ItemTemplate and EditTemplate. Testing with NO code behind in effect works perfectly on display & update. Now I need to manipulate some stuff in the code behind which requires me to cast the controls I'm after. However old cast code doesn't work (it says the row index doesn't exist).

First is a clip of the DetailsView which gives me 3 nice columns.

[Code]....

View 2 Replies

Error While Adding Dynamic Data To An Existing Web Site - The Method 'Skip' Is Only Supported For Sorted Input In LINQ To Entities. The Method 'OrderBy' Must Be Called Before The Method 'Skip'.

Apr 13, 2010

I am creating an Asp.net web site which will support dynamic data. When I am creating a dynamic web site from Scratch (from template in VS) all is working fine. But when I am trying to add dynamic entity (.edmx) file and running the application I am getting following error

"The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'. "

View 2 Replies

C# - WebService Request Type Casting / Cannot Convert The BOLibrary.Flight.DTContract To DTContract

Mar 31, 2011

I have a scenario where i have to pass the array list to the WebService.

WebService:

[WebMethod]
public void GetCommission(List<BOLibrary.Flight.DTContract> Loc)
{
CommissionManager test = new CommissionManager();
}

Client:

List<BOLibrary.Flight.DTContract> BoList = new List<BOLibrary.Flight.DTContract>();
BOLibrary.Flight.DTContract dtConboj = new BOLibrary.Flight.DTContract();
dtConboj.ValidatingCarrier = "AA";
DTContract[] loc1 = BoList .ToArray();
service.GetCommission(loc1);

when i am trying to do this i am getting the exception that cannot convert the BOLibrary.Flight.DTContract to DTContract This is because when webservice create proxey consider Type(DTContract) not namespace(BOLibrary.Flight.DTContract)and i have to pass the list or arraylist of BOLibrary.Flight.DTContract Type.

View 1 Replies

Web Forms :: Implicit Type Casting From "obj" To String Failing In 2.0?

Jun 25, 2010

I am using the following code to convert any valid object, to a date time:

[Code]....

When i call this mehod from my web pages, using a blank("") value in TextBox.Text property as a parameter, the method is throwing an FormatException exception.

[Code]....

Ironically, the same method does not throw any exception with the same input when tried in a different machine.

Are there any regional setting that need to be modified in .NET 2.0 for this to work ?

P.S:

The method also works when called using a string variable, like

[Code]....

[Code]....

View 4 Replies

Forms Data Controls :: When Casting From A Number The Value Must Be A Number Less Than Infinity

Mar 10, 2011

I get this message when i go to update formview.

View 2 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved