Search code examples
c#asp.netsql-serverispostback

Am I missing the obvious ? I think I will have to use ispostback here but not understanding how


I am trying to insert file path into SQL Server with an update query. However, I am unable to see the changes despite query running successfully without errors. Here is the code of my asp.net web app. I am trying to insert the stream value in the column of table inside my database, doing this on local machine,

protected void Uplad(object sender, EventArgs e)
    {
        string phone = Session["phn"].ToString();
        string base64 = Request.Form["imgCropped"];
        byte[] bytes = Convert.FromBase64String(base64.Split(',')[1]);
        using (FileStream stream = new FileStream(Server.MapPath("~/Images/dp/" + phone + ".png"), FileMode.Create))
        {
            str = stream.ToString(); 
            con.Open();
            SqlCommand dpcmd = new SqlCommand("update xyz set ab = '" + str + "' where Moile ='" + phone + "'", cnn); //if i mouseover i can see the value of stream as C://xyz.png (cant write full path here) but when i check in db it is not showing up...
            stream.Write(bytes, 0, bytes.Length);
            stream.Flush();
            con.Close();
        }

        display.Visible = false;
    }

I am also able to see the values while compiling, file is also getting saved to the specified folder, but why is database not getting updated? Is it taking the old value only? How can I implement changes accordingly in isPostback? Thank you in advance :)

protected void Page_Load(object sender, EventArgs e)
    {


        try
        {

            if (!IsPostBack) // this block doesnt get executed when above handler is executed...hence i am assigning a value in the else to test
            {
                string phone = Convert.ToString(Session["pn"]);
                oldbi.Text = phone; //old number 
                usrname.Text = Convert.ToString(Session["fname"]); //assigning the name
                nm = Convert.ToString(Session["paw"]);
                if (phone != "")
                {
                    SetInitialRow();
                }
                else
                {
                    Response.Redirect("join.aspx");
                }

            }
            else
            {
                str = "do"; // i tried to assign a value here and i tried inserting this value in update statement but even this is not showing up in DB. I think it is not updating new value
            }
        }
        catch
        {
            Response.Redirect("join.aspx");
        }
    }

Solution

  • You need to place following lines also. What you have done is just created an SqlCommand, but you have not executed it against the connection.

    dpcmd.CommandType=CommandType.Text;
    dpcmd.ExecuteNonQuery();
    

    UPDATE

    If you just want to save filename, then this might help

    string str=phone + ".png";
    SqlCommand dpcmd = new SqlCommand("update xyz set ab = '" + str + "' where Mobile ='" + phone + "'", cnn);