GridView Bind With DataBase in Asp.Net Application
--------------------------------------------------------------------------------------------------------------------------
PART-1
1st Step:- Create DataBase Like Image...
Sql Queries :-----
create Database GridViewConcept
use GridViewConcept
create table Student
(
RollNo int Primary Key,
Name nvarchar(50),
Class nvarchar(50),
Subject nvarchar(50),
MobileNo nvarchar(50),
City nvarchar(50)
)
select * from Student
insert into Student values(1,'RAM','BA','ARTS','8765432545','Patna')
insert into Student values(2,'SHYAM','BSC','Math','8765432545','Patna')
insert into Student values(3,'MOHAN','BA','History','8765432545','Patna')
insert into Student values(4,'SOHAN','BSC','Phy','8765432545','Patna')
-------------------------------------------------------------------------------------------------------------------------
PART-2
1st Step:-
- Add/Create Empty Asp .Net Application
- Add/Create Web Form Like this Image
After Designing Then Code For ADD Button Values into DataBase.
protected void btnADD_Click(object sender, EventArgs e)
{
try
{
SqlConnection _con = new SqlConnection("Data Source=.;Initial Catalog=GridViewConcept;Integrated Security=True");
_con.Open();
SqlCommand _cmd = new SqlCommand("Insert Into StudentRecord Values ("+Convert.ToString(txtRollNo.Text)+",'"+txtName.Text+"','"+txtClass.Text+"','"+txtSubject.Text+"','"+txtMobileNo.Text+"','"+txtCity.Text+"')", _con);
int res = _cmd.ExecuteNonQuery();
lblmASSAGE.Visible = true;
lblmASSAGE.ForeColor = Color.Green;
lblmASSAGE.Text = "Successfully SAVED";
_con.Close();
}
catch (Exception ex)
{
lblmASSAGE.Visible=true;
lblmASSAGE.ForeColor =Color.Red;
lblmASSAGE.Text = ex.ToString();
}
}
--Code For Bind The GridView on click of SHOW Button ..
protected void btnSHOW_Click(object sender, EventArgs e)
{
try
{
SqlConnection _con = new SqlConnection("Data Source=.;Initial Catalog=GridViewConcept;Integrated Security=True");
_con.Open();
SqlCommand _cmd = new SqlCommand("SELECT * FROM StudentRecord", _con);
SqlDataReader dr = _cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
_con.Close();
}
catch (Exception ex)
{
lblmASSAGE.Visible = true;
lblmASSAGE.ForeColor = Color.Red;
lblmASSAGE.Text = ex.ToString();
}
}
---Then Execute The Application
----Fill and Add button Click then Show Button Click value will be show into the Gridview Pannel...
...................................................................................................................................................................