ADO.NET :: How To Handle Null While Converting From My Datatable To List Using LINQ

Aug 27, 2010

I am converting my datatable to LISt using LINQ how do I handle nulls coming from database

List<Port> portDetails = new List<Port>();
DataTable dt = ds.Tables[0];
portDetails = (from q in dt.AsEnumerable()
select new Port
{
PortCode = q.Field<string>("Code"),
ExtCode = q.Field<string>("Nb"),
Name = q.Field<string>("Name"))
}).ToList();

In the above query if Code is null I do not want property portcode to be set to the value it should only set if it is not null or not blank
PortCode = q.Field<string>("Code"),

What should be syntax
I was trying somethign like this which doesnt work
Portcode = q.Field<bool>("Code") == null ? null : q.Field<bool>("Code")

View 1 Replies


Similar Messages:

Data Controls :: Handle NULL Values When Converting DataTable Cell Value To String In C#?

Dec 23, 2015

How to allow null value on my program?

for below statement, "WARNING_TYPE" IS NULL..

but when I run, it promp me error..what are the correct statement should i write. tq all

l_Connection.ConnectionString = constr
l_Command.Connection = l_Connection
l_Command.CommandType = CommandType.Text
l_Command.CommandText = " SELECT UNP.EMPLOYEE_ID, UNP.UNPAID_DAYS, ABST.ABSENT_DAYS, WARN.WARNING_TYPE " +

[code].....

View 1 Replies

ADO.NET :: LINQ Over DataTable Null Decimal Field ?

Feb 9, 2011

I'm having a spot of trouble figure out how to write my linq query so that is allows a decimal field in my datatable to be null

Right now I have this:

[Code]....

But it's not working. The error I am getting is: Cannot cast DBNull.Value to type 'System.Decimal'. Please use a nullable type.so how do I get it to allow program_ar_id to be null?

View 2 Replies

How To Extract Datatable Or DataSet From Linq Query Or List

Jun 13, 2010

how to extract DataTable or DataSet from Linq Query or List. e.g I have linq query like this

MyDBpDataContext dcSp = new MyDBpDataContext();
dcSp.myProgrammSP().ToList();

I wrote an Sp which sends a Table as result and I have code which is already using DataTable So I want to convert this result in DataTable.

View 1 Replies

Can Handle Null Value In Linq / Object Reference Not Set To An Instance Of An Object

May 10, 2010

[code]....

this code give that solution.but if any one of the field have null(example:r.urrQuickLinkRights contain the NULL) then only it will throw error like Object reference not set to an instance of an object.

View 2 Replies

ADO.NET :: Converting Datatable Data To Arraylist?

Sep 13, 2010

how to convert datatable data to arraylist

View 1 Replies

WCF / ASMX :: Converting BusinessEntity List To Datacontract List?

Jan 28, 2011

I have a wcf service.In many places I am converting business entity list to datacontract list.

So I am trying to write a generalised method.

I have acheived to convert business entities with out list.

But when we give a list of business entity,it is getting failed.

to convert list of Business Entity to List of Datacontracts.

View 5 Replies

SQL Server :: Converting Null To Blank?

Feb 25, 2011

Is there a way to convert nulls in all the fields in a table into blanks using transact sql? For example strings will be "", numbers will be 0, and dates will be "1/1/1901".

I have over 500 columns, I don't want to go over each column and change the default value.

View 8 Replies

How To Handle Null Values

Apr 13, 2010

I want to know how to handle null values....

for eg i hav a datetime field called Completion date which temporarily has a null value and when i display the records using a gridview it displays a default date...how do i get rid of this default date.the completion Date gets entered at a later stage in the application following which the gridview should show this date....but initially it shouldn't show anything

View 3 Replies

MVC :: How To Handle Null {id} On Route

Mar 11, 2010

What if a user hits my site with [URL]t rather than [URL] In other words, they do not specify a value for {id}. If they do not, I want to display a nice "Not Found" page, since they did not give an ID.Is there a more standard MVC framework way of handling this, rather than the code I presently use (see below), where I test accept a nullable int? and then for null? Surely there is a smoother way to handle this.

[Code]....

View 2 Replies

C# - How To Handle The Hashtable Null Value

Aug 12, 2010

I have a problem while checking a hashtable value. In the code below I am storing 60 values in the hashtable "hash". One of the values is (or can be) Null. I am checking each entry against its corresponding entry in hashtable "hash1", to see if they match each other. If they don't match I want to check if the value in "hash" is Null, but I can't catch it - it's falling through the 'else' part). How can I overcome this problem?

[code]....

View 3 Replies

ADO.NET :: Converting SQL Queries To Linq?

Jul 31, 2010

How to convert SQL Queries to Linq.is their any tool available to convert SQL Queries to Linq

View 2 Replies

What Is Standard Way To Handle Null Dates In C#

Mar 15, 2011

I have asp.net form with C#, where is I am taking user information to insert in the database as usual by using Linq. well. Where as I am taking Date of birth also from the user, but if user skip to fill date text box from ui, then I am getting date like '01/01/0001' something like this, which certainly database security would not allow to store it.

So I need to check somewhere in my code that it is null or in this (above given) format. If it is null or in format '01/01/0001' then what exactly I have to do? I don't have any default
value for dates.

So what is the standard way to handle if date is null (but not mandatory). So many times I found myself in trap while handling null for various types.

[code]....

View 6 Replies

DataSource Controls :: How To Handle Null Value Between Sql And C#

Feb 3, 2010

I need to retrive a value from a column of a table per stored proc. This is usually an integer, but also can be null. I get this value and store it in a session variable.

Later, I need to pass this same session variable to another stored proc.

Session["myVariable"] = ds.tables[0].rows[0][1]; // this can be null or an integer

somewhere else in the project, I need to pass it to another stored proc exactly as received.

[code]....

View 4 Replies

ADO.NET :: Converting XML Dataset To Be Enumerable For Linq?

Mar 21, 2011

The following code works fine for converting an XML data set to be enumerable for Linq.

'Query webservice for data
Dim propertyInfo As String = myService.GetProperty(userName, password, Acct)
' Create a new DataSet.

[code]...

However, it does not retain the xml tags in the Linq enumerablerable dataset ('newDataSet2"). I don't think I can rely on the array index order as being the same everytime, so this would obviously cause a problem if I'm trying to align the array items up to my database fields. How can I make newDataSet2 retain the XML tags (column headings) to insure I know what data field each array item belongs to?

View 1 Replies

Forms Data Controls :: Getting Null Columns While Converting Dataset To XML?

Apr 12, 2010

I have a dataset.table which has some null values. am trying to trasform this using xslt to display like excel. If the value is null, then that node is ommitted by the xml. i tried deafulting the null values from the database itself and thats not generic. Handling null values is troublesome. I tried couple of methods to default null values by looping and thats not optimal solution. I used LINQ to handle null values but couldnt comeup with a neat solution. what am looking for is a generic optimal solution to handle these values.

View 2 Replies

C# - Handle A Null Object In Code Block?

Feb 22, 2010

Given the following simplified code block in ASP.NET

<% foreach( item in Model) { %>
<%=item.OriginalText.OriginalText1 %>
<%} %>

OriginalText is a class which may be null. I am looking for a simple (clean) way to check for this null and return an empty string ("") instead.

View 6 Replies

DataSource Controls :: How To Handle Null DateTime

Feb 26, 2010

I get a recordset returned from DB and try to load it. However sometimes field "LastRun" may be empty (NULL). when i have this it compiles fine but errors out when value is null m_LastRun = Convert.ToDateTime(r["LastRun"]); when I take out Convert.ToDateTime it doesn't even compile.

View 3 Replies

C# - How To Handle Null Return From Custom HttpHandler

Jun 8, 2010

I'm using a custom ashx HttpHandler to retrieve gif images from a database and show it on a website - when the image exists, it works great.

However, there are cases when the image will not exist, and I'd like to have the html table holding the image to become invisible so the "image not found" icon is not shown.

[code]...

View 4 Replies

ADO.NET :: Converting Date To String And Formatting It Linq To SQL?

Aug 2, 2010

I have problem in converting date to string I am using cstr(p.tripstart) , which is returning Date as 12 Aug 2010 12:AM I need to make formating This is my code :

[Code]....

View 9 Replies

How To Handle Null Values In Stored Procedure With Pivot

Oct 30, 2010

I am using this stored procedure with pivot.If i dont have data i am getting null with this stored procedure.Can u tell me how to handle null.below query is pivot.

[Code]....

View 1 Replies

VS 2008 How To Handle Null Datetime Values Properly

Oct 25, 2010

I have a class with properties for stuff like FileNumber, OpenedDate, ClosedDate etc.When I Initialise the class I set the datetime variables to Date.MinValue.Then when I populate my textboxes I check if the value of the date variables are Date.Minvalue and I set the textbox text to an empty string.

Code:
If(File.OpenedDate = Date.MinValue, String.Empty, File.OpenedDate.ToString("yyyy/MM/dd"))
However whe I try to save the data to my sql server db I get a datetime out of range error. What is the correct way to handle these null values in my date columns?

View 9 Replies

Forms Data Controls :: How To Handle Null Rows

Jun 18, 2010

I have a query that averages data in 15 minute increments that I display in a chart using Microsoft Chart Controls. Everything works fine except when there is no data for a 15 minute increment. How do I get the chart to show just the empty time slot?

Query:

[Code]....

View 4 Replies

C# - Converting A T-sql Query To Linq To Sql With Coalesce And Multiple Join?

Jan 9, 2011

We have two related tables (inventory, inventoryLocalization) and a t-sql query works on them as follows..

select inv.[code],
coalesce(inL.name, inL2.name) as [name],
coalesce(inL.description, inL2.description) as [description]
from dbo.[inventory] inv
left join dbo.[inventoryLocalization] inL on inv.code = inL.code and inL.language = 'de'
left join dbo.[inventoryLocalization] inL2 on inv.code = inL2.code and inL2.language = 'en'
where inv.[code] like '15.' + '%'
order by inv.[code];

to performance improvement, we will try to use these tables from cache not from RDBMS directly.
for an asp.net 4.0 project, by c#. We've took both tables to chache with a DataSet and we can reach both of them through that DataSet.

DataSet ds = UtilityCache.getCachedDataSet();

"ds.Tables[0]" is inventory Table and "ds.Tables[1]" is inventoryLocalization Table

But how can we convert above T-Sql syntax to linq2Sql. is it possible to use coalesce for select statement and to join on two tables with multiple conditions ?

View 1 Replies

ADO.NET :: Linq To SQL - Handle Concurrency In Application

Aug 10, 2010

I am trying to handle concurrency in my application. Basically if user A has a support ticket open which currently has a status of 'Active' and user 'B' opens the same ticket and closes it (changing its status to closed), I would expect a confict execepton to be thown when user 'A' tries to close the support ticket. For some reason this is not happening. I have checked that Update check is set to 'Always' in the dbml file. Here is an exerpt of my code. // Update 'Active' lead to 'Close'

[Code]....

View 2 Replies







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