I am trying to find out if the file that is being uploaded already exists in the server. And one way for me to do this is to check if the same file name exists. However, somethig seems to not be correct in the code. I have run the query using SSMS and it works, but when I put it into Visual Studio, my return type is SQL.Data.Command and not the actual string itself. Can someone please point me in the right direction?
if (coda_file_upload.HasFile)
{
coda = Path.GetFileName(filePath); //gets the file name
using (connection = new SqlConnection(connection_string))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM name_table WHERE file_name LIKE '%"+coda+"%' ";
cmd.Connection = connection;
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
string sample = Convert.ToString(cmd);
if (cmd.ToString().Equals(String.Empty))
{
coda_file_upload.SaveAs(Server.MapPath("~/") + coda);
input_data = System.IO.File.ReadAllText(Server.MapPath("~/") + coda);
parse_CODA(input_data, coda);
testing.Text = "Success";
}
else
testing.Text = "File exists, please try another file.";
}
My 'else' gets executed every time instead of the if. To double check, I printed out the query to 'string sample' and thats when I see that the value being returned is SQL.Data.Command