2014-10-11 3 views
-1

vb.net 2010의 새로운 기능으로, 데이터를 mysql 데이터베이스로 업데이트하는 중에 예외가 발생했습니다. 무슨 일이 일어나고 있는지 모르겠지만, 문제를 해결할 수 없습니다.문자열 "SystemNullReferenceException : O"에서 유형 '정수'로 변환이 올바르지 않습니다.

Private sub updateData() 
    Dim path As String = "IMAGES/" 
    Dim reg As String = 1 

     dbcon.Close() ' This will close any open connection 
     con = "server=localhost; uid=root;pwd=;database=myDatabase;" 

     Try 
      dbcon.ConnectionString = con 
      dbcon.Open() 
      sql = "update t_table1 set data1='" & data1.Text & "',data2='" & data2.Text & "',"',reg='" & reg & "' where id='" & id.text & "';" 

      dbcom = New MySqlCommand(sql, dbcon) 
      dbcom.ExecuteNonQuery() 

       Try 
        If Not Directory.Exists(path) Then 
         'This will create a directory "IMAGE" 
         Dim di As DirectoryInfo = Directory.CreateDirectory(path) 
        End If 

        If Not PictureBox1.Image Is Nothing Then 
         'If the picturebox contain an image then it will save into "IMAGE"directory 
         PicCopy.Save(String.Concat(path, id.Text, ".png")) 
        Else 

        End If 
       Catch ex As Exception 
        MsgBox("The process failed: ", ex.ToString()) 
       End Try 

      MsgBox("Records Successfully Update!", MsgBoxStyle.Information) 
      dbcon.Close() 
     Catch ex As Exception 
      MsgBox("Unable to update data. Error is " & ex.Message) 
      dbcon.Close() 
      Exit Sub 
     End Try 
End Sub 

내가 뭘하려고 오전입니다 :이 열려있는

  • 하고 다음 내 데이터베이스
  • 업데이트에 대한 새 연결을 열면

    1. 연결을 닫습니다 여기 내 코드입니다 'data1'및 'data2'여기서 id = "id.text texbox의 값"
    2. "IMAGE"디렉토리가 존재하는지 확인하십시오. 작성하지 않은 경우 확인하십시오.
    3. 확인하십시오. 포함 된 다음 "id.text의 texbox"의 이름으로와 ".png를"
    4. 수행 연결
    5. 을 닫으의 파일 확장자 이미지 이토 "IMAGE"디렉토리를 저장하는 경우 picturebox2는 이미지를 포함

    하지만이 기능 내 데이터를 호출 할 때마다 업데이트를했다하지만 나에게 그 말을 예외 오류 제공 : 데이터를 업데이트 할 수

    수 없음을. 오류는 변환 양식 문자열 "SystemNullReferenceException : O"에서 '정수'유형이 유효하지 않습니다.

  • +0

    sql 쿼리는 ** 쓸모가 없습니다 **. 읽기 : [매개 변수화 된 SQL 쿼리는 어떻게 작성합니까? 왜해야합니까?] (http://stackoverflow.com/questions/542510/how-do-i-create-a-parameterized-sql-query-why-should-i) 및 this : [매개 변수화 된 SQL을 제공하거나 나에게 죽음을주십시오] (http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/). –

    답변

    0

    확인을 시도해 문제입니다. 이제는 내 자신의 문제를 해결했습니다. 왜 항상 문자열 등에서 변환 오류를 말하는 이유는 이미지를 저장하는 것이 오류이기 때문에 오류가 잡기 시도 및 잡기 기능에 의해 잡기 때문에 항상이 줄 때문에 변환 오류가 발생했습니다.

    Private sub updateData() 
        Dim reg As String = 1 
    
         dbcon.Close() ' This will close any open connection 
         con = "server=localhost; uid=root;pwd=;database=myDatabase;" 
    
         Try 
          dbcon.ConnectionString = con 
          dbcon.Open() 
    
          sql = "update t_table1 set data1='" & data1.Text & "',data2='" & data2.Text & "',"',reg='" & reg & "' where id='" & id.text & "';" 
    
          dbcom = New MySqlCommand(sql, dbcon) 
          dbcom.ExecuteNonQuery() 
    
          dbcon.Close() 
         Catch ex As Exception 
          MsgBox("Unable to update data. Error is " & ex.Message) 
          dbcon.Close() 
          Exit Sub 
         End Try 
    
         updateImage()' call this to properly create directory and save the image 
    End Sub 
    

    내가 만들 수있는 새로운 기능 'updateImage을()'제작 : 우선이 기능적인 CAL MsgBox("The process failed: ", ex.ToString())은 내가 MsgBox("The process failed: "& ex.message) 및 오류 팝업으로 변경하지만 이미지는, 그래서 내가 더 좋은 방법을 만들어 저장되지 않는다 디렉토리에 저장하고 새 이미지를 저장하십시오.

    Private sub updateImage() 
        Dim PicCopy As Image 
        Dim path As String = "IMAGES/" 
         Try 
          If Not Directory.Exists(path) Then 
             'This will create a directory "IMAGE" 
             Dim di As DirectoryInfo = Directory.CreateDirectory(path) 
            End If 
    
            If Not PictureBox1.Image Is Nothing Then 
             'If the picturebox contain an image then it will save into "IMAGE"directory 
             PicCopy.Save(String.Concat(path, id.Text, ".png")) 
            Else 
    
            End If 
           Catch ex As Exception 
            MsgBox("The process failed: " & ex.Message) 
           End Try 
    
          MsgBox("Records Successfully Update!", MsgBoxStyle.Information) 
    End Sub 
    

    그리고 잘 작동합니다. 그리고 오류가 없습니다. 감사합니다. 심지어 내 자신의 문제를 해결합니다.

    0

    당신의 테이블이 업데이트되었는데 예외가 발생해도 이미지를 지정된 디렉토리에 저장합니까? 난 당신이 준 MySQL의 쿼리 같은 느낌은 내가 같은 테스트하지 않았습니다으로 here.try

    sql = "update t_table1 set data1='" & data1.Text & "',data2='" & data2.Text & "',reg='" & reg & "' where id='" & id.text & "';" 
    

    그냥 ...

    +0

    감사합니다. 쿼리에 문제가 없습니다. 이미 mysql 명령 줄과 vb.net에서 시도해도 문제가 없습니다. 문제는 이미지 저장 중이라고 생각하지만 해결 방법을 모르겠습니다. – Polar

    +0

    내 코드에서이 줄을 제거하려고 시도했습니다. 'PicCopy.Save (String.Concat (path, id.Text, ".png "))' 오류가 발생하지 않았으므로 어쩌면이 줄이 내 문제입니다. 이 줄은 이미지를"IMAGE/"디렉토리에 저장하기위한 것입니다. – Polar

    관련 문제