Guest User

Untitled

a guest
Sep 11th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. How to get a value in another table, store it in a variable or data reader, and then use it to update another table
  2. SqlConnection conUpdate = new SqlConnection(GetConnectionString());
  3. conUpdate.Open();
  4.  
  5. SqlCommand com2 = new SqlCommand();
  6. com2.Connection = conUpdate;
  7. com2.CommandText = "SELECT Students.StudentID, Users.UserID FROM Students, Users " +
  8. "WHERE Students.UserID = Users.UserID";
  9.  
  10. int UserId = ((int)com2.ExecuteScalar());
  11.  
  12. com2.CommandText = "SELECT MAX(StudentID) FROM Students";
  13.  
  14. int StudentId = ((int)com2.ExecuteScalar());
  15.  
  16. com2.CommandType = CommandType.Text;
  17. com2.CommandText = "UPDATE Users SET UserName=@UserName, Password=@Password WHERE UserID=@UserID";
  18.  
  19. com2.Parameters.Add("@UserName", SqlDbType.NVarChar);
  20. com2.Parameters.Add("@Password", SqlDbType.NVarChar);
  21.  
  22. com2.Parameters[0].Value = reader;
  23. com2.Parameters[1].Value = reader;
  24.  
  25. com2.ExecuteNonQuery();
  26. conUpdate.Close();
  27. conUpdate.Dispose();
  28.  
  29. SqlDataReader reader = com2.ExecuteReader();
  30. while (reader.Read())
  31. {
  32. int UserId = Convert.ToInt(reader[0]);// or reader["UserID"]
  33. }
  34. reader.Close();
  35.  
  36. SqlDataAdapter a = new SqlDataAdapter(com2, connection);
  37. DataTable dt = new DataTable();
  38. a.Fill(dt);
  39.  
  40. // Execute the query
  41. SqlDataReader rdr = cmd.ExecuteReader();
  42. int UserId;
  43. while(rdr.Read())
  44. {
  45. UserId = Convert.ToInt32(rdr["UserID"].ToString());
  46. }
  47.  
  48. SqlConnection conUpdate = new SqlConnection(GetConnectionString());
  49. conUpdate.Open();
  50.  
  51. SqlCommand com2 = new SqlCommand();
  52. com2.Connection = conUpdate;
  53. com2.CommandType = CommandType.Text;
  54. com2.CommandText = "SELECT Students.StudentID, Users.UserID FROM Students, Users " +
  55. "WHERE Students.UserID = Users.UserID";
  56.  
  57. SqlDataReader reader = com2.ExecuteReader();
  58.  
  59. if(reader != null)
  60. {
  61. while(reader.Read())
  62. {
  63. SqlCommand com3 = new SqlCommand();
  64. com3.Connection = conUpdate;
  65. com3.CommandType = CommandType.Text;
  66. com3.CommandText = "UPDATE Users SET UserName=@UserName, Password=@Password WHERE UserID=@UserID";
  67. // Assuming that you need both the UserName and Password to default to StudentID
  68. com3.Parameters.AddWithValue("@UserName", reader.GetString(0)); // Assuming StudentID is NVARCHAR
  69. com3.Parameters.AddWithValue("@Password", reader.GetString(0)); // Assuming StudentID is NVARCHAR
  70. com3.Parameters.AddWithValue("@UserID", reader.GetString(1)); // Assuming UserID is NVARCHAR
  71. com3.ExecuteNonQuery();
  72. }
  73. reader.Close();
  74. }
  75.  
  76. conUpdate.Close();
Add Comment
Please, Sign In to add comment