You must Sign In to post a response.
  • Category: ASP.NET

    How to change the ouput from coumns to rows using string builder in asp.net using c#

    System.Text.StringBuilder sbNews = new System.Text.StringBuilder();
    string myConString = ConfigurationManager.ConnectionStrings["Reception2_DB"].ConnectionString;

    using (SqlConnection conn = new SqlConnection(myConString))
    {
    string query;
    query = Select Subject,Description,Link,Entereddate from LatestNewsMaster and active <>'d'";
    SqlCommand cmd = new SqlCommand(query, conn);
    conn.Open();
    using (SqlDataReader rdr = cmd.ExecuteReader())
    {
    while (rdr.Read())
    {
    sbNews.Append(rdr[3].ToString() + "
    ");
    sbNews.Append(rdr[0].ToString() + "
    ");
    sbNews.Append(rdr[1].ToString() + "
    ");
    sbNews.Append(rdr[2].ToString() + "
    ");

    }
    }
    NewsTicker = sbNews.ToString();
    }

    In database record as follows

    Subject Description Link entereddate
    Defects Defects page Defects.aspx 22Dec2015
    Accounts Accounts Page Accounts.aspx 29Dec2015

    When i run the above code output as follows

    22Dec2015
    Defects
    Defects page
    Defects.aspx

    22Dec2015
    Accounts
    Accounts Page
    Accounts.aspx


    But i want the output as follows in the run mode as follows

    22Dec2015 Defects Defects page Defects.aspx
    22Dec2015 Accounts Accounts page Accounts.aspx

    for that how can i do in asp.net using c#.
  • #763866
    Use string instead of String builder, change your code sample to following

    string szString = "";
    ArrayList szArr = new szArr();
    using (SqlDataReader rdr = cmd.ExecuteReader())
    {
    while (rdr.Read())
    {
    szString = rdr[3].ToString() + " " + rdr[0].ToString() + " " + rdr[1].ToString() + " " + rdr[2].ToString() ;
    //add first row in string arrayList
    szArr.Add(szString);
    }

    //after getting all rows from database
    //just loop on araylist to get your desired output
    foreach(stringitem in szArr)
    {
    //get you data here
    }
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #763870
    Do not go for string. You will face performance issue.

    Try the following


    while (rdr.Read())
    {
    sbNews.Append(rdr[3].ToString() + " ").Append(false).Append(rdr[0].ToString() + " ").Append(false).Append(rdr[1].ToString() + " ").Append(false).Append(rdr[2].ToString() + " ");
    }

    By Nathan
    Direction is important than speed

  • #763871
    Hi Nathan
    You will face performance issue only when you are appending string like below sample

    string sz;
    sz = sz + iCount;

    But In my answer I have not appending string in recursive loop, I am just assign new value to string on each datareader loop. If you want string concatenation then only you should go for stringBuilder otherwise string is ok
    see below link
    http://stackoverflow.com/questions/1825781/when-to-use-stringbuilder
    Hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #763887
    Hai Rao,
    You can just concatanate all your values:

    while (rdr.Read())
    {
    szString = rdr[3].ToString() + " " + rdr[0].ToString() + " " + rdr[1].ToString() + " " + rdr[2].ToString() ;
    }

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


Sign In to post your comments