Find Null/Empty Values in Asp.net
Datatable and Replace with Other Values
Introduction:
Here
I will explain how to find null or empty values in datatable and replace with
other values in asp.netusing 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
UserId
|
UserName
|
Education
|
Location
|
1
|
SureshDasari
|
Chennai
|
|
2
|
MadhavSai
|
MBA
|
|
3
|
MaheshDasari
|
B.Tech
|
Nuzividu
|
4
|
Mahendra
|
CA
|
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
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
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
|
0 comments:
Post a Comment