2011-11-11 17 views
0

我試圖綁定我的轉發器,但目前爲止沒有任何運氣。任何人都認爲這可以告訴我我要去哪裏錯了?我現在有兩個功能,按照一些教程/例子,但我希望只有一個......也許不可能。謝謝!如何從存儲過程數據綁定中繼器?

HTML:

   <ItemTemplate> 
         <tr class="row"> 
          <td><asp:Label ID="TitleLabel" runat="server" Text=""></asp:Label></td> 
          <td><asp:Label ID="NameLabel" runat="server" Text=""></asp:Label></td> 
          <td><asp:Label ID="PhoneLabel" runat="server" Text=""></asp:Label></td> 
          <td><asp:Label ID="EmailLabel" runat="server" Text=""></asp:Label></td> 
         </tr> 
       </ItemTemplate> 

VB

Protected Sub BindData() 

    Dim oCommand As SqlCommand 
    Dim oReader As SqlDataReader 

    Try 
     oCommand = DataAccess.GetSQLCommand("People_Retrieve", CommandType.StoredProcedure, SourceServer.ConnectionLocal) 
     oCommand.Connection.ChangeDatabase("MyDatabase") 

     oCommand.CommandTimeout() = 9000 
     oReader = oCommand.ExecuteReader() 

     PeopleRepeater.DataSource = oReader 
     PeopleRepeater.DataBind() 

    Catch ex As Exception 
     ErrorHandler.HandleError(ex) 
    Finally 
     oReader.Close() 
     oReader = Nothing 
    End Try 

End Sub 

Protected Sub PeopleRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles PeopleRepeater.ItemDataBound 

    Dim NameLabel As Label = CType(e.Item.FindControl("LabelName"), Label) 
    NameLabel.Text = e.Item.DataItem("Name") 

    Dim TitleLabel As Label = CType(e.Item.FindControl("TitleName"), Label) 
    NameLabel.Text = e.Item.DataItem("Title") 

    Dim PhoneLabel As Label = CType(e.Item.FindControl("PhoneName"), Label) 
    NameLabel.Text = e.Item.DataItem("Phone") 

    Dim EmailLabel As Label = CType(e.Item.FindControl("EmailName"), Label) 
    NameLabel.Text = e.Item.DataItem("Email") 

End Sub 

回答

1

使用SqlDataAdapter

Using adap As New SqlDataAdapter(oCommand) 
    Dim table As New DataTable() 
    adap.Fill(table) 

    PeopleRepeater.DataSource = table 
    PeopleRepeater.DataBind() 
End Using 

我沒有看到你要麼打開連接,所以你可能需要補充一點:

oCommand.Connection.Open() 
+0

在VB.NET轉換工作。 –

1

按照以下步驟

  1. 創建命名爲 'SelectPersonalDetails'

    CREATE PROCEDURE SelectPersonalDetails

    存儲過程 - 添加參數這裏的存儲過程

    @Email SYSNAME

    AS 
    
    BEGIN 
    

    - 添加SET NOCOUNT ON以防止來自 的額外結果集 - 干擾SELECT語句。

    SET NOCOUNT ON;

    - Insert語句這裏程序

    開始嘗試這 BEGIN TRANSACTION

    BEGIN

    SELECT姓名,職務,電話,電子郵件,PersonalDetails

    WHERE 電子郵件= @Email

    END

    COMMIT TRANSACTION

    END TRY

    BEGIN CATCH

    ROLLBACK TRANSACTION 
    DECLARE @ERR AS VARCHAR(500) 
    SELECT @ERR = ERROR_MESSAGE() 
    RAISERROR(@ERR,16,1) 
    
    RETURN 
    

    END CATCH END

  2. 以在中繼器綁定的數據來創建數據集。

    公共數據集Get_PersonaldetailbasedEmail() {

    try 
        { 
         DataSet oDS = new DataSet(); 
         SqlParameter[] oParam = new SqlParameter[1]; 
    
         oParam[0] = new SqlParameter("@Email", _sEmail); 
    
    
         oDS = SqlHelper.ExecuteDataset(DataConnectionString,   CommandType.StoredProcedure, "SelectPersonalDetails", oParam); 
         return oDS; 
        } 
        catch (Exception e) 
        { 
         ErrorMessage = e.Message; 
         return null; 
        } 
    } 
    

注意事項:(一)SelectPersonalDetails是存儲過程的名稱

(ii) In order to select unique record from the table i have used emailid 

    (iii) I have assume the table name as PersonalDetails. 
  1. 創建用戶控制網頁類似Personeldetails .ascx

    /LI> /LI> /LI> /LI>

注意上面我有中繼器的HTML代碼,但我不知道如何在這個編輯器中解決。反正中繼身份證是相同的你的中繼器和標籤ID是相同的標籤ID。

  1. 數據綁定

    創建綁定的數據

    函數

    公共無效FillArray(ArrayList的ALIST) {ArrayList的 AL =新的ArrayList();

    foreach (Object objRow in alist) 
        { 
         string sTitle = ((DataRow)objRow)["Title"].ToString(); 
         string sName = ((DataRow)objRow)["Name"].ToString(); 
         string sPhone = ((DataRow)objRow)["Phone"].ToString(); 
         string sMail = ((DataRow)objRow)["Mail"].ToString();   
    
         al.Add(new string[]{ sTitle,sName,sPhone,sMail}); 
    
    
        } 
        PeopleRepeater.DataSource = al; 
        PeopleRepeater.DataBind(); 
    
    
    } 
    
  2. 現在稱爲檔案數據綁定

    如果(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { 串[] itemArray =( (串[])e.Item.DataItem強制轉換);

     Label myTitle = (Lable)e.Item.FindControl("TitleLabel"); 
         Label myName = (Label)e.Item.FindControl("NameLabel"); 
         Label myPhone = (Label)e.Item.FindControl("PhoneLabel"); 
         Label myEmail = (Label)e.Item.FindControl("EmailLabel"); 
    
         myTitle.Text = itemArray[0]; 
         myName.Text = itemArray[1]; 
         myPhone.Text = itemArray[2]; 
         myEmail.Text = itemArray[3]; 
    
    
    
    
        } 
    

如果你找到答案有用的,請把它標記爲你的答案別人讓我知道....

+0

我認爲這有點過於本地化。我會盡量減少OP的要求。 –

+0

此問題也被標記爲VB.NET。 –