dharmendr says:
DataTable dt =
new
DataTable();
dt.Columns.AddRange(
new
DataColumn[5] {
new
DataColumn(
"ProductId"
,
typeof
(
string
)),
new
DataColumn(
"Product"
,
typeof
(
string
)),
new
DataColumn(
"Price"
,
typeof
(
int
)),
new
DataColumn(
"Quantity"
,
typeof
(
int
)),
new
DataColumn(
"Total"
,
typeof
(
int
))});
dt.Rows.Add(101,
"Sun Glasses"
, 200, 5, 1000);
dt.Rows.Add(102,
"Jeans"
, 400, 2, 800);
dt.Rows.Add(103,
"Trousers"
, 300, 3, 900);
dt.Rows.Add(104,
"Shirts"
, 550, 2, 1100);
Replace above with the below code with the article.
C#
DataTable dt = new DataTable();
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT ProductId,Product,Price,Quantity,Total FROM Products", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
con.Open();
cmd.CommandType = CommandType.Text;
sda.SelectCommand = cmd;
sda.Fill(dt);
con.Close();
}
}
}
VB.Net
Dim dt As New DataTable()
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constring)
Using cmd As New SqlCommand("SELECT ProductId,Product,Price,Quantity,Total FROM Products", con)
Using sda As New SqlDataAdapter()
con.Open()
cmd.CommandType = CommandType.Text
sda.SelectCommand = cmd
sda.Fill(dt)
con.Close()
End Using
End Using
End Using