A simple code to bind a gridview on a button click.

Posted by Anishabathija under ASP.NET on 1/11/2013 | Points: 10 | Views : 10938 | Status : [Member] | Replies : 4
hey

I have a button and a gridview.
Just give me a simple code to bind it to a tables sales in my local db.




Responses

Posted by: Self-Innovator on: 1/11/2013 [Member] Bronze | Points: 25

Up
0
Down
public Datatable fillcity()

{
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("sp_fillcity", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count == 0)
{
Label1.Visible = true;
Label1.Text = "No city added till now";
GridView1.Visible = false;
}
else
{
Label1.Visible = false;
GridView1.Visible = true;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}

protected void Button1_Click(object sender, EventArgs e)
{
fillcity();
}


Join Hands Change lives
Thanks & Regards
Straight Edge Society

Anishabathija, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shubham on: 1/11/2013 [Member] Starter | Points: 25

Up
0
Down
Hi!!! Anishabathija

con.ConnectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
con.Open();
MySqlCommand cmd = new MySqlCommand("select * from table", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();

Thanks&Regards:-
Shubham Choudhary
Software Engineer
www.facebook.com/shubham.kunar

Anishabathija, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 1/11/2013 [Member] Starter | Points: 25

Up
0
Down
sqlcommand cmd=new sqlcommand("select * from tablename");
sqdataadapter adp=new sqdataadapter(cmd);
dataset ds=new dataset();
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();


Anishabathija, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shubh on: 1/11/2013 [Member] Starter | Points: 25

Up
0
Down
hii friend!!!


connection.Open();
MySqlCommand cmd1 = new MySqlCommand("Select * from Sales", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd1);
DataTable dt = new DataTable();
da.Fill(dt);
GridView2.DataSource = dt;
GridView2.DataBind(); Grid1.Visible = false;
GridView2.Visible = true;
connection.Close();

enjoy with this...

Shubham Agrahari

Anishabathija, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response