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 WindowsFirstsample { public partial class Form1 : Form { private SqlConnection cn; private SqlDataAdapter da; private SqlCommand cmd; private DataSet ds; private string strcon; private string strq; private int icounter; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { strcon="Data Source=HP-PC//SQLEXPRESS;Initial Catalog=school;Integrated security=true; "; cn = new SqlConnection(strcon); strq = "select * from student"; cn.Open(); da = new SqlDataAdapter(strq,cn); ds = new DataSet(); da.Fill(ds); textBox1.Text = ds.Tables[0].Rows[0]["id"].ToString(); textBox2.Text = ds.Tables[0].Rows[0]["name"].ToString(); } private void button1_Click(object sender, EventArgs e) { icounter = 0; textBox1.Text = ds.Tables[0].Rows[0]["id"].ToString(); textBox2.Text = ds.Tables[0].Rows[0]["name"].ToString(); } private void button2_Click(object sender, EventArgs e) { int irowcount = ds.Tables[0].Rows.Count - 1; if (icounter < irowcount) { icounter++; textBox1.Text = ds.Tables[0].Rows[icounter]["id"].ToString(); textBox2.Text = ds.Tables[0].Rows[icounter]["name"].ToString(); } else { MessageBox.Show("you are already on the last record"); } } private void button3_Click(object sender, EventArgs e) { if (icounter > 0) { icounter--; textBox1.Text = ds.Tables[0].Rows[icounter]["id"].ToString(); textBox2.Text = ds.Tables[0].Rows[icounter]["name"].ToString(); } else { MessageBox.Show("you are already on the first record"); } } private void button4_Click(object sender, EventArgs e) { icounter = ds.Tables[0].Rows.Count - 1; textBox1.Text = ds.Tables[0].Rows[icounter]["id"].ToString(); textBox2.Text = ds.Tables[0].Rows[icounter]["name"].ToString(); } } }
Quote:The exception that is thrown when there is an attempt to dereference a null object reference.
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)