0 Replies - 627 Views - Last Post: 08 April 2015 - 03:10 PM Rate Topic: -----

#1 hyeri  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-March 15

Updatepanel is not working!

Posted 08 April 2015 - 03:10 PM

I'm inserting something in mysql database entered by the user in a pop up modal box and then that text is shown in a treeview. So when the save button is clicked the treeview part should auto refresh showing the recently added text. but it is not working and I still have to refresh the page to see the newly inserted text :/

Here is the code

<asp:ScriptManager  ID="ScriptManager1" runat="server" />
     
        <asp:UpdatePanel runat="server" UpdateMode="Conditional" ID="updatePanel1">
    <ContentTemplate>
<asp:TreeView runat="Server" ExpandImageUrl="~/images/database.png" CollapseImageUrl="~/images/database.png" OnTreeNodePopulate="Node_Populate" ID="tvwauthors" >      
    
<Nodes>

 <asp:TreeNode Text="Existing Database" PopulateOnDemand="true" Value="0"/>

</Nodes>
</asp:TreeView>
</div>
    
				   <div id="dialog-1" title="Add new Database">
      	Database name: <asp:TextBox ID="name" runat="server" ></asp:TextBox>
      	<asp:Button Text="Save" class="btn btn-danger" runat="server" id="saveButton" CausesValidation="false" onclick="SaveDB" style="padding-left:2%;padding-right:2%;" /> 
      </div>

        </ContentTemplate>  
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="saveButton" EventName="Click" />        
    </Triggers>
 </asp:UpdatePanel>     


This is the code that takes values from database and fills in treeview
 public static void FillDatabases(TreeNode node)
    {
        string user = System.Web.HttpContext.Current.User.Identity.Name;
        
        string connString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        MySqlConnection connection = new MySqlConnection(connString);
        MySqlCommand command = new MySqlCommand("Select * From db where Username = '" + user + "';", connection);
        MySqlDataAdapter adapter = new MySqlDataAdapter(command);
        DataSet db = new DataSet();
        adapter.Fill(db);
        if (db.Tables.Count > 0)
        {
            foreach (DataRow row in db.Tables[0].Rows)
            {
                TreeNode newNode = new TreeNode(row["Dbname"].ToString());
                newNode.PopulateOnDemand = true;
                newNode.SelectAction = TreeNodeSelectAction.Expand;
                node.ChildNodes.Add(newNode);
            }
        }
    }




SaveDB code in aspx.cs file (I'm using 3 tier architecture)

 protected void SaveDB(object sender, EventArgs e)
    {

        object objec;
        string user = User.Identity.Name;
        obj.dbname=Convert.ToString(name.Text);
        string dbn = obj.dbname;
        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        MySqlConnection sqlConnection1 = new MySqlConnection(constr);
        
            sqlConnection1.Open();
            MySqlCommand cmd = new MySqlCommand();
            MySqlCommand cm;

            MySqlDataReader reader;
            cmd.CommandText = "Select UserId from Users where Username = '" + user + "';";
            string query = "select count(*) from db where Dbname = '" + dbn + "' and Username = '" + user + "' ;";

            cm = new MySqlCommand(query, sqlConnection1);
            cm.CommandType = CommandType.Text;
            objec = cm.ExecuteScalar(); 
            cmd.CommandType = CommandType.Text;
            cmd.Connection = sqlConnection1;
    
            int rows = 0;

            reader = cmd.ExecuteReader();
            // Data is accessible through the DataReader object here.
            while (reader.Read())
            {
                if (reader["UserId"] != System.DBNull.Value)
                {
                    rows = (int)reader["UserId"];

                }
            }



            sqlConnection1.Close();

        if (Convert.ToInt32(objec) == 0)
        
        {
            
            obj.dbname = Convert.ToString(name.Text);
            obj.userid = rows;
            obj.username = user;
            int result = obj.InsertDb();
            //updatePanel1.Update();
            if (result != 0)
            {
                //updatePanel1.Update();
               // Node_Populate();
                string msg = "<script>alert('Inserted Successfully');</script>";
                ScriptManager.RegisterStartupScript(this, typeof(Control), "alertmsg", msg, false);
            }
            else
            {
                string msg = "<script>alert('Insertion Unsuccessfully');</script>";
                ScriptManager.RegisterStartupScript(this, typeof(Control), "alertmsg", msg, false);
            }
        }
        else
        {
            string msg = "<script>alert('Database already exists');</script>";
            ScriptManager.RegisterStartupScript(this, typeof(Control), "alertmsg", msg, false);
        }

        
    }



Is This A Good Question/Topic? 0
  • +

Page 1 of 1