Amarpreet Kaur
Amarpreet Kaur

Reputation: 39

unable to save file name on database

I am try to save file_name and path of the file on database. but unable to save it :(. database Connection working fine,but i dn't know what is problem

 con.Open();
         string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
         SqlCommand cmd = new  SqlCommand("insertintoTbl_Videos(VideoName,VideoPath)values(@VideoName,@VideoPath)",con);
        if (fileExt == ".avi")
        {
            try
            {
                cmd.Parameters.AddWithValue("@VideoName",  "video/"+FileUpload1.FileName);
                cmd.Parameters.AddWithValue("@VideoPath", "video/" +FileUpload1.FileName);
                FileUpload1.SaveAs(Server.MapPath("~/video/" + FileUpload1.FileName)); 
                Literal1.Text = "upload"; 
   cmd.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                Label1.Text = "ERROR: " + ex.Message.ToString();
            }

        }
        else
        {
            Label1.Text = "Only .avi files allowed!";
        }

    }
        }

Upvotes: 1

Views: 180

Answers (1)

MattW
MattW

Reputation: 4628

You haven't provided the command with a SqlConnection. You need to:

SqlCommand cmd = new  SqlCommand("insertintoTbl_Videos(VideoName,VideoPath)values(@VideoName,@VideoPath)", con);

You also don't actually execute the command anywhere. That also needs to be done explicitly:

cmd.ExecuteNonQuery();

Upvotes: 2

Related Questions