Ramu Eppakayala

Ramu Eppakayala

  • NA
  • 25
  • 2.3k

datatable retreiving first record of the database

Feb 10 2020 11:08 PM
I am performing search operation based on id which is auto incremented when I enter id as 0 it is retrieving data which is having id 1.
 
This is the search method
  1. public DataTable Search(int code=0)  
  2. {  
  3. SqlConnection con = sqlConnection; String Query = "Select * from person" + (code > 0 ? " where code =" + code : "");  
  4. SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(Query, con);  
  5. {  
  6. DataTable dataTable = new DataTable("Temp");  
  7. sqlDataAdapter.Fill(dataTable);  
  8. con.Close();  
  9. return dataTable;  
  10. }  
  11. }  
This is search button code
  1. private void btnSearch_Click(object sender, EventArgs e)  
  2. {  
  3. if(txtSearchCode.Text.Length>0)  
  4. {  
  5. DataTable dataTable = new SqlHelper().Search(int.Parse(txtSearchCode.Text));  
  6. if(dataTable.Rows.Count>0)  
  7. {  
  8. try  
  9. {  
  10. txtCode.Text = dataTable.Rows[0]["code"].ToString();  
  11. txtFirstname.Text = dataTable.Rows[0]["firstname"].ToString();  
  12. txtLastname.Text = dataTable.Rows[0]["lastname"].ToString(); var a = dataTable.Rows[0]["gender"].ToString() == "Male" ? rbnMale.Checked = true : rbnFemale.Checked = true;  
  13. txtMobile.Text = dataTable.Rows[0]["mobile"].ToString();  
  14. dtpDOB.Text = dataTable.Rows[0]["dob"].ToString();  
  15. txtAge.Text = dataTable.Rows[0]["age"].ToString();  
  16. chkStatus.Checked = dataTable.Rows[0]["status"].ToString() == "active"true :false;  
  17. }  
  18. catch(Exception ex)  
  19. {  
  20. MessageBox.Show(ex.Message);  
  21. }  
  22. }  
  23. else  
  24. {  
  25. MessageBox.Show("No Data Found");  
  26. }  
  27. }  
  28. else  
  29. {  
  30. MessageBox.Show("Please Enter Code");  
  31. }  
  32. }  

Answers (2)