2015-04-18 112 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块内的连接。

相关问题