Binding Source:
Retrieving Information from database by using Binding Source:
- Establish the connection to the sql server by using sql connection class.
SqlConnection cnn = new SqlConnection("Data Source = abc;
Initial Catalog = master;Integrated Security= true");
- Execute the sql query to retrieve information by using sql data adapter.
SqlDataAdapter da = new SqlDataAdapter("select * from student", cnn);
- Fill the values in Data Set by using the sql data adapter.
da.Fill(ds, "student");
- Bind the dataset as data source to the binding source.
bs.DataSource = ds.Tables["student"];
- Bind the values to the controls.
textBox1.DataBindings.Add("Text", bs, "studnetid");
- Repeat the syntax for all the controls.
Use the binding source methods to navigate between the values.
bs.MoveFirst();bs.MovePrevious();bs.MoveNext();bs.MoveLast();
Complete code Listing Goes Here
public partial class Form1 : Form
{
SqlConnection cnn = new SqlConnection("Data Source = .\\SQLEXPRESS;
Initial Catalog = master;Integrated Security= true");
BindingSource bs = new BindingSource(); DataSet ds = new DataSet();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cnn.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from
student", cnn);
da.Fill(ds, "student");
bs.DataSource = ds.Tables["student"];
textBox1.DataBindings.Add("Text", bs, "studnetid");
textBox2.DataBindings.Add("Text", bs, "studnetname");
textBox3.DataBindings.Add("Text", bs, "phoneno");
textBox4.DataBindings.Add("Text", bs, "Address");
textBox5.DataBindings.Add("Text", bs, "DOB");
textBox6.DataBindings.Add("Text", bs, "Gender");
}
private void btnfirst_Click(object sender, EventArgs e)
{
bs.MoveFirst();
}
private void btnprevious_Click(object sender, EventArgs e)
{
bs.MovePrevious();
}
private void btnnext_Click(object sender, EventArgs e)
{
bs.MoveNext();
}
private void btnlast_Click(object sender, EventArgs e)
{
bs.MoveLast();
}
}
No comments:
Post a Comment