How to handle null values in DataBinder.Eval()

G

Guest

Hello,

in my aspx page I have the followong code:

<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
....
<%# Server.HtmlDecode((string)DataBinder.Eval(Container.DataItem,"tekst")) %>
....
</ItemTemplate>
</asp:Repeater>

Whereby "tekst" one of the columns is being loaded in the dataset
In the code behind page data is loaded as as follows:

....
meetmate.DataLayer.Meetings m = new meetmate.DataLayer.Meetings();
ds = m.GetMeetingByID(meetingID);
dr = ds.Tables[0].Rows[0];
DataView dv = new DataView(ds.Tables[0]);
this.Repeater1.DataSource = dv;
this.Repeater1.DataBind();
....

The problem is that column "tekst" can be null.
In that case, I'm getting an error on
<%# Server.HtmlDecode((string)DataBinder.Eval(Container.DataItem,"tekst")) %>
because the (string) cast then goes wrong. If "tekst" is not null, it is ok.

Now this is just for "tekst", but also other columns in my table could be
null.
How can I deal with this is a nice way?

Eddy de Boer
 
S

Scott M.

You should test for null using IsDBNull(object) prior to allowing the value
in question to be used in your repeater.
 
G

Guest

Thank you for your answer.

But how would I do that?
Because if it is null, is it your intention to replace it with "" in case of
a strng and a 0 in case of a integer?

Then I should put the following code in the code behind like:

....
for each (Column cl in ds.Tables.Rows[j].Columns) )
{
if (cl.IsDBnull(cl))
{
if (cl.Type == DataType.Integer)
{
cl.Value = 0;
}
....
}
ds.ApplyUpdates()

something like this?

Or is there another way?
Scott M. said:
You should test for null using IsDBNull(object) prior to allowing the value
in question to be used in your repeater.


eddy de boer said:
Hello,

in my aspx page I have the followong code:

<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
...
<%# Server.HtmlDecode((string)DataBinder.Eval(Container.DataItem,"tekst"))
%>
...
</ItemTemplate>
</asp:Repeater>

Whereby "tekst" one of the columns is being loaded in the dataset
In the code behind page data is loaded as as follows:

...
meetmate.DataLayer.Meetings m = new meetmate.DataLayer.Meetings();
ds = m.GetMeetingByID(meetingID);
dr = ds.Tables[0].Rows[0];
DataView dv = new DataView(ds.Tables[0]);
this.Repeater1.DataSource = dv;
this.Repeater1.DataBind();
...

The problem is that column "tekst" can be null.
In that case, I'm getting an error on
<%# Server.HtmlDecode((string)DataBinder.Eval(Container.DataItem,"tekst"))
%>
because the (string) cast then goes wrong. If "tekst" is not null, it is
ok.

Now this is just for "tekst", but also other columns in my table could be
null.
How can I deal with this is a nice way?

Eddy de Boer
 
C

Chris Botha

Write a function in the code-behind and call the function (instead of
Server.HtmlDecode) with the column parameters, the function can check if it
is a null (int or string) and return what you want, else the function calls
Server.HtmlDecode.
Your idea of going through the table and change the nulls will work as well.

eddy de boer said:
Thank you for your answer.

But how would I do that?
Because if it is null, is it your intention to replace it with "" in case
of
a strng and a 0 in case of a integer?

Then I should put the following code in the code behind like:

...
for each (Column cl in ds.Tables.Rows[j].Columns) )
{
if (cl.IsDBnull(cl))
{
if (cl.Type == DataType.Integer)
{
cl.Value = 0;
}
....
}
ds.ApplyUpdates()

something like this?

Or is there another way?
Scott M. said:
You should test for null using IsDBNull(object) prior to allowing the
value
in question to be used in your repeater.


eddy de boer said:
Hello,

in my aspx page I have the followong code:

<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
...
<%#
Server.HtmlDecode((string)DataBinder.Eval(Container.DataItem,"tekst"))
%>
...
</ItemTemplate>
</asp:Repeater>

Whereby "tekst" one of the columns is being loaded in the dataset
In the code behind page data is loaded as as follows:

...
meetmate.DataLayer.Meetings m = new meetmate.DataLayer.Meetings();
ds = m.GetMeetingByID(meetingID);
dr = ds.Tables[0].Rows[0];
DataView dv = new DataView(ds.Tables[0]);
this.Repeater1.DataSource = dv;
this.Repeater1.DataBind();
...

The problem is that column "tekst" can be null.
In that case, I'm getting an error on
<%#
Server.HtmlDecode((string)DataBinder.Eval(Container.DataItem,"tekst"))
%>
because the (string) cast then goes wrong. If "tekst" is not null, it
is
ok.

Now this is just for "tekst", but also other columns in my table could
be
null.
How can I deal with this is a nice way?

Eddy de Boer
 
C

Chris Botha

Sorry, to elaborate, instead of having
<%#
Server.HtmlDecode((string)DataBinder.Eval(Container.DataItem,"tekst"))
%>

then you have
<%#
MyFunction(Container.DataItem,"tekst")
%>


Chris Botha said:
Write a function in the code-behind and call the function (instead of
Server.HtmlDecode) with the column parameters, the function can check if
it is a null (int or string) and return what you want, else the function
calls Server.HtmlDecode.
Your idea of going through the table and change the nulls will work as
well.

eddy de boer said:
Thank you for your answer.

But how would I do that?
Because if it is null, is it your intention to replace it with "" in case
of
a strng and a 0 in case of a integer?

Then I should put the following code in the code behind like:

...
for each (Column cl in ds.Tables.Rows[j].Columns) )
{
if (cl.IsDBnull(cl))
{
if (cl.Type == DataType.Integer)
{
cl.Value = 0;
}
....
}
ds.ApplyUpdates()

something like this?

Or is there another way?
Scott M. said:
You should test for null using IsDBNull(object) prior to allowing the
value
in question to be used in your repeater.


Hello,

in my aspx page I have the followong code:

<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
...
<%#
Server.HtmlDecode((string)DataBinder.Eval(Container.DataItem,"tekst"))
%>
...
</ItemTemplate>
</asp:Repeater>

Whereby "tekst" one of the columns is being loaded in the dataset
In the code behind page data is loaded as as follows:

...
meetmate.DataLayer.Meetings m = new meetmate.DataLayer.Meetings();
ds = m.GetMeetingByID(meetingID);
dr = ds.Tables[0].Rows[0];
DataView dv = new DataView(ds.Tables[0]);
this.Repeater1.DataSource = dv;
this.Repeater1.DataBind();
...

The problem is that column "tekst" can be null.
In that case, I'm getting an error on
<%#
Server.HtmlDecode((string)DataBinder.Eval(Container.DataItem,"tekst"))
%>
because the (string) cast then goes wrong. If "tekst" is not null, it
is
ok.

Now this is just for "tekst", but also other columns in my table could
be
null.
How can I deal with this is a nice way?

Eddy de Boer

 
G

Guest

Thank you for your answer!

Chris Botha said:
Sorry, to elaborate, instead of having
<%#
Server.HtmlDecode((string)DataBinder.Eval(Container.DataItem,"tekst"))
%>

then you have
<%#
MyFunction(Container.DataItem,"tekst")
%>


Chris Botha said:
Write a function in the code-behind and call the function (instead of
Server.HtmlDecode) with the column parameters, the function can check if
it is a null (int or string) and return what you want, else the function
calls Server.HtmlDecode.
Your idea of going through the table and change the nulls will work as
well.

eddy de boer said:
Thank you for your answer.

But how would I do that?
Because if it is null, is it your intention to replace it with "" in case
of
a strng and a 0 in case of a integer?

Then I should put the following code in the code behind like:

...
for each (Column cl in ds.Tables.Rows[j].Columns) )
{
if (cl.IsDBnull(cl))
{
if (cl.Type == DataType.Integer)
{
cl.Value = 0;
}
....
}
ds.ApplyUpdates()

something like this?

Or is there another way?
:

You should test for null using IsDBNull(object) prior to allowing the
value
in question to be used in your repeater.


Hello,

in my aspx page I have the followong code:

<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
...
<%#
Server.HtmlDecode((string)DataBinder.Eval(Container.DataItem,"tekst"))
%>
...
</ItemTemplate>
</asp:Repeater>

Whereby "tekst" one of the columns is being loaded in the dataset
In the code behind page data is loaded as as follows:

...
meetmate.DataLayer.Meetings m = new meetmate.DataLayer.Meetings();
ds = m.GetMeetingByID(meetingID);
dr = ds.Tables[0].Rows[0];
DataView dv = new DataView(ds.Tables[0]);
this.Repeater1.DataSource = dv;
this.Repeater1.DataBind();
...

The problem is that column "tekst" can be null.
In that case, I'm getting an error on
<%#
Server.HtmlDecode((string)DataBinder.Eval(Container.DataItem,"tekst"))
%>
because the (string) cast then goes wrong. If "tekst" is not null, it
is
ok.

Now this is just for "tekst", but also other columns in my table could
be
null.
How can I deal with this is a nice way?

Eddy de Boer


 
J

James Doughty

If you are using a typed dataset you can set a property of the column
called NullValue and have it come back with a default value (which is a
empty string)
 
G

Guest

Thank you for your response.
I don't know how to create a strong typed dataset, I will show you how I get
my data.
I'm using the folling DataHandler to get my data:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace meetmate.DataLayer
{
/// <summary>
/// Summary description for DataHandler.
/// </summary>
public class DataHandler
{
private SqlConnection conn;
private string connectionString;

public DataHandler()
{
connectionString = ConfigurationSettings.AppSettings["connectionString"];
conn = new SqlConnection(connectionString);
}

public DataSet GetDataSet(SqlCommand cmd)
{
cmd.Connection = conn;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);

return ds;
}

public void ExecuteCommand(SqlCommand cmd)
{
cmd.Connection = conn;
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}
}
}
}


In another module I call the Get like this:

public DataSet GetMeetingByID(int meetingID)
{
string qry = "select
titel,datum,tijd_van,tijd_tot,adres,plaats,tekst,verslag " +
"from Meetings " +
"where ID = @ID";
SqlCommand cmd = new SqlCommand(qry);
cmd.Parameters.Add(new SqlParameter("@ID",meetingID));
DataHandler dh = new DataHandler();
DataSet ds = dh.GetDataSet(cmd);

return ds;
}

Then, in my BusinessLayer, I call the GetMeetingByID.
Do you know or have a reference how I could use typed DataSet in this?

Thank you,
Eddy


James Doughty said:
If you are using a typed dataset you can set a property of the column
called NullValue and have it come back with a default value (which is a
empty string)

Hello,

in my aspx page I have the followong code:

<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
...
<%#
Server.HtmlDecode((string)DataBinder.Eval(Container.DataItem,"tekst"))
%> ...
</ItemTemplate>
</asp:Repeater>

Whereby "tekst" one of the columns is being loaded in the dataset
In the code behind page data is loaded as as follows:

...
meetmate.DataLayer.Meetings m = new meetmate.DataLayer.Meetings();
ds = m.GetMeetingByID(meetingID);
dr = ds.Tables[0].Rows[0];
DataView dv = new DataView(ds.Tables[0]);
this.Repeater1.DataSource = dv;
this.Repeater1.DataBind();
...

The problem is that column "tekst" can be null.
In that case, I'm getting an error on
<%#
Server.HtmlDecode((string)DataBinder.Eval(Container.DataItem,"tekst"))
%> because the (string) cast then goes wrong. If "tekst" is not null,
it is ok.

Now this is just for "tekst", but also other columns in my table could
be null.
How can I deal with this is a nice way?

Eddy de Boer
 
S

sp3d2orbit

<%#
Server.HtmlDecode(IsDBNull(DataBinder.Eval(Container.DataItem,"tekst"))
? "" : (string) DataBinder.Eval(Container.DataItem,"tekst")) %>
 
J

James Doughty

Take a look at http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/dnhcvb03/html/vb03f9.asp

If you have any questions or don't understand something about let me
know.

Thank you for your response.
I don't know how to create a strong typed dataset, I will show you how
I get my data.
I'm using the folling DataHandler to get my data:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace meetmate.DataLayer
{
/// <summary>
/// Summary description for DataHandler.
/// </summary>
public class DataHandler
{
private SqlConnection conn;
private string connectionString;

public DataHandler()
{
connectionString =
ConfigurationSettings.AppSettings["connectionString"];
conn = new SqlConnection(connectionString);

}

public DataSet GetDataSet(SqlCommand cmd)
{
cmd.Connection = conn;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);

return ds;
}

public void ExecuteCommand(SqlCommand cmd)
{
cmd.Connection = conn;
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}
}
}
}


In another module I call the Get like this:

public DataSet GetMeetingByID(int meetingID)
{
string qry = "select
titel,datum,tijd_van,tijd_tot,adres,plaats,tekst,verslag " +
"from Meetings " +
"where ID = @ID";
SqlCommand cmd = new SqlCommand(qry);
cmd.Parameters.Add(new SqlParameter("@ID",meetingID));
DataHandler dh = new DataHandler();
DataSet ds = dh.GetDataSet(cmd);

return ds;
}

Then, in my BusinessLayer, I call the GetMeetingByID.
Do you know or have a reference how I could use typed DataSet in this?

Thank you,
Eddy


James Doughty said:
If you are using a typed dataset you can set a property of the column
called NullValue and have it come back with a default value (which is
a empty string)

Hello,

in my aspx page I have the followong code:

<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
...
<%#
Server.HtmlDecode((string)DataBinder.Eval(Container.DataItem,"tekst"
)) %> ...
</ItemTemplate>
</asp:Repeater>

Whereby "tekst" one of the columns is being loaded in the dataset
In the code behind page data is loaded as as follows:

...
meetmate.DataLayer.Meetings m = new meetmate.DataLayer.Meetings();
ds = m.GetMeetingByID(meetingID);
dr = ds.Tables[0].Rows[0];
DataView dv = new DataView(ds.Tables[0]);
this.Repeater1.DataSource = dv;
this.Repeater1.DataBind();
...

The problem is that column "tekst" can be null.
In that case, I'm getting an error on
<%#
Server.HtmlDecode((string)DataBinder.Eval(Container.DataItem,"tekst"
)) %> because the (string) cast then goes wrong. If "tekst" is not
null, it is ok.

Now this is just for "tekst", but also other columns in my table
could be null.
How can I deal with this is a nice way?

Eddy de Boer
 
G

Guest

Thank you James

James Doughty said:
Take a look at http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/dnhcvb03/html/vb03f9.asp

If you have any questions or don't understand something about let me
know.

Thank you for your response.
I don't know how to create a strong typed dataset, I will show you how
I get my data.
I'm using the folling DataHandler to get my data:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace meetmate.DataLayer
{
/// <summary>
/// Summary description for DataHandler.
/// </summary>
public class DataHandler
{
private SqlConnection conn;
private string connectionString;

public DataHandler()
{
connectionString =
ConfigurationSettings.AppSettings["connectionString"];
conn = new SqlConnection(connectionString);

}

public DataSet GetDataSet(SqlCommand cmd)
{
cmd.Connection = conn;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);

return ds;
}

public void ExecuteCommand(SqlCommand cmd)
{
cmd.Connection = conn;
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}
}
}
}


In another module I call the Get like this:

public DataSet GetMeetingByID(int meetingID)
{
string qry = "select
titel,datum,tijd_van,tijd_tot,adres,plaats,tekst,verslag " +
"from Meetings " +
"where ID = @ID";
SqlCommand cmd = new SqlCommand(qry);
cmd.Parameters.Add(new SqlParameter("@ID",meetingID));
DataHandler dh = new DataHandler();
DataSet ds = dh.GetDataSet(cmd);

return ds;
}

Then, in my BusinessLayer, I call the GetMeetingByID.
Do you know or have a reference how I could use typed DataSet in this?

Thank you,
Eddy


James Doughty said:
If you are using a typed dataset you can set a property of the column
called NullValue and have it come back with a default value (which is
a empty string)

=?Utf-8?B?ZWRkeSBkZSBib2Vy?= <[email protected]>
wrote in
Hello,

in my aspx page I have the followong code:

<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
...
<%#
Server.HtmlDecode((string)DataBinder.Eval(Container.DataItem,"tekst"
)) %> ...
</ItemTemplate>
</asp:Repeater>

Whereby "tekst" one of the columns is being loaded in the dataset
In the code behind page data is loaded as as follows:

...
meetmate.DataLayer.Meetings m = new meetmate.DataLayer.Meetings();
ds = m.GetMeetingByID(meetingID);
dr = ds.Tables[0].Rows[0];
DataView dv = new DataView(ds.Tables[0]);
this.Repeater1.DataSource = dv;
this.Repeater1.DataBind();
...

The problem is that column "tekst" can be null.
In that case, I'm getting an error on
<%#
Server.HtmlDecode((string)DataBinder.Eval(Container.DataItem,"tekst"
)) %> because the (string) cast then goes wrong. If "tekst" is not
null, it is ok.

Now this is just for "tekst", but also other columns in my table
could be null.
How can I deal with this is a nice way?

Eddy de Boer
 
Joined
Oct 23, 2009
Messages
1
Reaction score
0
Null value in templatefield for eval databinding

See adam.blogix.co.il/2009/10/19/null-values-in-aspnet-templatefield-databinding for the correct approach to null value databinding in templatefields
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top