Insert data to Database not working after ASP.Net UpdatePanel PostBack

Last Reply 8 days ago By micah

Posted 9 days ago

After posting data Updata panel not updating to show data

 

<asp:UpdatePanel ID="updResult" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:DataList ID="GetMergedAll" runat="server" DataKeyName="Id" OnItemCommand="GetMergedAll_ItemCommand"
            OnItemDataBound="GetMergedAll_ItemDataBound">
            <ItemTemplate>
                <asp:Label ID="Label36" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Name") %>'
                    ForeColor="#999999" />
                <asp:Label ID="Label36" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Title") %>'
                    ForeColor="#999999" />
            </ItemTemplate>
        </asp:DataList>
    </ContentTemplate>
</asp:UpdatePanel>

 

protected void linkpost_Click(object sender, EventArgs e)
{
    if (this.Page.User.Identity.IsAuthenticated)
    {
        string username = this.Page.User.Identity.Name;
        {
            string tendString = TextBixcomment.Text.Trim();
            string strname = ""; // ADDED
            if (FileUploadImage.HasFile)
            {
                strname = FileUploadImage.FileName.ToString();
                FileUploadImage.PostedFile.SaveAs(Server.MapPath("~/UserImage/") + strname);
            } // <-- ADDED SO THAT WE JUST SAVE THE FILE IF THE FILE IS UPLOADED

            // THEN UNCHANGED WE SAVE THE DATA TO THE DB

            string str = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
            string getADPOST = "Insert INTO USERPost (UserName,ContentPost,ImageName1) values (@UserName,@ContentPost,@ImageName1)";
            // string getADPOST = "Insert INTO CommunityFollow  (MyUserName,CommunityStatus) values (@MyUserName,1)";
            using (SqlConnection con = new SqlConnection(str))
            {

                using (SqlCommand cmd = new SqlCommand(getADPOST, con))
                {
                    // Get Filename from fileupload control
                    //   string filename = Path.GetFileName(FileUploadImage.PostedFile.FileName);
                    //  Save images into Images folder
                    //    Server.MapPath("~/UserImage/" + FileUploadImage.FileName);

                    //  cmd.Parameters.AddWithValue("@ImageName1", filename);
                    //    cmd.Parameters.AddWithValue("@ImageName1", "~/UserImage/" + filename);
                    cmd.Parameters.AddWithValue("@ContentPost", TextBixcomment.Text.Trim());
                    cmd.Parameters.AddWithValue("@UserName", username);

                    cmd.Parameters.AddWithValue("@ImageName1", strname);
                    //cmd.Parameters.AddWithValue("@VideoName", strname);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();

                    //  Response.Redirect("Home.aspx");
                    string message = "Post submitted successfully.";
                    string script = "window.onload = function(){ alert('";
                    script += message;
                    script += "')};";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);
                }
            }

        }
        // } ONE OF THIS DISAPPEARS AS WE CLOSED THE IF BLOCK MUCH EARLIER
    }
}

 

 

Results 1 - 5 of 7 12
Posted 9 days ago
Hi @micah,
Please try the following

jQuery DataTable plugin not working after ASP.Net UpdatePanel PostBack

It might help you.

Cheers Andrea.

Posted 8 days ago Modified on 8 days ago

Hi micah,

I did some modification in your code. So please refer the below code.

HTML

<form id="form1" runat="server">
<div>
    <asp:FileUpload ID="FileUploadImage" runat="server" />
    <asp:TextBox ID="TextBixcomment" runat="server" />
    <asp:LinkButton ID="btnSubmit" OnClick="linkpost_Click" Text="Submit" runat="server" />
    <asp:ScriptManager runat="server" />
    <br />
    <br />
    <asp:UpdatePanel ID="updResult" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:DataList ID="GetMergedAll" runat="server" DataKeyName="Id" OnItemCommand="GetMergedAll_ItemCommand"
                OnItemDataBound="GetMergedAll_ItemDataBound">
                <ItemTemplate>
                    <asp:Label ID="lblName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Name") %>'
                        ForeColor="#999999" />
                    <asp:Label ID="lblTitle" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Title") %>'
                        ForeColor="#999999" />
                </ItemTemplate>
            </asp:DataList>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>
</form>

C#

private string str = ConfigurationManager.ConnectionStrings["Constring"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
    this.GetData();
}

private void GetData()
{
    SqlConnection con = new SqlConnection(str);
    SqlCommand cmd = new SqlCommand("SELECT UserName as Name,ContentPost as Title,Id,ImageName1 FROM USERPost1", con);
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    GetMergedAll.DataSource = dt;
    GetMergedAll.DataBind();
}

protected void linkpost_Click(object sender, EventArgs e)
{
    if (this.Page.User.Identity.IsAuthenticated)
    {
        string username = this.Page.User.Identity.Name;
        {

            string tendString = TextBixcomment.Text.Trim();

            string strname = ""; // ADDED
            if (FileUploadImage.HasFile)
            {
                strname = FileUploadImage.FileName.ToString();
                FileUploadImage.PostedFile.SaveAs(Server.MapPath("~/UserImage/") + strname);
            } // <-- ADDED SO THAT WE JUST SAVE THE FILE IF THE FILE IS UPLOADED

            // THEN UNCHANGED WE SAVE THE DATA TO THE DB

            //string str = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
            string getADPOST = "Insert INTO USERPost(UserName,ContentPost,ImageName1) values (@UserName,@ContentPost,@ImageName1)";
            // string getADPOST = "Insert INTO CommunityFollow  (MyUserName,CommunityStatus) values (@MyUserName,1)";
            using (SqlConnection con = new SqlConnection(str))
            {
                using (SqlCommand cmd = new SqlCommand(getADPOST, con))
                {
                    // Get Filename from fileupload control
                    //   string filename = Path.GetFileName(FileUploadImage.PostedFile.FileName);
                    //  Save images into Images folder
                    //    Server.MapPath("~/UserImage/" + FileUploadImage.FileName);

                    //  cmd.Parameters.AddWithValue("@ImageName1", filename);
                    //    cmd.Parameters.AddWithValue("@ImageName1", "~/UserImage/" + filename);
                    cmd.Parameters.AddWithValue("@ContentPost", TextBixcomment.Text.Trim());
                    cmd.Parameters.AddWithValue("@UserName", "PETER");
                    cmd.Parameters.AddWithValue("@ImageName1", strname);
                    //cmd.Parameters.AddWithValue("@VideoName", strname);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    //  Response.Redirect("Home.aspx");
                    string message = "Post submitted successfully.";
                    string script = "window.onload = function(){ alert('";
                    script += message;
                    script += "')};";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);
                }
            }
        }
        // } ONE OF THIS DISAPPEARS AS WE CLOSED THE IF BLOCK MUCH EARLIER
    }

    this.GetData();
}

Screenshot

 

 

 


Posted 8 days ago Modified on 8 days ago

hi please this is the methode i used to fetch the record on page load, i used procedure. So i tried to modify it i couldnt

 

protected void Page_Load(object sender, EventArgs e)
    {

        if (!this.IsPostBack)
    {
        if (!this.IsPostBack)
        
            {
                username = this.Page.User.Identity.Name;
             
                DataTable dt = GetProfile();
                               
                        GetMergedAll.DataSource = dt;
                ViewState["DataTable"] = dt;
                GetMergedAll.DataSource = dt;
                GetMergedAll.DataBind();              
              
               // if (dt.Rows.Count > 0)
                {
                    
                }
            }
        }
      
    }
    public DataTable GetProfile()
    {
        SqlConnection constr = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        SqlDataAdapter adp = new SqlDataAdapter("GetUserPOSTS", constr);
    adp.SelectCommand.CommandType = CommandType.StoredProcedure;
   // adp.SelectCommand.Parameters.AddWithValue("@Email", username);
    adp.SelectCommand.Parameters.AddWithValue("@UserName", username);
    adp.SelectCommand.Parameters.AddWithValue("@Id", Id);
    DataTable dt = new DataTable();
        //
    adp.Fill(dt);
    return dt;

        
    }


 

 


Posted 8 days ago

just help me modify your select method to use procedure


Posted 8 days ago

Hi micah,

Please refer the below link read carefully.First understand the store procedure than Implement in your project.

Single Stored Procedure for Select Insert Update Delete in SQL Server

I agree, here is the link: http://e-iceblue.com/free-api