welcome again guys...
In last post we have seen data navigation through datareader. Now in this post i am going to show u a navigation of data through dataset as wel as the CRUD operations.
Here we are....
Design:
Coding:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Datareader_navigation
{
public partial class Form1 : Form
{
int inc,count;
private SqlDataReader dr;
private SqlConnection con;
private DataTable dt;
private DataSet ds;
private SqlDataAdapter da;
DataRow drow;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string strcon = @"Data Source=NARESH-PC;Initial Catalog=PracticeDB;Persist Security Info=True;User ID=sa;Password=123";
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
ds = new DataSet();
da = new SqlDataAdapter("select * from sample",con);
SqlCommandBuilder cmb = new SqlCommandBuilder(da);
da.Fill(ds,"sample");
count = ds.Tables["sample"].Rows.Count;
if (ds == null)
{
MessageBox.Show("dataset is empty..");
}
showNextRecord( inc);
panel1.Visible = false;
}
private void showNextRecord(int inc)
{
if (ds != null)
{
DataRow drow = ds.Tables["sample"].Rows[inc];
textBox4.Text = drow.ItemArray.GetValue(0).ToString();
textBox1.Text = drow.ItemArray.GetValue(1).ToString();
textBox2.Text = drow.ItemArray.GetValue(2).ToString();
textBox3.Text = drow.ItemArray.GetValue(3).ToString();
}
else
{
MessageBox.Show("dataset is empty..");
}
}
private void button1_Click(object sender, EventArgs e)
{
//next button click
panel1.Visible = false;
if (inc != count - 1)
{
inc++;
showNextRecord(inc);
}
else
{
MessageBox.Show("no further record....");
}
}
private void button3_Click_1(object sender, EventArgs e)
{
//previous button click
panel1.Visible = false;
if (inc > 0)
{
inc--;
showNextRecord( inc);
}
else
{
MessageBox.Show("first record");
}
}
private void button4_Click_1(object sender, EventArgs e)
{
//first button click
panel1.Visible = false;
if (inc != 0)
{
inc = 0;
showNextRecord(inc);
}
}
private void button2_Click_1(object sender, EventArgs e)
{
//last button click
panel1.Visible = false;
if (inc != count - 1)
{
inc = count - 1;
showNextRecord(inc);
}
}
private void addnew_Click(object sender, EventArgs e)
{
textBox1.Focus();
textBox4.Visible = false;
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
panel1.Visible = false;
DataRow row = ds.Tables["sample"].NewRow();
row["name"] = textBox1.Text;
row["surname"] = textBox2.Text;
row["city"] = textBox3.Text;
ds.Tables["sample"].Rows.Add(row);
count = count + 1;
inc = count - 1;
da.Update(ds,"sample");
MessageBox.Show("entry added successfully...");
}
private void delete_Click(object sender, EventArgs e)
{
ds.Tables["sample"].Rows[inc].Delete();
count--;
inc = 0;
da.Update(ds, "sample");
MessageBox.Show("deleted successfully..");
showNextRecord(inc);
panel1.Visible = false;
}
private void update_Click(object sender, EventArgs e)
{
DataRow row1 = ds.Tables["sample"].Rows[inc];
row1["name"] = textBox1.Text;
row1["surname"] = textBox2.Text;
row1["city"] = textBox3.Text;
da.Update(ds,"sample");
MessageBox.Show("updated successfully..");
panel1.Visible = false;
}
private void viewall_Click(object sender, EventArgs e)
{
panel1.Visible = true;
dataGridView1.DataSource = ds;
}
}
}
No comments:
Post a Comment