0

Now I wan to retrieve a datatable value line by line. I had find a way to done it but I can't get the result I want. It only retrieve a line of record. Here is the code :

MySqlConnection connStr = new MySqlConnection();
connStr.ConnectionString = "Server = localhost; Database = healthlivin; Uid = root; Pwd = khei92;";
String searchPerson = "SELECT PersonIDB from contactFriend WHERE PersonID = @id";
MySqlCommand cmdSearch = new MySqlCommand(searchPerson, connStr);
connStr.Open();
cmdSearch.Parameters.AddWithValue("@id", "M000001");


MySqlDataReader dtrRead2 = cmdSearch.ExecuteReader();               
dt.Load(dtrRead2);

dtrRead2.Close();
connStr.Close();


if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
    String s = (String)(DataBinder.Eval(e.Item.DataItem, "PersonID"));
    String val = "";


    for (int i = 0; i < dt.Rows.Count ; i++) 
    {
        //val = dt.Rows[i]["PersonIDB"].ToString();

        if (dt.Rows[i]["PersonIDB"].ToString().Equals(s))
        e.Item.Visible = false;
        else
        e.Item.Visible = true;
    }

EDIT

Page Load :

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                MySqlConnection connStr = new MySqlConnection();
                connStr.ConnectionString = "Server = localhost; Database = healthlivin; Uid = root; Pwd = khei92;";
                String searchOverall = "SELECT PersonID, PersonName, Email, Picture FROM Person";
                MySqlCommand cmdSearch = new MySqlCommand(searchOverall, connStr);
                connStr.Open();

                MySqlDataReader dtrRead2 = cmdSearch.ExecuteReader();
                friendRepeater.DataSource = dtrRead2;
                friendRepeater.DataBind();
                dtrRead2.Close();
                dtrRead2 = null;

                connStr.Close();

            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.ToString());
            }            
        }             
    }

For the ItemDataBound :

protected void ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //String personID = "";
        DataTable dt = new DataTable();

        try
        {
            MySqlConnection connStr = new MySqlConnection();
            connStr.ConnectionString = "Server = localhost; Database = healthlivin; Uid = root; Pwd = khei92;";
            String searchPerson = "SELECT PersonIDB from contactFriend WHERE PersonID = @id";
            MySqlCommand cmdSearch = new MySqlCommand(searchPerson, connStr);
            connStr.Open();
            cmdSearch.Parameters.AddWithValue("@id", "M000001");

            MySqlDataReader dtrRead2 = cmdSearch.ExecuteReader();               
            dt.Load(dtrRead2);

            dtrRead2.Close();
            connStr.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            String s = (String)(DataBinder.Eval(e.Item.DataItem, "PersonID"));
            String val = "";


                for (int i = 0, count = dt.Rows.Count; i < count ; i++) 
                {
                     //val = dt.Rows[i]["PersonIDB"].ToString();
                    //DataRow dr = dt.Rows[i];

                    if (dt.Rows[i]["PersonIDB"].ToString().Equals(s))
                         e.Item.Visible = false;
                     else
                         e.Item.Visible = true;
                }              
        }
    }
4

1 回答 1

0

您没有正确填充中继器。它应该是这样的:

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
    <HeaderTemplate>
        <table>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>'<%# Eval("PersonIDB")%>'</td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

中继器的 itemdatabound 可能如下所示:

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        // Do anything special you want for the item
    }
}

并且不要忘记将数据源绑定到转发器,通常在页面加载时:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        LoadData();
    }
}

您的 LoadData() 方法应如下所示:

private void LoadData() 
{
    DataTable dt = new DataTable();
    MySqlConnection connStr = new MySqlConnection();
    connStr.ConnectionString = "Server = localhost; Database = healthlivin; Uid = root; Pwd = khei92;";
    String searchPerson = "SELECT PersonIDB from contactFriend WHERE PersonID = @id";
    MySqlCommand cmdSearch = new MySqlCommand(searchPerson, connStr);
    connStr.Open();
    cmdSearch.Parameters.AddWithValue("@id", "M000001");

    MySqlDataReader dtrRead2 = cmdSearch.ExecuteReader();
    dt.Load(dtrRead2);

    dtrRead2.Close();
    connStr.Close();

    Repeater1.DataSource = dt;
    Repeater1.DataBind();
}

编辑 :

查看您的编辑后,我建议更改页面加载中的查询字符串:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        try
        {
            MySqlConnection connStr = new MySqlConnection();
            connStr.ConnectionString = "Server = localhost; Database = healthlivin; Uid = root; Pwd = khei92;"; PesronIDB
            //Change query to include PersonIDB               
            String searchOverall = "SELECT P.PersonID, P.PersonName, P.Email, P.Picture, CF.PersonIDB FROM Person P LEFT JOIN contactFriend  CF ON P.PersonID = CF.PersonID";
            MySqlCommand cmdSearch = new MySqlCommand(searchOverall, connStr);
            connStr.Open();

            MySqlDataReader dtrRead2 = cmdSearch.ExecuteReader();
            friendRepeater.DataSource = dtrRead2;
            friendRepeater.DataBind();
            dtrRead2.Close();
            dtrRead2 = null;

            connStr.Close();

        }
        catch (Exception ex)
        {
            //MessageBox.Show(ex.ToString());
        }            
    }             
}

在中继器的 itemdatabound 中,您不需要运行任何查询。

于 2013-11-09T23:34:12.080 回答