2011-01-17 5 views
2

C#의 다음 삽입 쿼리를 사용하여 데이터베이스에 여러 열을 삽입하려고 시도했지만 어딘가에 예외가 발생하고 삽입을 위해 값이 제공되지 않습니다. 난 그걸 확인하고 내가 어떻게 insert 문을 고칠 수 있는지 알아야 겠어. 아래 그림은 런타임에 매개 변수로 전달되는 내용을 표시합니다. 이 정보를 얻기 위해 중단 점을 사용했습니다.매개 변수를 사용하여 C#에서 쿼리 삽입

이 ...이 일에 전문 지식을 필요로 감사

if (Page.IsValid) 
     { 
      DateTime exhibitDate = DateTime.Now; 
      int caseid = Convert.ToInt32(CaseIDDropDownList.SelectedItem.Text); 
      string exhibittype = exhibitTypeTextBox.Text.ToString(); 
      string storedloc = storedLocationTextBox.Text.ToString(); 
      string offid = DropDownList1.SelectedItem.Text.ToString(); 
      Stream imgStream = exhibitImageFileUpload.PostedFile.InputStream; 
      int imgLen = exhibitImageFileUpload.PostedFile.ContentLength;     
      byte[] imgBinaryData = new byte[imgLen]; 
      int n = imgStream.Read(imgBinaryData,0,imgLen); 
      try 
      { 
       SqlConnection connections = new SqlConnection(strConn); 
       SqlCommand command = new SqlCommand("INSERT INTO Exhibits (CaseID, ExhibitType, ExhibitImage, DateReceived, StoredLocation, InvestigationStatus, OfficerID, SuspectID, InvestigatorID, ManagerID, AdminID) VALUES (@CaseID, @ExhibitType, @ExhibitImage, @DateReceived, @StoredLocation, @InvestigationStatus, @OfficerID, @SuspectID, @InvestigatorID, @ManagerID, @AdminID)", connections); 

       SqlParameter param0 = new SqlParameter("@CaseID", SqlDbType.Int); 
       param0.Value = caseid; 
       command.Parameters.Add(param0); 

       SqlParameter param1 = new SqlParameter("@ExhibitType", SqlDbType.NText); 
       param1.Value = exhibittype; 
       command.Parameters.Add(param1); 

       SqlParameter param2 = new SqlParameter("@ExhibitImage", SqlDbType.Image); 
       param2.Value = imgBinaryData; 
       command.Parameters.Add(param2); 

       SqlParameter param3 = new SqlParameter("@DateReceived", SqlDbType.SmallDateTime); 
       param3.Value = exhibitDate; 
       command.Parameters.Add(param3); 

       SqlParameter param4 = new SqlParameter("@StoredLocation", SqlDbType.NText); 
       param4.Value = storedloc; 
       command.Parameters.Add(param4); 

       SqlParameter param5 = new SqlParameter("@InvestigationStatus", SqlDbType.VarChar, 50); 
       param5.Value = ""; 
       command.Parameters.Add(param5); 

       SqlParameter param6 = new SqlParameter("@OfficerID", SqlDbType.NChar, 10); 
       param6.Value = offid; 
       command.Parameters.Add(param6); 

       SqlParameter param7 = new SqlParameter("@SuspectID", SqlDbType.NChar, 10); 
       param7.Value = null; 
       command.Parameters.Add(param7); 

       SqlParameter param8 = new SqlParameter("@InvestigatorID", SqlDbType.NChar, 10); 
       param8.Value = null; 
       command.Parameters.Add(param8); 

       SqlParameter param9 = new SqlParameter("@ManagerID", SqlDbType.NChar, 10); 
       param9.Value = null; 
       command.Parameters.Add(param9); 

       SqlParameter param10 = new SqlParameter("@AdminID", SqlDbType.NChar, 10); 
       param10.Value = adminID; 
       command.Parameters.Add(param10); 

       connections.Open(); 
       int numRowsAffected = command.ExecuteNonQuery(); 
       connections.Close(); 

       if (numRowsAffected != 0) 
       { 
        Response.Write("<BR>Rows Inserted successfully"); 
        CaseIDDropDownList.ClearSelection(); 
        exhibitTypeTextBox.Text = null; 
        storedLocationTextBox.Text = null; 
        DropDownList1.ClearSelection(); 
       } 
       else 
       { 
        Response.Write("<BR>An error occurred uploading the image"); 
       } 
      } 
      catch (Exception ex) 
      { 
       string script = "<script>alert('" + ex.Message + "');</script>"; 
      } 

alt text

  • $ 예외 { "매개 변수화 된 쿼리를 '(다음과 같이 예외는 @CaseID int, @ ExhibitType ntext, @ ExhibitImage image, @ DateReceive '는 제공되지 않은'@SuspectID '매개 변수를 필요로합니다. "} System.Exception {System.Data.Sq 당신은 훨씬 쉽게 수행 할 수 있습니다
+2

예외는 무엇입니까? – Femaref

+1

무슨 예외가 있습니까? 그 메시지의 내용은 많은 도움이 될 것입니다 !! –

+1

catch 블록의 예외 메시지는 무엇입니까? – TheGeekYouNeed

답변

4

, 당신은이 같은 DBNull.Value 사용할 필요가 : 당신이 지금 null에 뭔가를 설정하는 곳

SqlParameter param9 = new SqlParameter("@ManagerID", SqlDbType.NChar, 10); 
param9.Value = DBNull.Value; 
command.Parameters.Add(param9); 

이 작업을 수행을하고 난 꽤 잘 작동 할거야. 모든 것이 괜찮아 보입니다.

+0

굉장합니다. Marc_s. 마술처럼 작동했습니다 .... :) 정말 고마워요. – Selase

0

lClient.SqlException는}, 같은 그것을 시도 :이 실제로 정확한 예외를 모르고 무슨 일이 일어나고 있는지가 해결됩니다 경우

if (Page.IsValid) 
     { 
      DateTime exhibitDate = DateTime.Now; 
      int caseid = Convert.ToInt32(CaseIDDropDownList.SelectedItem.Text); 
      string exhibittype = exhibitTypeTextBox.Text.ToString(); 
      string storedloc = storedLocationTextBox.Text.ToString(); 
      string offid = DropDownList1.SelectedItem.Text.ToString(); 
      Stream imgStream = exhibitImageFileUpload.PostedFile.InputStream; 
      int imgLen = exhibitImageFileUpload.PostedFile.ContentLength;     
      byte[] imgBinaryData = new byte[imgLen]; 
      int n = imgStream.Read(imgBinaryData,0,imgLen); 
      try 
      { 
       SqlConnection connections = new SqlConnection(strConn); 
       SqlCommand command = new SqlCommand("INSERT INTO Exhibits (CaseID, ExhibitType, ExhibitImage, DateReceived, StoredLocation, InvestigationStatus, OfficerID, SuspectID, InvestigatorID, ManagerID, AdminID) VALUES (@CaseID, @ExhibitType, @ExhibitImage, @DateReceived, @StoredLocation, @InvestigationStatus, @OfficerID, @SuspectID, @InvestigatorID, @ManagerID, @AdminID)", connections); 
       command.Parameters.AddWithValue("@CaseID", caseid); 
       //and so on for your 10 parameters 




       connections.Open(); 
       int numRowsAffected = command.ExecuteNonQuery(); 
       connections.Close(); 

       if (numRowsAffected != 0) 
       { 
        Response.Write("<BR>Rows Inserted successfully"); 
        CaseIDDropDownList.ClearSelection(); 
        exhibitTypeTextBox.Text = null; 
        storedLocationTextBox.Text = null; 
        DropDownList1.ClearSelection(); 
       } 
       else 
       { 
        Response.Write("<BR>An error occurred uploading the image"); 
       } 
      } 
      catch (Exception ex) 
      { 
       string script = "<script>alert('" + ex.Message + "');</script>"; 


       } 
} 

잘 모르겠어요. 당신이 많이 도움이 될 실제 예외를 제공 할 수 있다면. 당신이 당신의 데이터베이스/매개 변수 유형에 대한 NULL 전달하려면

+1

AddWithValue는 매개 변수를 varchar로 보내고 형식 검사를하지 않습니다. 이는 모든 매개 변수에 대해 바람직한 동작이 아닐 수도 있습니다. – NotMe

+0

@Chris Lively, 나는이 사실을 알지 못했다. 그러나 그것은 의미가있다. – msarchet

+0

특히 이미지 속성은 내가 추측하고 일부 정수 값을 가지고 있습니다 ... 그래서 Chris는 무엇을 추천합니까? – Selase

관련 문제