J'ai un problème, je ne peux pas charger datatables dans les zones de text que la requête récupère à partir de la database dans le formulaire Windows. La boucle while ne peut pas s'exécuter. comment résoudre ce problème. ou ne pas avoir d'erreur ou d'exception. Les commands internes ne peuvent pas exécuter le déplacement du débogueur pour attraper et terminer.
private void btnCheck_Click(object sender, EventArgs e) { try { // query = "SELECT Id, Emplname, CNIC, City, MobileNo, Address, Salary, DailyWage, CompanyId, Status FROM Employees where id = '" + labCompyId.Text + "'"; query = "SELECT CNIC, City, MobileNo, Address, Salary, DailyWage, Status FROM Employees WHERE (EmployId = '"+txtEmployId+"') AND (Emplname = '"+txtEmplyName+"')"; SqlCommand command1 = DBConnectivity.getCommandForQuery(query, connection); SqlDataReader reader1 = command1.ExecuteReader(); while(reader1.Read()) { this.txtCNIC.Text = (reader1["CNIC"].ToSsortingng()); this.txtEmplyCity.Text = (reader1["City"].ToSsortingng()); this.txtEmplyAddress.Text = (reader1["Address"].ToSsortingng()); this.txtSalary.Text = (reader1["Salary"].ToSsortingng()); this.txtDailyWage.Text = (reader1["DailyWage"].ToSsortingng()); reader1.Close(); } } catch (Exception ex) { } }
Oh quoi.Stop !!! Utiliser une requête paramétrée pour éviter l'injection SQL
Mentionnez votre string de connection
en connection
J'espère que le problème est que vous avez missesd txtEmployId.Text
value et txtEmplyName.Text
valeur dans votre requête select
SqlConnection connection= new SqlConnection(your Connection ssortingng); ssortingng query = "SELECT CNIC, City, MobileNo, Address, Salary, DailyWage, Status FROM Employees WHERE EmployId =@EmpID AND Emplname = @Emplname "; SqlCommand command1 = new SqlCommand(query, connection); connection.Open(); command1.Parameters.AddWithValue("@EmpID",txtEmployId.Text); command1.Parameters.AddWithValue("@Emplname",txtEmplyName.Text); SqlDataReader reader1 = command1.ExecuteReader(); while(reader1.Read()) { this.txtCNIC.Text = (reader1["CNIC"].ToSsortingng()); this.txtEmplyCity.Text = (reader1["City"].ToSsortingng()); this.txtEmplyAddress.Text = (reader1["Address"].ToSsortingng()); this.txtSalary.Text = (reader1["Salary"].ToSsortingng()); this.txtDailyWage.Text = (reader1["DailyWage"].ToSsortingng()); reader1.Close(); }
Le problème du code est la string de connection à mon avis. Ouvrez la connection à l'intérieur du bloc try.