Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Bind Data in Gridview in Asp.net using Dataset (DataTable) in C#, VB.NET

Apr 8, 2015
Introduction

Here I will explain how to bind
gridview in asp.net using dataset in c#, vb.net with example or bind asp.net gridview using dataset from database in c#, vb.net with example.


Before implement this example first design one table productinfo in your database as shown below

Column Name
Data Type
Allow Nulls
productid
Int(IDENTITY=TRUE)
Yes
productname
varchar(50)
Yes
price
varchar(50)
Yes
Once table created in database enter some dummy data to test application now open your aspx page and write the code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Bind data to gridview in asp.net using c#, vb.net</title>
<style type="text/css">
.GridviewDiv {font-size: 100%; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helevetica, sans-serif; color: #303933;}
.headerstyle
{
color:#FFFFFF;border-right-color:#abb079;border-bottom-color:#abb079;background-color: #df5015;padding:0.5em 0.5em 0.5em 0.5em;text-align:center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="GridviewDiv">
<asp:GridView runat="server" ID="gvDetails" AllowPaging="true" PageSize="10" AutoGenerateColumns="false" Width="420px" OnPageIndexChanging="gvDetails_PageIndexChanging">
<HeaderStyle CssClass="headerstyle" />
<Columns>
<asp:BoundField DataField="productid" HeaderText="Product Id" />
<asp:BoundField DataField="productname" HeaderText="Product Name" />
<asp:BoundField DataField="price" HeaderText="Price" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

After completion of aspx page add following namespaces in codebehind

C# Code


using System;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

After completion of adding namespaces you need to write the code like as shown below


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
protected void BindGridview()
{
DataSet ds = new DataSet();
using (SqlConnection con = new SqlConnection("Data Source=Suresh;Integrated Security=true;Initial Catalog=MySampleDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from productinfo", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();
gvDetails.DataSource = ds;
gvDetails.DataBind();
}
}
protected void gvDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvDetails.PageIndex = e.NewPageIndex;
BindGridview();
}
VB.NET Code


Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGridview()
End If
End Sub
Protected Sub BindGridview()
Dim ds As New DataSet()
Using con As New SqlConnection("Data Source=Suresh;Integrated Security=true;Initial Catalog=MySampleDB")
con.Open()
Dim cmd As New SqlCommand("select * from productinfo", con)
Dim da As New SqlDataAdapter(cmd)
da.Fill(ds)
con.Close()
gvDetails.DataSource = ds
gvDetails.DataBind()
End Using
End Sub
Protected Sub gvDetails_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
gvDetails.PageIndex = e.NewPageIndex
BindGridview()
End Sub
End Class

Demo


Download Sample Code Attached


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.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

6 comments :

Anonymous said...

hlo plz tell me how we can call a java script file in aspx age

Unknown said...





<%#Eval("qualification_id") %>




<%#Eval("qualification_name") %>








<%#Eval("date") %>










public void grid_qul_fill()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from qualification", con);
//cmd.ExecuteNonQuery();
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataSet dss = new DataSet();
ad.Fill(dss);

grid_qul.DataSource = dss;
grid_qul.DataBind();

con.Close();

}
protected void grid_qul_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grid_qul.EditIndex = -1;
grid_qul_fill();
}
protected void grid_qul_RowEditing(object sender, GridViewEditEventArgs e)
{
grid_qul.EditIndex = e.NewEditIndex;
grid_qul_fill();
}
protected void grid_qul_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = grid_qul.EditIndex;
GridViewRow gr = grid_qul.Rows[index];
TextBox id = gr.FindControl("txt_id") as TextBox;
TextBox n = gr.FindControl("txt_qn") as TextBox;
con.Open();
SqlCommand cmd = new SqlCommand("sp_qualification_insert_update", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id",id.Text);
cmd.Parameters.AddWithValue("@n",n.Text);
cmd.ExecuteNonQuery();
con.Close();
grid_qul.EditIndex = -1;

grid_qul_fill();
}
protected void grid_qul_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = grid_qul.DataKeys[e.RowIndex].Value.ToString();
con.Open();
SqlCommand cmd = new SqlCommand("delete from qualification where qualification_id='"+id+"'", con);
cmd.ExecuteNonQuery();
con.Close();
grid_qul_fill();
}



Unknown said...
This comment has been removed by the author.
Unknown said...

protected void btn_save_Click(object sender, EventArgs e)
{
SqlCommand cm=new SqlCommand("select * from Candidate_registration where email='"+txt_email.Text+"'",con);
SqlDataAdapter da = new SqlDataAdapter(cm);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
lbl.Text = "Email Id already subbmit:";
}
else{
string hob = "";
for (int i = 0; i < chk_hobi.Items.Count; i++)
{
if (chk_hobi.Items[i].Selected == true)
{
hob += chk_hobi.Items[i].Text;
}
}
string fn = Path.GetFileName(upload_resume.PostedFile.FileName);
upload_resume.SaveAs(Server.MapPath("File" + "\\" + fn));
con.Open();
SqlCommand cmd = new SqlCommand("sp_Candidate_registration_insert", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@n",txt_name.Text);
cmd.Parameters.AddWithValue("@email",txt_email.Text);
cmd.Parameters.AddWithValue("@contact",txt_mob.Text);
cmd.Parameters.AddWithValue("@password",txt_pwd.Text);
cmd.Parameters.AddWithValue("@secque",ddl_sec_que.SelectedItem.Text);
cmd.Parameters.AddWithValue("@answer",txt_answer.Text);
cmd.Parameters.AddWithValue("@gender",rbtn_gender.SelectedItem.Text);
cmd.Parameters.AddWithValue("@hobies",hob);
cmd.Parameters.AddWithValue("@profile",ddl_profile.SelectedItem.Text);
cmd.Parameters.AddWithValue("@qualification",ddl_qulification.SelectedValue);
cmd.Parameters.AddWithValue("@years",ddl_year.SelectedItem.Text);
cmd.Parameters.AddWithValue("@months",ddl_month.SelectedItem.Text);
cmd.Parameters.AddWithValue("@refranc_name",txt_refranc_name.Text);
cmd.Parameters.AddWithValue("@current_company",txt_curent_comp.Text);
cmd.Parameters.AddWithValue("@salary",txt_crnt_salry.Text);
cmd.Parameters.AddWithValue("@notic_priod",txt_notic_prid.Text);
cmd.Parameters.AddWithValue("@expect_max",txt_max_ctc.Text);
cmd.Parameters.AddWithValue("@expect_min",txt_min_ctc.Text);
cmd.Parameters.AddWithValue("@resume",fn);
cmd.ExecuteNonQuery();
con.Close();
lbl.Text = "u r regisred Welcome"+" : "+txt_name.Text;
grid_candidate_fill();
}

News and amazing tools said...

25

Anonymous said...

public class Class1
{
public SqlConnection con;
public SqlCommand cmd;
public Class1()
{
con = new SqlConnection("Data Source=.;Initial Catalog=DBEX1;Integrated Security=True;Pooling=False");
cmd = new SqlCommand();
cmd.Connection = con;
}
public SqlConnection getcon()
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
return con;
}
public void dbclose()
{
con.Close();
}
public int exenonquery(String sql)
{
getcon();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
int i = cmd.ExecuteNonQuery();
return i;
}
public object exescalar(String sql)
{
getcon();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
object ob = cmd.ExecuteScalar();
return ob;
}
public SqlDataReader exereader(String sql)
{
getcon();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
SqlDataReader dr = cmd.ExecuteReader();
return dr;
}
public DataTable exetable(String sql)
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataSet exedataset(String sql)
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
public void fillgrid(String sql,GridView dv)
{
dv.DataSource=exedataset(sql);
dv.DataBind();
}
public void fiilddl(String sql, DropDownList ddl, String txt, String value)
{
ddl.DataTextField = txt;
ddl.DataValueField = value;
ddl.DataSource = exedataset(sql);
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("---Select--", "0"));
}
public void fiilchk(String sql, CheckBoxList chk, String txt, String value)
{
chk.DataTextField = txt;
chk.DataValueField = value;
chk.DataSource = exedataset(sql);
chk.DataBind();
//chk.Items.Insert(0, new ListItem("---Select--", "0"));
}

}

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.