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

Last Reply 5 months ago By micah

Posted 5 months 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
    }
}

 

 

Posted 5 months ago Modified on 5 months 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

 

 

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html?aff_id=108