2013-11-26 1 views
0

잘못된 방식으로 접근했을 수 있습니다. 데이터 액세스 레이어 기술을 사용할 때 데이터를 가져올 수 없습니다. 내가 메인 클래스에서 다음 코드를 사용하는 경우 그것은 작동 :데이터 액세스 레이어 기술 데이터 세트 및 어댑터를 사용할 때 데이터를 검색 할 수 없습니다.

private void button1_Click(object sender, RoutedEventArgs e) 
{ 
    myCon.connectionString(); 
    string SomeString = string.Empty; 

    SomeString = "SELECT * FROM TableA"; 
    SqlCommand cmd = new SqlCommand(SomeString, myCon.Con); 
    adapter = new SqlDataAdapter(cmd); 
    ds = new DataSet(); 
    adapter.Fill(ds, "TableA"); 

    txtID.Text = ds.Tables[0].Rows[rno][0].ToString(); 
    txtFirstName.Text = ds.Tables[0].Rows[rno][1].ToString(); 
    txtLastName.Text = ds.Tables[0].Rows[rno][2].ToString(); 
} 

내가 데이터를 검색 할 수 없습니다이고 오류가없는 다른 클래스에서 위의 코드를 삽입합니다.

를 ClassA :

public void Button(string id, string firstname, string lastname) 
{ 
    myCon.connectionString(); 
    string SomeString = string.Empty; 

    SqlConnection cnn = myCon.Con; 

    cnn.Open();   

    CmdString = "SELECT * FROM TableA"; 
    SqlCommand cmd = new SqlCommand(CmdString, cnn.Con); 
    adapter = new SqlDataAdapter(cmd); 
    ds = new DataSet(); 
    adapter.Fill(ds, "Table"); 

    id = ds.Tables[0].Rows[rno][0].ToString(); 
    firstname = ds.Tables[0].Rows[rno][1].ToString(); 
    lastname = ds.Tables[0].Rows[rno][2].ToString(); 
} 

Main 클래스 : 사전에

public partial class MainWindow : Window 
{ 
    ClassA objs = new ClassA(); 

    private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     objs.Button(txtID.Text, txtFirstName.Text, txtLastName.Text); 
    } 

감사 누군가가 나를 여기에 도울 수 있다면.

+1

문자열 자체는 매개 변수로 전달하지 않습니다. 또한 명시 적 값 할당 대신 바인딩을 사용하는 것이 좋습니다. – Somedust

답변

0

전화 번호 objs.Button을 통해 txtID.Text, txtFirstName.Text 등의 값을 설정하려고합니까?

흥미로운 접근 방법이지만 매개 변수가 전달되는 방식 때문에 작성된대로 작동하지 않습니다. 문자열 변수의 값을 변경하면 변수 자체를 변경하지 않고 완전히 새로운 문자열을 할당합니다. 따라서 루틴에서 매개 변수를 재 지정할 때 더 이상 컨트롤의 .Text 속성을 가리키는 것이 아니라 다른 문자열을 가리키게됩니다. .Text 속성은 변경되지 않습니다. 이

이 주변의 여러 가지 방법이 있지만 가장 간단한은 서브 루틴의 서명을 변경할 수 있습니다 :

public void Button(TextBox textId, TextBox txtFirstname, TextBox txtLastname) 
    { 

     myCon.connectionString(); 
     string SomeString = string.Empty; 

     SqlConnection cnn = myCon.Con; 


     cnn.Open(); 
     { 

     CmdString = "SELECT * FROM TableA"; 
     SqlCommand cmd = new SqlCommand(CmdString, cnn.Con); 
     adapter = new SqlDataAdapter(cmd); 
     ds = new DataSet(); 
     adapter.Fill(ds, "Table"); 

     txtId.Text = ds.Tables[0].Rows[rno][0].ToString(); 
     txtFirstname.Text = ds.Tables[0].Rows[rno][1].ToString(); 
     txtLastname.Text = ds.Tables[0].Rows[rno][2].ToString(); 
     } 

} 

다른 옵션은 입력 매개 변수 후 out 일련의 매개 변수를 가지고하는 것입니다. ref 키워드로도 작동하게 할 수도 있지만, 확실하지 않습니다.

관련 문제