-2

Im trying to save a .txt file to my database but i keep on getting the above error, i have tried alot but just cant solve it. Database table=Data. im using SqlServer. fileloc is a string

private void button2_Click(object sender, EventArgs e)
    {

    try
      {
           fileloc = textBox1.Text;
            byte[] doc = null;
            FileStream fs = new FileStream(fileloc, FileMode.Open, FileAccess.Read);
           BinaryReader br = new BinaryReader(fs);
            doc = br.ReadBytes((int)fs.Length);
            string qry = "insert into Data values(@Data)";
                if ( con.State != ConnectionState.Open)
                    con.Open();
                        cmd = new SqlCommand(qry, con);
                      cmd.Parameters.Add(new SqlParameter("@Data", (object)doc));
         int row = cmd.ExecuteNonQuery();
                        if (row > 0)
                        {
                            MessageBox.Show("Doc Added", "Message");
                        }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }


    }

I did the edit but still getting the same error.

Database Conenction

        SqlConnection con = new SqlConnection("Data Source=sochellespencer\\sqlexpress;Initial Catalog=AscottHighSchool;Integrated Security=True");

I SOLVED IT.

I PLACE THE FILE LOCATION INTO A TEXTBOX THEN PASS IT TO fileloc.

Thank you a lot.

  • Pro-tip 1: Do some research on your error message before asking. It's all over the internet. http://stackoverflow.com/search?q=empty+path+name+is+not+legal – Anthony Pegram Jun 11 '13 at 04:15
  • Pro-tip 2: Don't append your UI input to the SQL string like that. It's dangerous. I see you using parameterized queries, but use them *better.* – Anthony Pegram Jun 11 '13 at 04:17
  • Put a break point on the line FileStream fs = new FileStream... RUn the code and use the watch window to examine the fileloc variable. What does it contain? – grantnz Jun 11 '13 at 04:21
  • Where is database connection and File name ? – Shyam sundar shah Jun 11 '13 at 04:47

1 Answers1

0

There is no data originating from richTextBox.Text. You are populating a variable named 'doc' from a file.

If you have a Values clause in your 'insert' statement with a quoted string, there is no point in also have a parameter. Consider the following alternative:

string qry = "insert into Data values(@var1);";
.
.
.
cmd.Parameters.Add(new SqlParameters(@var1, (object)doc)));


If "richTextBox.Text" has the name of the file in it, then that belongs where you have "fileLoc". Nothing in your code indicates how 'fileLoc' ever gets a file name or anything else assigned to it.

Meredith Poor
  • 351
  • 2
  • 9