Introduction:
Here
I will explain how to find null or empty values in datatable and replace with
other values in asp.net
using
C#, VB.NET.
Description:
In
previous posts I explained Dynamically create datatable in asp.net, jQuery display average rating with decimal values, jQuery generate thumbnails from you tube videos, jQuery redirect to another page after some time delay, send forgot password as email in
asp.net
and many articles relating to asp.net,
C#, VB.NET code snippets. Now I will explain how
to find null values in datatable and replace with other values in asp.net
using
C#, VB.NET.
I
have one datatable which contains data with some null values as shown below
Now
I want to replace these null or empty values with some other values for that we
need to write the code as shown below
UserId
|
UserName
|
Education
|
Location
|
1
|
SureshDasari
|
Chennai
|
|
2
|
MadhavSai
|
MBA
|
|
3
|
MaheshDasari
|
B.Tech
|
Nuzividu
|
4
|
Mahendra
|
CA
|
for (int i = 0; i <
dt.Rows.Count;i++ )
{
for(int
j=0;j<dt.Columns.Count;j++)
{
if(string.IsNullOrEmpty(dt.Rows[i][j].ToString()))
{
// Write your Custom Code
dt.Rows[i][j] = "your
required value";
}
}
}
|
If you want to see it in complete example first
write following code in your aspx page
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<title>Find Null Values in datatable and replace with other values
in asp.net</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:GridView ID="gvDetails"
runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
|
In code behind add the following namespaces
C#
Code
using System;
using System.Data;
|
After that write the following code in code behind
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridviewData();
}
}
/// <summary>
/// Dynamically create & bind data to
datatable and bind datatable to gridview
/// </summary>
protected void
BindGridviewData()
{
DataTable dt = new DataTable();
dt.Columns.Add("UserId",
typeof(Int32));
dt.Columns.Add("UserName",
typeof(string));
dt.Columns.Add("Education",
typeof(string));
dt.Columns.Add("Location",
typeof(string));
DataRow dtrow = dt.NewRow(); // Create New
Row
dtrow["UserId"]
= 1; //Bind Data to Columns
dtrow["UserName"]
= "SureshDasari";
dtrow["Education"]
= null;
dtrow["Location"]
= "Chennai";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); //
Create New Row
dtrow["UserId"]
= 2; //Bind Data to Columns
dtrow["UserName"]
= "MadhavSai";
dtrow["Education"]
= "MBA";
dtrow["Location"]
= string.Empty;
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); //
Create New Row
dtrow["UserId"]
= 3; //Bind
Data to Columns
dtrow["UserName"]
= "MaheshDasari";
dtrow["Education"]
= "B.Tech";
dtrow["Location"]
= "Nuzividu";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow(); //
Create New Row
dtrow["UserId"]
= 4; //Bind Data to Columns
dtrow["UserName"]
= "Mahendra";
dtrow["Education"]
= "CA";
dtrow["Location"]
= null;
dt.Rows.Add(dtrow);
for (int i = 0; i <
dt.Rows.Count;i++ )
{
for(int
j=0;j<dt.Columns.Count;j++)
{
if(string.IsNullOrEmpty(dt.Rows[i][j].ToString()))
{
// Write your Custom Code
dt.Rows[i][j] = "N/A";
}
}
}
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
|
VB.NET
Code
Imports System.Data
Partial Class
Default2
Inherits System.Web.UI.Page
Protected Sub
Page_Load(ByVal sender As Object, ByVal e As
EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGridviewData()
End If
End Sub
Protected Sub
BindGridviewData()
Dim dt As New DataTable()
dt.Columns.Add("UserId",
GetType(Int32))
dt.Columns.Add("UserName",
GetType(String))
dt.Columns.Add("Education",
GetType(String))
dt.Columns.Add("Location",
GetType(String))
Dim dtrow As DataRow =
dt.NewRow()
' Create New Row
dtrow("UserId")
= 1
'Bind Data to Columns
dtrow("UserName")
= "SureshDasari"
dtrow("Education")
= Nothing
dtrow("Location")
= "Chennai"
dt.Rows.Add(dtrow)
dtrow = dt.NewRow()
' Create New Row
dtrow("UserId")
= 2
'Bind Data to Columns
dtrow("UserName")
= "MadhavSai"
dtrow("Education")
= "MBA"
dtrow("Location")
= String.Empty
dt.Rows.Add(dtrow)
dtrow = dt.NewRow()
' Create New Row
dtrow("UserId")
= 3
'Bind Data to Columns
dtrow("UserName")
= "MaheshDasari"
dtrow("Education")
= "B.Tech"
dtrow("Location")
= "Nuzividu"
dt.Rows.Add(dtrow)
dtrow = dt.NewRow()
' Create New Row
dtrow("UserId")
= 4
'Bind Data to Columns
dtrow("UserName")
= "Mahendra"
dtrow("Education")
= "CA"
dtrow("Location")
= Nothing
dt.Rows.Add(dtrow)
For i As Integer = 0 To
dt.Rows.Count - 1
For j As Integer = 0 To
dt.Columns.Count - 1
If String.IsNullOrEmpty(dt.Rows(i)(j).ToString())
Then
' Write your Custom Code
dt.Rows(i)(j) = "N/A"
End If
Next
Next
gvDetails.DataSource = dt
gvDetails.DataBind()
End Sub
End Class
|
In above code I am replacing null or empty values with "N/A"
run
your application and check below output
Output
UserId
|
UserName
|
Education
|
Location
|
1
|
SureshDasari
|
N/A
|
Chennai
|
2
|
MadhavSai
|
MBA
|
N/A
|
3
|
MaheshDasari
|
B.Tech
|
Nuzividu
|
4
|
Mahendra
|
CA
|
N/A
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
6 comments :
Thank you very much! I'm looking for this
h
mokka..i need how to find and replace a text in excel file using microsoft interop excell
Its good but how to set text box value in place of null value
Thanks, and i want one more thing is that if can i change it if its null then an one.jpg else two.jpg
Hi..I want to delete the empty cell and merge with the next row value. Please suggest some logic. Thank You.
Note: Only a member of this blog may post a comment.