locked
How to get sql query which stored in database? RRS feed

  • Question

  • User2144323856 posted

    I got a treeview which bind with database in behind code, just as below. All the page name, url are get from database.Then, after redirect to the page, there was a gridview in the page... i want to use sql query to get the data for the gridview, but i want to use the sql query that already stored in database.  How to do it?

     protected void Page_Load(object sender, EventArgs e)
            {     

    LoadUser();

    }

      private void LoadUser()
            {
          
                string SubModuleID, SubModuleNM, DocModuleID, DocModuleName, DocS1ID, DocS1Name;
                SqlConnection con = new SqlConnection(ConString);
                string CmdString = "SELECT SUBMODULEID, SUBMODULENM, URLNAME FROM SUBMODULE";
                SqlCommand cmd = new SqlCommand(CmdString, con);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                TreeNode node1 = new TreeNode();
                node1.Text = "Users";
                TreeView5.Nodes.Add(node1);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    SubModuleID = dt.Rows[i]["SUBMODULEID"].ToString();
                    SubModuleNM = dt.Rows[i]["SUBMODULENM"].ToString();
                    TreeNode SubModuleNode = new TreeNode(SubModuleNM, SubModuleID);
                    SubModuleNode.NavigateUrl = dt.Rows[i]["URLNAME"].ToString();

                    node1.ChildNodes.Add(SubModuleNode);

                    CmdString = "SELECT DDID, DOCNAME, DDADDRESS FROM DOCMODULE WHERE SUBMODULEID=@SUBMODULEID";
                    cmd = new SqlCommand(CmdString, con);
                    cmd.Parameters.AddWithValue("@SUBMODULEID", SubModuleID);
                    sda = new SqlDataAdapter(cmd);
                    DataTable dt2 = new DataTable();
                    sda.Fill(dt2);

                    for (int j = 0; j < dt2.Rows.Count; j++)
                    {
                        DocModuleID = dt2.Rows[j]["DDID"].ToString();
                        DocModuleName = dt2.Rows[j]["DOCNAME"].ToString();
                        TreeNode DocModuleNode = new TreeNode(DocModuleName, DocModuleID);
                        DocModuleNode.NavigateUrl = dt2.Rows[j]["DDADDRESS"].ToString();

                        SubModuleNode.ChildNodes.Add(DocModuleNode);

                        CmdString = "SELECT DID, DNAME, DADDRESS FROM DOCUMENTS1 WHERE DDID=@DDID AND DID=@DID";
                        cmd = new SqlCommand(CmdString, con);
                        cmd.Parameters.AddWithValue("@DDID", DocModuleID);
                        cmd.Parameters.AddWithValue("@DID", "390");
                        sda = new SqlDataAdapter(cmd);
                        DataTable dt3 = new DataTable();
                        sda.Fill(dt3);

                        for (int k = 0; k < dt3.Rows.Count; k++)
                        {
                            DocS1ID = dt3.Rows[k]["DID"].ToString();
                            DocS1Name = dt3.Rows[k]["DNAME"].ToString();
                            TreeNode MenuTransNode = new TreeNode(DocS1Name, DocS1ID);
                            MenuTransNode.NavigateUrl = dt3.Rows[k]["DADDRESS"].ToString();
                            DocModuleNode.ChildNodes.Add(MenuTransNode);
                        }

                        if (DocModuleNode.ChildNodes.Count == 0)
                        {
                            SubModuleNode.ChildNodes.Remove(DocModuleNode);
                        }
                    }
                    if (SubModuleNode.ChildNodes.Count == 0)
                    {
                        node1.ChildNodes.Remove(SubModuleNode);
                    }
                }
            }

    Tuesday, April 6, 2021 6:40 AM

Answers

  • User-1716253493 posted

    ID, TITLE, DADDRESS, SQLPROGRAM

    Pass id as querystring then get SQLPROGRAM column value based the ID

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 6, 2021 3:06 PM
  • User-1716253493 posted

    Something like this

    DocModuleNode.NavigateUrl = String.Format("{0}?id={1}",dt2.Rows[j]["DDADDRESS"], dt2.Rows[j]["ID"]);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 7, 2021 12:59 PM

All replies

  • User-1716253493 posted

    ID, TITLE, DADDRESS, SQLPROGRAM

    Pass id as querystring then get SQLPROGRAM column value based the ID

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 6, 2021 3:06 PM
  • User2144323856 posted

    yeah i think so too, but can u give any example of how to get the id of the selected treeview node?

    Wednesday, April 7, 2021 3:14 AM
  • User-1716253493 posted

    Something like this

    DocModuleNode.NavigateUrl = String.Format("{0}?id={1}",dt2.Rows[j]["DDADDRESS"], dt2.Rows[j]["ID"]);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 7, 2021 12:59 PM
  • User2144323856 posted

    That's help me alots .. Thanks bro !

    Thursday, April 8, 2021 12:39 AM