Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data.SqlClient;
  8. using System.Web.Configuration;
  9.  
  10. namespace WebApplication4
  11. {
  12. public partial class index : System.Web.UI.Page
  13. {
  14. SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["lista"].ConnectionString);
  15. protected void kiir()
  16. {
  17.  
  18.  
  19. string select ="Select * From posts";
  20. SqlCommand cmd = new SqlCommand(select,conn);
  21. SqlDataReader dr;
  22. try {
  23. conn.Open();
  24. dr=cmd.ExecuteReader();
  25. GridView1.DataSource=dr;
  26. GridView1.DataBind();
  27. dr.Close();
  28. }
  29. catch(Exception x){
  30. Label4.Text=x.ToString();
  31. }
  32. finally
  33. {
  34. conn.Close();
  35. };
  36. }
  37. protected void Page_Load(object sender, EventArgs e)
  38. {
  39. kiir();
  40. }
  41.  
  42. protected void Button1_Click(object sender, EventArgs e)
  43. {
  44. string insertstring = "INSERT INTO posts VALUES(@title,@desc)";
  45. SqlCommand insert = new SqlCommand(insertstring, conn);
  46. insert.Parameters.AddWithValue("@title", TextBox1.Text);
  47. insert.Parameters.AddWithValue("@desc", TextBox2.Text);
  48. try {
  49. conn.Open();
  50. if (insert.ExecuteNonQuery() < 0)
  51. Label4.Text = "Nem sikerult";
  52. }
  53. catch(Exception x){
  54. Label4.Text=x.ToString();
  55. }
  56. finally{conn.Close();
  57. kiir();
  58. }
  59. }
  60.  
  61. protected void Button2_Click(object sender, EventArgs e)
  62. {
  63. string insertstring = "DELETE FROM posts Where title = @title";
  64. SqlCommand delete = new SqlCommand(insertstring, conn);
  65. delete.Parameters.AddWithValue("@title", TextBox1.Text);
  66. delete.Parameters.AddWithValue("@desc", TextBox2.Text);
  67. try {
  68. conn.Open();
  69. if (delete.ExecuteNonQuery() < 0)
  70. Label4.Text = "Nem sikerult";
  71. }
  72. catch(Exception x){
  73. Label4.Text=x.ToString();
  74. }
  75. finally{conn.Close();
  76. kiir();
  77. }
  78.  
  79.  
  80. }
  81.  
  82. protected void Button3_Click(object sender, EventArgs e)
  83. {
  84. string insertstring = "UPDATE posts SET description = @desc WHERE title = @title";
  85. SqlCommand update = new SqlCommand(insertstring, conn);
  86. update.Parameters.AddWithValue("@title", TextBox1.Text);
  87. update.Parameters.AddWithValue("@desc", TextBox2.Text);
  88. try {
  89. conn.Open();
  90. if (update.ExecuteNonQuery() < 0)
  91. Label4.Text = "Nem sikerult";
  92. }
  93. catch(Exception x){
  94. Label4.Text=x.ToString();
  95. }
  96. finally{conn.Close();
  97. kiir();
  98. }
  99.  
  100.  
  101. }
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement