Sakshi Kaul

Sakshi Kaul

  • NA
  • 72
  • 7.2k

How to retrieve data from database in textboxes

Apr 3 2019 1:39 AM
Hi
 
I want when employee enters emp id in text box and press enter then the corresponding other details present in the registration form should be entered and by default all text box should be kept disable and when the user clicks on edit button then he should be able to edit the text (enter the information) and when he clicks on registration button then the data should be saved in other table and on clear button again the new page should be loaded
I have done coding for registration button that will save data in the table.
 
What all changes should be done in order to implement above scenario ?
  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.Sql;  
  8. using System.Data.SqlClient;  
  9. using System.Data;  
  10. using System.Configuration;  
  11. public partial class _Default : System.Web.UI.Page  
  12. {  
  13. SqlCommand cmd = new SqlCommand();  
  14. SqlConnection con= new SqlConnection();  
  15. protected void Page_Load(object sender, EventArgs e)  
  16. {  
  17. con.ConnectionString = @"Data Source=SAKSHIKAUL-LT\SQLEXORESS1;Initial Catalog=registrationForm;Integrated Security=True";  
  18. con.Open();  
  19. {  
  20. if (!this.IsPostBack)  
  21. Label1.Text = "";  
  22. }  
  23. }  
  24. protected void Button1_Click(object sender, EventArgs e)  
  25. { SqlCommand cmd = new SqlCommand("insert into RegistrationTable" + "(EmpId,Nationality,Religion,Gender,Address,EmailId,Hobbies,MaritalStatus) values (@EmpId,@Nationality,@Religion,@Gender,@Address,@EmailId,@Hobbies,@MaritalStatus)", con);  
  26. //string insertQuery = "insert into RegistrationTable(EmpId,Nationality,Religion,Gender,Address,EmailId,Hobbies,Maritalstatus)values (@EmpId,@Nationality,@Religion,@Gender,@Address,@EmailId,@MHobbies,@Maritalstatus)";  
  27. cmd.Parameters.AddWithValue("@EmpId", TextBox1.Text);  
  28. cmd.Parameters.AddWithValue("@Nationality", TextBox2.Text);  
  29. cmd.Parameters.AddWithValue("@Religion", TextBox3.Text);  
  30. cmd.Parameters.AddWithValue("@Gender", TextBox4.Text);  
  31. cmd.Parameters.AddWithValue("@Address", TextBox5.Text);  
  32. cmd.Parameters.AddWithValue("@EmailId", TextBox6.Text);  
  33. cmd.Parameters.AddWithValue("@Hobbies", TextBox7.Text);  
  34. cmd.Parameters.AddWithValue("@Maritalstatus", TextBox8.Text);  
  35. cmd.ExecuteNonQuery();  
  36. Label1.Text = "Registered Successfuly";  
  37. }  
  38. protected void Button2_Click(object sender, EventArgs e)  
  39. {  
  40. cmd.CommandText = "update RegistrationTable set Nationality =' " + TextBox2.Text + "' where EmpId='" + TextBox1.Text + "' ";  
  41. cmd.CommandText = "update RegistrationTable set Religion =' " + TextBox3.Text + "' where EmpId='" + TextBox1.Text + "' ";  
  42. cmd.CommandText = "update RegistrationTable set Gender =' " + TextBox4.Text + "' where EmpId='" + TextBox1.Text + "' ";  
  43. cmd.CommandText = "update RegistrationTable set Address =' " + TextBox5.Text + "' where EmpId='" + TextBox1.Text + "' ";  
  44. cmd.CommandText = "update RegistrationTable set EmailId =' " + TextBox6.Text + "' where EmpId='" + TextBox1.Text + "' ";  
  45. cmd.CommandText = "update RegistrationTable set Hobbies =' " + TextBox7.Text + "' where EmpId='" + TextBox1.Text + "' ";  
  46. cmd.CommandText = "update RegistrationTable set MaritalStatus =' " + TextBox8.Text + "' where EmpId='" + TextBox1.Text + "' ";  
  47. cmd.Connection = con;  
  48. cmd.ExecuteNonQuery();  
  49. Label1.Text = "Updated Successfuly";  
  50. }  
  51. protected void Button3_Click(object sender, EventArgs e)  
  52. {  
  53. TextBox1.Text = "";  
  54. TextBox2.Text = "";  
  55. TextBox3.Text = "";  
  56. TextBox4.Text = "";  
  57. TextBox5.Text = "";  
  58. TextBox6.Text = "";  
  59. TextBox7.Text = "";  
  60. TextBox8.Text = "";  
  61. Label1.Text = "Clear All";  
  62. }  
  63. protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)  
  64. {  
  65. }  
  66. protected void TextBox1_TextChanged(object sender, EventArgs e)  
  67. {  
  68. }  
  69. }  

Answers (10)