emma oj

emma oj

  • 1.5k
  • 195
  • 24.1k

Updating values from a while loop

Feb 18 2018 3:51 PM
  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.WebControls.WebParts;  
  11. using System.Web.UI.HtmlControls;  
  12. using System.Data.SqlClient;  
  13. using System.Web.Configuration;  
  14. using System.Collections.Generic;  
  15. using System.Text;  
  16.   
  17. public partial class Nampak_AddFal : System.Web.UI.Page  
  18. {  
  19. string selectSQL, selectSQL1, selectSQL5;  
  20. string updateSQL, updateSQL1, updateSQL5, updateSQL6, updateSQL2;  
  21. SqlCommand cmd = new SqlCommand();  
  22. SqlCommand cmd1 = new SqlCommand();  
  23. SqlCommand cmd2 = new SqlCommand();  
  24. SqlCommand cmd3 = new SqlCommand();  
  25. SqlCommand cmd5 = new SqlCommand();  
  26. SqlCommand cmd6 = new SqlCommand();  
  27. SqlCommand cmd9 = new SqlCommand();  
  28. SqlConnection dbConn = new SqlConnection();  
  29. SqlConnection dbConn1 = new SqlConnection();  
  30. SqlConnection dbConn2 = new SqlConnection();  
  31. SqlConnection dbConn3 = new SqlConnection();  
  32. SqlConnection dbConn4 = new SqlConnection();  
  33. SqlConnection dbConn5 = new SqlConnection();  
  34. SqlConnection dbConn6 = new SqlConnection();  
  35. SqlDataReader dr, dr1, dr2, dr3, dr5, dr8;  
  36. protected void Page_Load(object sender, EventArgs e)  
  37. {  
  38. }  
  39. protected void SetSS_Click(object sender, EventArgs e)  
  40. {  
  41. Session["YY"] = DropDownList1.SelectedItem.Text;  
  42. int Q = Convert.ToInt32(TextBox1.Text);  
  43. selectSQL = "select id, RTicket from Staff WHERE Jobtype ='Contract'";  
  44. dbConn.ConnectionString = "Data Source=LAPTOP\\SQLEXPRESS; Initial Catalog=Namh;Integrated Security=True;";  
  45. cmd.Connection = dbConn;  
  46. cmd.CommandText = selectSQL;  
  47. cmd.CommandType = CommandType.Text;  
  48. try  
  49. {  
  50. dbConn.Open();  
  51. dr = cmd.ExecuteReader();  
  52. while (dr.Read())  
  53. {  
  54. Session["FT"] = dr["Rticket"].ToString();  
  55. int W = Convert.ToInt32(Session["FT"]);  
  56. int S = W + Q;  
  57. string P = S.ToString();  
  58. Session["PP"] = S.ToString();  
  59. updateSQL1 = "update Staff set Rticket='" + Session["PP"].ToString() + "' where Jobtype ='Contract' ";  
  60. // Response.Write(updateSQL1);  
  61. dbConn1.ConnectionString = "Data Source=LAPTOP\\SQLEXPRESS; Initial Catalog=Namh;Integrated Security=True;";  
  62. cmd1.Connection = dbConn1;  
  63. cmd1.CommandText = updateSQL1;  
  64. cmd1.CommandType = CommandType.Text;  
  65. try  
  66. {  
  67. dbConn1.Open();  
  68. int updated1 = cmd1.ExecuteNonQuery();  
  69. if (updated1 == 1)  
  70. {  
  71. Response.Write("well done");  
  72. }  
  73. else  
  74. {  
  75. Response.Write("double");  
  76. }   
  77. }  
  78. catch (Exception err)  
  79. {  
  80. Label6.Text = "Error Logging in 2 ";  
  81. Label6.Text += err.ToString();  
  82. }  
  83. finally  
  84. {  
  85. dbConn1.Close();  
  86. }  
  87. }  
  88. dr.Close();  
  89. }  
  90. catch (Exception err)  
  91. {  
  92. Label6.Text = "Error Logging in 2 ";  
  93. Label6.Text += err.ToString();  
  94. }  
  95. finally  
  96. {  
  97. dbConn.Close();  
  98. }  
  99. }  
  100. }  
my problem is that i am able to read values before adding a value and lastly updating it in the database e.g

20
34
56
67

but it updates all the values to same values in the database e.g

98
98
98
98

instead of say int Q =4

24
38
60
71
in the database ,please help

Answers (6)