2013-03-19 3 views
0

데이터베이스에 데이터를 보내는 데 사용하는 다른 클래스의 메서드가 있습니다. 그 방법은 here입니다.ListBox를 반복 할 때 오류 : 지정된 캐스트가 유효하지 않습니다.

public Int32 AddOrder(clsStock NewItem) 
{ 
    //this function takes the data passed via NewItem 
    //and passes it to the parameters for the stored procedure 
    // 
    //create an instance of the data dictionary 
    clsDataDictionary DD = new clsDataDictionary(); 
    //create an instance of the object class 
    Int32 ReturnValue; 
    //create an instance of the data conduit 
    clsDataConduit Items = new clsDataConduit(); 
    //pass the data for this address 
    Items.AddParameter(DD.sproc_tblOrders_Add_AuthId, NewItem.AuthId); 
    Items.AddParameter(DD.sproc_tblOrders_Add_ItemId, NewItem.ItemId); 
    Items.AddParameter(DD.sproc_tblOrders_Add_DateOrdered, NewItem.DateOrdered); 
    Items.AddParameter(DD.sproc_tblOrders_Add_Cancel, NewItem.Cancel); 
    //execute the stored procedure 
    ReturnValue = Items.Execute(DD.sproc_tblOrders_Add); 
    //return the primary key value 
    return ReturnValue; 
} 

난 내 목록 상자를 통해 반복하고 목록 상자에서 각 항목에 대해 그 방법을 실행하는 데 사용하고 내 aspx 페이지에 방법뿐만 아니라 here입니다.

protected void btnSubmit_Click1(object sender, EventArgs e) 
{ 
    //create an instance of the collection class 
    clsStockCollection Items = new clsStockCollection(); 

    foreach(int id in lstAdded.Items) 
    { 
     TheItem.AuthId = 5; 
     TheItem.ItemId = Convert.ToInt32(lstAdded.Items[id].Value); 
     TheItem.Cancel = "false"; 
     Items.AddOrder(TheItem); 
    } 
    Response.Redirect("Order.aspx"); 
} 

내 웹 사이트를 실행하고 다음과 같은 오류주고 년대 btnSubmit에 충돌하는 경우 :

aspx 페이지의 방법에

"Specified cast is not valid" (2 페이스트 빈 파일)

어떤 생각 이유를 뭐야?

+0

의 더 나은 당신이 코드 블록 :이 응답에 대한 – Habib

답변

1

그것은이

foreach(ListItem item in lstAdded.Items) 
{ 
    TheItem = new clsStock(); 
    TheItem.AuthId = 5; 
    TheItem.ItemId = Convert.ToInt32(item.Value); 
    TheItem.Cancel = "false"; 
    Items.AddOrder(TheItem); 
} 
+0

이것이 작동하는 방식입니다! everyones 입력 주셔서 감사합니다. 나는 충분한 평판이 없다. 그렇지 않으면 나는 모든 답을 upvoting 할 것이다. – JARRRRG

0

ListBox.Itemsint 유형 필드로 반복합니다. 의에서 대신에게 단일 항목

foreach(var id in lstAdded.Items) 
{ 
    TheItem.AuthId = 5; 
    TheItem.ItemId = Convert.ToInt32(id.Text); //Change here 
    TheItem.Cancel = "false"; 
    Items.AddOrder(TheItem); 
} 

현재 당신이 foreach 루프 인덱스로 고려하고 나타납니다 ListBox.Items는 같은 var 키워드를 사용하여 암시 적으로 형식화 된 변수를 사용한다 할 수있는 무엇 ListItemCollection입니다 lstAdded

+0

감사합니다 귀하의 질문에 코드를 게시 할 경우 @Habib과 같이해야한다 나는 너도 내 질문을 편집 한 것으로 나타났다. 너무 많은 코드가 아닐 때 코드 블록에 내 코드를 게시하는 것에 대해 염두에 두겠습니다. – JARRRRG

관련 문제