2

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");
        }
    }
jarlh
  • 42,561
  • 8
  • 45
  • 63
  • Just *creating* a `SqlCommand` object doesn't execute it. Also, please look into using *parameters* with your SQL, to protect against SQL Injection. – Damien_The_Unbeliever Feb 19 '16 at 10:46
  • 1
    You don't execute the SqlCommand, you just create it: https://msdn.microsoft.com/de-de/library/system.data.sqlclient.sqlcommand%28v=vs.110%29.aspx – Thomas Feb 19 '16 at 10:47
  • where is your execute non query statement with command. something like cmd.ExecuteNonQuery(); refere http://www.codeproject.com/Articles/361579/A-Beginners-Tutorial-for-Understanding-ADO-NET – LifeOfPi Feb 19 '16 at 10:49

1 Answers1

2

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);
Hemal
  • 3,682
  • 1
  • 23
  • 54
  • They don't *need* the first line since the default for `CommandType` is `Text`. – Damien_The_Unbeliever Feb 19 '16 at 10:54
  • Thank you so much Hemal Sir, Howvere new issue, now in database it shows up system.IO.Filestream . How can I get the file name ? Thank you so much again – santhoshkumar B Feb 19 '16 at 11:02
  • 1
    If you dont need to save the full file, why are you using Stream?You can just save filename in a variable and use it it update query. See my update. – Hemal Feb 19 '16 at 11:06
  • awesome..I was totally dumb for trying to insert stream itself..while i knew the logic, i used the method u mentioned. something like this str = "~/Images/dp/" + phone + ".png"; ..Now i can see this in my db, but is it necessary to prefix this ~ symbol before the filepath ? – santhoshkumar B Feb 19 '16 at 11:30