如何从存储过程中对转发器进行数据绑定?

时间:2011-11-11 21:09:38

标签: asp.net vb.net repeater data-binding

我正在尝试对我的中继器进行数据绑定,但到目前为止还没有任何运气。有谁认为可以告诉我哪里出错了?我现在有两个函数,通过一些教程/示例,但我希望只有一个...也许不可能。谢谢!

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

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分:1)

按照步骤

  1. 创建名为“SelectPersonalDetails”

    的存储过程

    CREATE PROCEDURE SelectPersonalDetails

    - 在此处添加存储过程的参数

    @Email SYSNAME

    AS
    
    BEGIN
    

    - 添加SET NOCOUNT ON以防止出现额外的结果集 - 干扰SELECT语句。

    设置NOCOUNT ON;

    - 在此处插入程序声明

    开始尝试 开始交易

    BEGIN

    SELECT Name,Title,Phone,Email FROM PersonalDetails

    WHERE 电子邮件= @Email

    END

    COMMIT TRANSACTION

    结束尝试

    BEGIN CATCH

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

    END CATCH     END

  2. 创建数据集以便在转发器中绑定数据。

    public DataSet 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;
        }
    }
    
  3. 注意:(i)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

      的用户控制页面

            /立GT;       /立GT;       /立GT;       /立GT;

    2. 上面注意我有转发器的html代码,但我不知道如何在这个编辑器中解决。无论如何,转发器ID与转发器相同,标签ID与您的标签ID相同。

      1. 数据绑定

        创建一个绑定数据的函数

        public void FillArray(ArrayList alist)     {         ArrayList al = new 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. 现在名为Item databound

        if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)         {             string [] itemArray =((string [])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];
        
        
        
        
            }
        
      3. 如果您觉得答案有用,请将其标记为您的答案,否则请告诉我....