2012-06-15 2 views
0

나는 데이터베이스에서 값으로 textBox 자동 완성을하려고합니다. 내가 총알 테스트 할 때 코드는 아래 기능에서 if 문까지 걸어 갈 것입니다. 어떤 사람은 제 코드가 그 이후에 실행되지 않는 이유를 말해 줄 수 있습니까 if?왜 작동하지 않습니까? 코드를 실행하지 않습니까?

나는 다음을 실행하려고하지만 if 전에 점프 계속 :

public AutoCompleteStringCollection AutoCompleate(string dataBase, string procedure) 
{ 
     AutoCompleteStringCollection namesCollection = 
      new AutoCompleteStringCollection(); 

     SqlDataReader dReader; 
     SqlCommand cmd = new SqlCommand(procedure, con); 
     cmd.CommandType = CommandType.StoredProcedure; 
     con.Open(); 
     dReader = cmd.ExecuteReader(); // This is the last place my bullet check 
             // gets before it hop's out! 
     if (dReader.HasRows == true) 
     { 
      while (dReader.Read()) 
       namesCollection.Add(dReader["SystemUser"].ToString()); 
     } 
     con.Close(); 
     dReader.Close(); 
     return namesCollection; 
    } 

추가 정보하지만, 위의에 집중하십시오! 당신이 어떤 정보를 필요로하는 경우


는 문의하십시오. :)


위의 호출 :

textBoxUser.AutoCompleteMode = AutoCompleteMode.Suggest; 
textBoxUser.AutoCompleteSource = AutoCompleteSource.CustomSource; 
textBoxUser.AutoCompleteCustomSource = 
    DatabaseService.Instance.AutoCompleate("AuditIT", "AutoCompleate"); 

저장된 프로 시저 : 데이터베이스를 공격 할 때

USE [AuditIT] 
GO 

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
--Allow textboxs to have a autocomplete 
ALTER procedure [dbo].[AutoCompleate] 
as 

Select distinct SystemUser from SchemaAudit 
      order by SystemUser asc 
+1

어떻게'SqlConnection'을 정의합니까? –

+2

"튀어 나오면"예외가 발생했음을 의미합니다. 디버거에서 코드를 실행 해 보셨습니까? 이 코드 외부에서 예외 처리기를 사용하고 있습니까? –

+0

@AlexR. con = 새 SqlConnection (Properties.Settings.Default.DBConnectionString.ToString()); 연결 문자열을 작동 나는 다른 방식으로 같은 일을했습니다 :) – Pomster

답변

3

처리되지 않은 예외가 있습니다. 이 같은 코드를 리팩토링보십시오 :

try{ 

if (dataBase.Length > 0) { procedure = dataBase + ".." + procedure; } //Set procedure to DBNAME..ProcedureName 

SqlDataReader dReader; 
      SqlCommand cmd = new SqlCommand(procedure, con); 
      cmd.CommandType = CommandType.StoredProcedure; 
      con.Open(); 
      dReader = cmd.ExecuteReader(); // This is the last place my bullet check gets before it hop's out! 
      if (dReader.HasRows == true) 
      { 
       while (dReader.Read()) 
        namesCollection.Add(dReader["SystemUser"].ToString()); 
      } 
      con.Close(); 
      dReader.Close(); 
} 
catch(Exception e) 
{ 
    // handle the exception better than me :) 
    Console.WriteLine(e.Message); 
} 

메시지 박스 또는 console.WriteLine에 중단 점을 넣고의 일이 무엇인지를 참조하십시오.