Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C#
public string GetConnectionStrings()
    {
        string ConStr = "server=ACDDXB-PC03\\MSSQL2008;uid=sa;pwd=sa12345;database=DocManager";
        return ConStr;
    }

    public DataTable SelectDataTable(String Sql)
    {
        DataTable dt = new DataTable();
        SqlConnection oSqlConnection = new SqlConnection(GetConnectionStrings());
        try
        {
            oSqlConnection.Open();
            SqlDataAdapter sqlda = new SqlDataAdapter(Sql, GetConnectionStrings());
            sqlda.Fill(dt);
        }

        catch (Exception ex)
        {
           
            throw new Exception(ex.Message);

        }
        finally
        {
            oSqlConnection.Close();
            oSqlConnection.Dispose();
        }

        return dt;
    }


    private string GenerateMenu(DataRow[] drParentMenu, DataTable oDataTable, StringBuilder oStringBuilder)
    {
        string FixedMenuFlg = "";
        if (page == 0)
        {
            oStringBuilder.AppendLine("<ul>");
        }
        else
        {
            oStringBuilder.AppendLine("<ul>");
        }
        if (drParentMenu.Length > 0)
        {
            foreach (DataRow dr in drParentMenu)
            {
                string MenuURL = dr["LinkToPage"].ToString();
                string MenuName = dr["MenuName"].ToString();
                string line = String.Format(@"<li><a href="">{1}</a>", MenuURL, MenuName);
                oStringBuilder.Append(line);
                string MenuID = dr["MenuID"].ToString();
                string ParentID = dr["ParentMenuID"].ToString();
                FixedMenuFlg=dr["FixedMenuFlg"].ToString();
                DataRow[] subMenu = oDataTable.Select(String.Format("ParentMenuID = {0}", MenuID));
                if (subMenu.Length > 0 && !MenuID.Equals(ParentID))
                {
                    var subMenuBuilder = new StringBuilder();
                    oStringBuilder.Append(GenerateMenu(subMenu, oDataTable, subMenuBuilder));
                    oStringBuilder.Append("</li>");

                }
                else
                {
                    if (FixedMenuFlg == "Y")
                    {
                    }
                    else
                    {
                       /*** Here i need to accept the value ***/

                    }
                }
                
            }
        }

        oStringBuilder.Append("</ul>");
        return oStringBuilder.ToString();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //String userid = Session["UserID"].ToString();
            DataTable oDataTable = new DataTable();




            oDataTable = SelectDataTable("SELECT * FROM MstModules Where MstModules.ActiveFlg='Y' And (MstModules.FixedMenuFlg='Y' Or MstModules.ParentMenuID=0 Or Exists (SELECT MstUserGroupDetails.* FROM MstUserGroupDetails RIGHT OUTER JOIN MstUserDetails ON MstUserGroupDetails.UserGroupID = MstUserDetails.UserGroupID AND MstUserGroupDetails.CompanyCode = MstUserDetails.CompanyCode Where MstUserDetails.UserID=1 And MstUserGroupDetails.MenuID=MstModules.MenuID And MstUserGroupDetails.AllowAccessFlg='Y')) Order By MstModules.Priority");

            //if (oDataTable.Rows.Count > 0)
            //{
            //    con.getData("select * from MstModules where LinktoFlg='#' and MenuID=ParentMenuID");
            //    if (con.dt.Rows.Count > 0)
            //    {

            //    }
            //}
            //else
            //{
            //}



            DataRow[] drParentMenu = oDataTable.Select("ParentMenuID = 0");
            var oStringBuilder = new StringBuilder();
            oStringBuilder.Append("<div>");
            string MenuList = GenerateMenu(drParentMenu, oDataTable, oStringBuilder) + "</div>";
            Literal1.Text = MenuList;

           

       
            



        }
    }</ul>
Posted
Updated 19-Oct-14 6:06am
v6
Comments
[no name] 19-Oct-14 7:15am    
Your answer would still be ToString.

Hi,

You can use StringBuilder.ToString() to output the value.

Please see the below sample .

StringBuilder sb = new StringBuilder("This is test");
           sb.Append("Apple");
           sb.ToString();



Thanks
 
Share this answer
 
v2
Comments
Kochathu Thomas Tinu 19-Oct-14 7:12am    
Thanks for the response. A little change in the question. I need to select the current data from stringbuilder when selecting multiple values from database.
Yes. Just examine the
.ToString ()
method
 
Share this answer
 
Comments
Kochathu Thomas Tinu 19-Oct-14 7:12am    
Thanks for the response. A little change in the question. I need to select the current data from stringbuilder when selecting multiple values from database.
CHill60 19-Oct-14 7:16am    
Not sure what you mean...post the code snippet you're having a problem with
[no name] 19-Oct-14 8:19am    
And format your code so it's readable. And actually tell us what the real problem is. The answer to update #3 is still ToString.
CHill60 19-Oct-14 12:08pm    
I still don't get the problem. Which specific line is the problem..."snippet" implies just post the relevant code
Kochathu Thomas Tinu 21-Oct-14 2:42am    
Thanks for response. I solved it myself

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