2016-06-11 4 views
-5

이 오류가 계속 발생합니다. InvoiceNum 변수에서 while 루프로 들어갈 때이 오류가 발생합니다. 내가 뭘 잘못하고 있는지 조언 해 줄 수 있니?'System.Int32'형식의 개체를 'System.String'오류로 캐스팅 할 수 없습니다.

string selectSqlQuery = "SELECT * FROM Invoice"; //WHERE invoice_no=" + 1 + ""; 
       //Create a command in C# to perform the sql Query 
       SqlCommand mySelectCommand = new SqlCommand(selectSqlQuery, myDBconnection); 
       //Creating the process for performing the command 
       SqlDataReader performCommand = mySelectCommand.ExecuteReader(); 
       //Executing the command or running the command 
      // performCommand.Read(); 

       while (performCommand.Read()) 
       { 

        invoiceNum = Convert.ToInt16(performCommand.GetString(0)); 
        pcBarcode = Convert.ToInt16(performCommand.GetString(1)); 
        customerID = Convert.ToInt16(performCommand.GetString(2)); 
        probDescr = performCommand.GetString(3); 
        empID = Convert.ToInt16(performCommand.GetString(4)); 
        currentDate = Convert.ToDateTime(performCommand.GetString(5)); 
        solution = performCommand.GetString(6); ; 
        partsID = Convert.ToInt16(performCommand.GetString(7)); 
        labourPrice = Convert.ToInt16(performCommand.GetString(8)); 
        totalPrice = Convert.ToInt16(performCommand.GetString(9)); 
        // invoiceClass = new Invoice(invoiceNum, pcBarcode, customerID, probDescr, empID, currentDate, solution, partsID, labourPrice, totalPrice); 
       } 
       //Closing the execution of the command 
       performCommand.Close(); 
       //Closing the connection 
       myDBconnection.Close(); 
      } 
+0

InvoiceNum 변수 정의 및 Invoice 테이블 정의의 첫 번째 열은 무엇입니까? – piotrwest

+0

둘 다 int 유형입니다 – noor

답변

1

변경 :

int i = 0; 
string s = i.ToString(); 

또한, 당신의 유형은 다음과 같은 작업을 수행 할 수 IConvertible 구현하는 경우 인보이스 번호 :

invoiceNum = performCommand.GetInt16(0); 
+0

이 오류가 발생합니다 : System.InvalidCastException : 지정된 캐스트가 유효하지 않습니다. – noor

+0

invoiceNum = performCommand.GetInt32 (0)으로 변경했을 때 작동했습니다.이 시간으로 2 시간 동안 고생했습니다. 감사합니다. 많이 있습니다. – noor

+0

invoiceNum = performCommand.GetInt32 (0)은 어떻습니까? ? – piotrwest

0

이러한 오류를 해결하려면 당신은 당신의 정수에 대한 ToString 메서드를 호출해야합니다 : 라인

int i = 0; 
string s = Convert.ToString(i); 
0

invoiceNum은 32 비트 정수로 보이므로 독자의 GetInt32 방법을 사용하는 것과 같이 읽어야합니다. 독자는 유형 간의 자동 번역을하지 않습니다. GetString은 원본으로 사용하는 입력란이 텍스트로 입력되고 발생하는 캐스팅 오류가 정확하게 정수를 문자열로 변환 할 수 없다고 알려주는 경우에만 작동합니다. 라고 GetString documentation의 설명 부분을 참조하십시오

No conversions are performed; therefore, the data retrieved must already be a string.

Call IsDBNull to check for null values before calling this method.

당신은 라인을 다시 작성해야

:

invoiceNum = Convert.ToInt16(performCommand.GetString(0)); 

대신 읽기 :

invoiceNum = Convert.ToInt16(performCommand.GetInt32(0)); 

당신은 나머지 필드 읽기를 검토해야합니다을 라인은 올바른 기본 유형을 기반으로합니다.

대부분의 코드를 동일하게 유지하려면 GetValue을 사용해야합니다. 이는 object을 반환하기 때문에 런타임 유형에 필드 유형이 반영 될 것이기 때문입니다. 그런 다음 해당 메소드를 Convert 메소드 중 하나에 전송할 수 있으며, 소스와 요청 된 유형 중 하나를 지원한다고 가정하면 변환을 수행합니다. 따라서 독자의 GetString 전화를 모두 GetValue으로 변경하십시오.

관련 문제