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.IO; // don't forget import
using System.Data.SqlClient; // don't forget import
namespace databasepractice
{
public partial class Form1 : Form
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\kitkat\Desktop\c#\databseconnectivity\databasepractice\Database1.mdf;Integrated Security=True;User Instance=True"); // connectionstring copy and paste here
SqlCommand cmd;
SqlDataAdapter da;
SqlDataReader dr;
DataSet ds = new DataSet();
DataTable dt;
int i=0;
int cnt = 0;
public Form1()
{
InitializeComponent();
}
private void btndelete_Click(object sender, EventArgs e)
{
try
{
conn.Open();
cmd = new SqlCommand();
// cmd.CommandType = CommandType.Text;
cmd.CommandText = "delete from register where name='"+textBox1.Text+"'";
cmd.Connection = conn;
cmd.ExecuteNonQuery();
MessageBox.Show("delete");
conn.Close();
}
catch (SqlException se)
{
MessageBox.Show(se.Message.ToString());
}
}
private void btninsert_Click(object sender, EventArgs e)
{
try
{
conn.Open();
cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into register values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";
cmd.Connection = conn;
cmd.ExecuteNonQuery();
MessageBox.Show("isert");
conn.Close();
}
catch(SqlException se)
{
MessageBox.Show(se.Message.ToString());
}
}
private void Form1_Load(object sender, EventArgs e)
{
conn.Open();
cmd = new SqlCommand("select * from register", conn);
da = new SqlDataAdapter(cmd);
da.Fill(ds, "register");
cnt = ds.Tables[0].Rows.Count - 1;
//while (ds.Tables[0].Rows.Count > i)
//{
// comboBox1.Items.Add(ds.Tables[0].Rows[i][0].ToString());
// i = i + 1;
//}
conn.Close();
}
public void loaddata()
{
try
{
conn.Open();
cmd = new SqlCommand("select * from register",conn);
da = new SqlDataAdapter(cmd);
da.Fill(ds, "register");
textBox1.Text = ds.Tables[0].Rows[i][0].ToString();
textBox2.Text = ds.Tables[0].Rows[i][1].ToString();
textBox3.Text = ds.Tables[0].Rows[i][2].ToString();
textBox4.Text = ds.Tables[0].Rows[i][3].ToString();
conn.Close();
}
catch (SqlException se)
{
MessageBox.Show(se.Message.ToString());
}
}
private void btnupdate_Click(object sender, EventArgs e)
{
try
{
conn.Open();
cmd = new SqlCommand();
// cmd.CommandType = CommandType.Text;
cmd.CommandText = "update register set city='" + textBox2.Text + "',course='" + textBox3.Text + "',password='" + textBox4.Text + "'where name='" + textBox1.Text + "'";
cmd.Connection = conn;
cmd.ExecuteNonQuery();
MessageBox.Show("update");
conn.Close();
}
catch (SqlException se)
{
MessageBox.Show(se.Message.ToString());
}
}
private void btnfirst_Click(object sender, EventArgs e)
{
loaddata();
}
private void btnnext_Click(object sender, EventArgs e)
{
try
{
if (i < ds.Tables[0].Rows.Count - 1)
{
i = i + 1;
loaddata();
}
else
{
MessageBox.Show("last record");
}
}
catch(SqlException se)
{
}
}
private void btnprecious_Click(object sender, EventArgs e)
{
if (i > 0)
{
i = i - 1;
loaddata();
}
else {
MessageBox.Show("1st record");
}
}
private void btnlast_Click(object sender, EventArgs e)
{
i = cnt;
loaddata();
}
}
}
Comments
Post a Comment