Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying upload a file path to a specific record where = txtBox.text

C#
void UploadSOW()
        {
            string filePath = Server.MapPath("~/Uploads/" + FileUpload1.FileName);

            FileUpload1.SaveAs(filePath);
            lblResult.Text = "Your file was successfully uploaded!" + Path.GetFileName(FileUpload1.FileName).ToString();

            SqlCommand cmd = new SqlCommand();
            con.Open();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "tblSystem_Upload_SOW";
            cmd.Parameters.AddWithValue("@SOW", FileUpload1.FileName);
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            con.Dispose();
            con.Close();

            FillSysGrid();

        }


What I have tried:

I have tried to look all over the internet for some hint.
Posted
Updated 4-Jul-22 7:03am
v2

You can't add a WHERE clause to a call of a stored procedure: the WHERE clause is only applicable to some SQL statements like SELECT and UPDATE - it won't work at all with an INSERT for example.

If you want a WHERE clause, you need to change the SP to include it where relevant, and pass the data to compare as a parameter to the SP when you call it.
 
Share this answer
 
You don't.

If you're calling a stored procedure, you have to pass any parameters required by the stored procedure. That means passing values, not a WHERE clause. The stored procedure can then use that value in a WHERE clause, if it's properly written.
 
Share this answer
 
Actually technically you can pass a where clause to an SP - you would need to pass the where clause as a text parameter then construct the SQL statement within the SP as dynamic SQL.
If you google Dynamic SQL for a solution but also google SQL Injection (which is a potential drawback) for solutions on how to protect against this.
 
Share this answer
 
Thank you all for the help. I found a work around although it may not be the ideal or correct approach but it does what I need for school project. I just update the column with the file path where it equals to sysName.

void UploadSOW()
        {
            string filePath = Server.MapPath(@"~/Uploads/" + FileUpload1.FileName);

            FileUpload1.SaveAs(filePath);
            lblResult.Text = "Your file was successfully uploaded!" + Path.GetFileName(FileUpload1.FileName).ToString();

            con.Open();

            SqlCommand cmd = new SqlCommand(
                "UPDATE tblSystem " +
                "SET sysSOW = '"+FileUpload1.FileName+"' " +
                "WHERE sysName = '"+txtSysName.Text+"'", con);

            cmd.ExecuteNonQuery();
            cmd.Dispose();
            con.Dispose();
            con.Close();

            FillSysGrid();

        }
 
Share this answer
 
Comments
CHill60 5-Jul-22 14:10pm    
Your sql command is vulnerable to SQL Injection attack. You were using command parameters earlier, you should still be using them

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900