2015-04-18 169 views
1

我有問題我無法將數據加載到文本框中,查詢從窗體中的數據庫中獲取。 while循環無法執行。如何解決這個問題。或沒有任何錯誤或例外。內部命令不能執行調試器移動來捕獲和完成。如何從sql數據庫表加載數據到文本框

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"].ToString()); 
     this.txtEmplyCity.Text = (reader1["City"].ToString()); 
     this.txtEmplyAddress.Text = (reader1["Address"].ToString()); 
     this.txtSalary.Text = (reader1["Salary"].ToString()); 
     this.txtDailyWage.Text = (reader1["DailyWage"].ToString()); 

      reader1.Close(); 
     } 


     } 
     catch (Exception ex) 
     { 

     } 

    } 
+0

你有什麼例外,你正在進入'catch'塊? –

回答

0

哦什麼.Stop !!!使用參數化查詢,以避免SQL注入

提到你在connection

conncection字符串我希望這些問題是你已經missesd txtEmployId.Text價值,並在您選擇查詢txtEmplyName.Text

SqlConnection connection= new SqlConnection(your Connection string); 
string query = "SELECT CNIC, City, MobileNo, Address, Salary, DailyWage, Status 
     FROM Employees WHERE EmployId [email protected] 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"].ToString()); 
    this.txtEmplyCity.Text = (reader1["City"].ToString()); 
    this.txtEmplyAddress.Text = (reader1["Address"].ToString()); 
    this.txtSalary.Text = (reader1["Salary"].ToString()); 
    this.txtDailyWage.Text = (reader1["DailyWage"].ToString()); 

     reader1.Close(); 
    } 
1
  1. 哪裏是你的ConnectionString的?如果它不在頁面中,請包含一個。
  2. 打開連接像Con.open()
  3. 使用參數化查詢來避免sql注入,但這只是一個建議。

代碼的問題是我認爲的連接字符串。打開try塊內的連接。

相關問題