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

发布于 2024-12-15 02:06:22 字数 2085 浏览 2 评论 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

I'm trying to databind my repeater but so far not having any luck. Anyone think that can show me where I'm going wrong? I have two functions at the moment by following some tutorials/examples but I was hoping to have just one... maybe not possible. Thanks!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光是把杀猪刀 2024-12-22 02:06:22

使用 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()

Use a SqlDataAdapter:

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

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

I don't see where you're opening the connection either, so you might need to add that:

oCommand.Connection.Open()
流年里的时光 2024-12-22 02:06:22

按照以下步骤

  1. 创建名为“SelectPersonalDetails”的存储过程

    创建程序选择个人详细信息

    --在这里添加存储过程的参数

    @电子邮件系统名

    <前><代码>AS

    开始

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

    设置不计数;

    -- 在此处插入过程语句

    开始尝试
    开始交易

    开始

    从个人详细信息中选择姓名、职务、电话、电子邮件

    哪里
    电子邮件 = @Email

    结束

    提交交易

    结束尝试

    开始捕捉

    回滚事务
    将 @ERR 声明为 VARCHAR(500)
    选择@ERR = ERROR_MESSAGE()   
    RAISERROR(@ERR,16,1)
    
    返回
    

    结束捕获
    END

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

    公共数据集 Get_PersonaldetailbasedEmail()
    {

    <前><代码>尝试
    {
    数据集 oDS = new DataSet();
    SqlParameter[] oParam = new SqlParameter[1];

    oParam[0] = new SqlParameter("@Email", _sEmail);

    oDS = SqlHelper.ExecuteDataset(DataConnectionString, CommandType.StoredProcedure, "SelectPersonalDetails", oParam);
    返回消耗臭氧层物质;
    }
    捕获(异常 e)
    {
    错误消息 = e.Message;
    返回空值;
    }
    }

注意:(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

    /li>
    /li>
    /li>
    /li>

请注意,上面我有中继器的 html 代码,但我不知道如何在此编辑器中解决问题。无论如何,中继器 ID 与您的中继器相同,标签 ID 与您的标签 ID 相同。

  1. 数据绑定

    创建一个函数来绑定数据

    public void FillArray(ArrayList alist)
    {
    ArrayList al = new ArrayList();

     foreach(列表中的对象 objRow)
        {
            字符串 sTitle = ((DataRow)objRow)["标题"].ToString();
            字符串 sName = ((DataRow)objRow)["名称"].ToString();
            string sPhone = ((DataRow)objRow)["Phone"].ToString();
           字符串 sMail = ((DataRow)objRow)["Mail"].ToString();        
    
            al.Add(new string[]{ sTitle,sName,sPhone,sMail});
    
    
        }
        PeopleRepeater.DataSource = al;
        PeopleRepeater.DataBind();
    
    
    }
    
  2. 现在称为项目数据绑定

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
    string[] itemArray = ((string[])e.Item.DataItem);

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

如果您觉得答案有用,请将其标记为您的答案,否则请告诉我......

Follow the steps

  1. Create the stored procedure named as 'SelectPersonalDetails'

    CREATE PROCEDURE SelectPersonalDetails

    -- Add the parameters for the stored procedure here

    @Email SYSNAME

    AS
    
    BEGIN
    

    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.

    SET NOCOUNT ON;

    -- Insert statements for procedure here

    BEGIN TRY
    BEGIN TRANSACTION

    BEGIN

    SELECT Name,Title,Phone,Email FROM PersonalDetails

    WHERE
    Email = @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. Create dataset in order to bind the data in the repeater.

    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;
        }
    }
    

Note: (i) SelectPersonalDetails is the stored procedure name

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

  (iii) I have assume the table name as PersonalDetails.
  1. Create a user control page something like Personeldetails.ascx

    /li>
    /li>
    /li>
    /li>

Note above i have the html code for repeater but i don't know how to work around in this editor. anyways repeater id is same as your repeater and label ids are same as your label id.

  1. Databind

    Create a function to bind the data

    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. Now called 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];
    
    
    
    
        }
    

If you find the answer useful, please mark it as your answer else let me know....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文