Hi NerakSeven,
Please refer below sample.
HTML
<asp:GridView runat="server" ID="gvNames" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
C#
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
string name;
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
{
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (!string.IsNullOrEmpty(dr["Name"].ToString()))
{
name = dr["Name"].ToString();
}
else
{
name = "0";
}
dt.Rows.Add(name);
// INSERT TO Table code.
}
con.Close();
}
}
gvNames.DataSource = dt;
gvNames.DataBind();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Name")
Dim name As String
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers", con)
con.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
While dr.Read()
If Not String.IsNullOrEmpty(dr("Name").ToString()) Then
name = dr("Name").ToString()
Else
name = "0"
End If
dt.Rows.Add(name)
End While
con.Close()
End Using
End Using
gvNames.DataSource = dt
gvNames.DataBind()
End If
End Sub
Screenshot