Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an interesting scenario.. i am working on a web-based project on visual studio 2012 and using MySQL server 2012 as the database.

I have User Messages in Stored in the database. The Job is to load the recent 5 messages from database and showed in the 5 text boxes respectively as the page load. You can say just like Twitter.

I have Succeeded in fetching the 5 recent messages of a particular user. Now How to show these 5 MSG in different 5 textboxes.

C#
  protected void Page_Load(object sender, EventArgs e)
    {
DBClass db1 = new DBClass();
    // Variables 
    int tempidd = 87;
        try {
        db1.sqlcon.Open();
        string sqlquery = "SELECT TOP 5 Msg FROM TempNoti WHERE UserID = @U  ORDER BY MsgDate DESC";
        db1.sqlcmd = new SqlCommand(sqlquery, db1.sqlcon);
        if (db1.sqlcon.State == ConnectionState.Closed)
        { db1.sqlcon.Open(); }
        db1.sqlcmd.Parameters.Add("@U", SqlDbType.Int).Value = tempidd ;
        SqlDataReader sqldr = db1.sqlcmd.ExecuteReader();
        do
        {
            int count = sqldr.FieldCount;
            while (sqldr.Read())
            {
                    switch (count)
                    {
                        case 0:
                            TextBox0.Text = Convert.ToString((sqldr.GetValue(count)));
                            break;
                        case 1:
                            TextBox1.Text = Convert.ToString((sqldr.GetValue(count)));
                            break;
                        case 2:
                            TextBox2.Text = Convert.ToString((sqldr.GetValue(count)));
                            break;
                        case 3:
                            TextBox3.Text = Convert.ToString((sqldr.GetValue(count)));
                            break;
                        case 4:
                            TextBox4.Text = Convert.ToString((sqldr.GetValue(count)));
                            break;
                        case 5:
                            TextBox5.Text = Convert.ToString((sqldr.GetValue(count)));
                            break;
                        default:
                            Response.Write("Eroro");
                            break;
                    }  
            }
        } while (sqldr.NextResult());
            }
            catch (Exception ee)
            {
                Response.Write(ee);
            } 
 


        //if (Session["TempID"] != null)
        //{
        //    //Retrieving UserName from Session
        //    tempidd = Convert.ToInt32(Session["TempID"]);
        //}
        //else
        //{
        //    //Do Something else
        //}
    }



But it only displays the most recent msg in the texbox0..
Posted

1 solution

Do you feel the deifference between rows and columns?

SqlDataReader.GetValue(columnindex) returns the value of the specified column. If you specify count (in this case is equal to zero, because in SELECT statement you specify only one column), the data are entered only to TextBox0 control.


C#
int count = 0;
while(sqlreader.Read())
{
    Control mTextBox = FindControl("TextBox" + count.ToString());
    mTextBox.Text = sqlReader.GetValue(0).ToString();
    count+=1;
}


For further information, please see:
SqlDataReader class[^]
Control.FindControl Method (String)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900