Advertisement
Ang377ou

insert,update and delete using c#

Jun 3rd, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.SqlClient;
  10.  
  11. namespace Insert_update_delete
  12. {
  13. public partial class Form1 : Form
  14. {
  15. SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
  16. SqlCommand cmd = new SqlCommand();
  17. SqlDataReader dr;
  18.  
  19. public Form1()
  20. {
  21. InitializeComponent();
  22. }
  23.  
  24. private void Form1_Load(object sender, EventArgs e)
  25. {
  26. cmd.Connection = cn;
  27. Loadlist();
  28. }
  29.  
  30. private void button1_Click(object sender, EventArgs e)
  31. {
  32. if (txtId.Text != "" & txtName.Text != "")
  33. {
  34. cn.Open();
  35. cmd.CommandText = "Insert into info (Id, Name) Values('"+txtId.Text+"' , '"+txtName.Text+"')";
  36. cmd.ExecuteNonQuery();(it is showing error here )
  37. cmd.Clone();
  38. MessageBox.Show("Record Inserted!");
  39. cn.Close();
  40. txtId.Text = "";
  41. txtName.Text = "";
  42. Loadlist();
  43. }
  44. }
  45. private void Loadlist()
  46. {
  47. listBox1.Items.Clear();
  48. listBox2.Items.Clear();
  49. cn.Open();
  50. cmd.CommandText = "Select * From info";
  51. dr = cmd.ExecuteReader();
  52. if (dr.HasRows)
  53. {
  54. while (dr.Read())
  55. {
  56. listBox1.Items.Add(dr[0].ToString());
  57. listBox2.Items.Add(dr[1].ToString());
  58. }
  59. }
  60.  
  61. cn.Close();
  62. }
  63.  
  64. private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
  65. {
  66. ListBox l = sender as ListBox;
  67. if(l.SelectedIndex != -1)
  68. {
  69. listBox1.SelectedIndex = l.SelectedIndex;
  70. listBox2.SelectedIndex = l.SelectedIndex;
  71. txtId.Text = listBox1.SelectedItem.ToString();
  72. txtName.Text = listBox2.SelectedItem.ToString();
  73. }
  74. }
  75.  
  76. private void button2_Click(object sender, EventArgs e)
  77. {
  78. if (txtId.Text != "" & txtName.Text != "")
  79. {
  80. cn.Open();
  81. cmd.CommandText = "Delete from info where id*'"+txtId.Text+"' and name*'"+txtName.Text+"'";
  82. cmd.ExecuteNonQuery();
  83. cn.Close();
  84. MessageBox.Show("Record Deleted");
  85. Loadlist();
  86. txtId.Text = "";
  87. txtName.Text = "";
  88.  
  89. }
  90. }
  91.  
  92. private void button3_Click(object sender, EventArgs e)
  93. {
  94. if (txtId.Text != "" & txtName.Text != "" & listBox1.SelectedIndex != -1)
  95. {
  96. cn.Open();
  97. cmd.CommandText = "Update info set id='"+txtId.Text+"', name='"+txtName.Text+"' where id='"+listBox1.SelectedItem.ToString()+"' and name= '"+listBox2.SelectedItem.ToString()+"'";
  98. cmd.ExecuteNonQuery();
  99. cn.Close();
  100. MessageBox.Show("Record Updated");
  101. Loadlist();
  102. txtId.Text = "";
  103. txtName.Text = "";
  104.  
  105. }
  106. }
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement