Click here to Skip to main content
15,914,452 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have created a table in SQl.
The table is like this..
SQL
Create table filee
(
id varchar(5),
filee nvarchar(max)
)

After that I want to save a (.doc) file in filee column.
I have written a code but this code give error.
The code is like this.....
C#
    FileStream fs = new FileStream(flname, FileMode.Open, FileAccess.Read);
            byte[] data = new byte[fs.Length];
            fs.Read(data, 0, System.Convert.ToInt32(fs.Length));


            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Mith;Integrated Security=True");
            SqlDataAdapter da = new SqlDataAdapter("Select * from filee",con);
            con.Open();
            DataSet ds = new DataSet();
            da.Fill(ds, "filee");
            DataRow dr = ds.Tables["filee"].NewRow();

            dr[0] = textBox1.Text;

            dr[1] = data;

            try
            {
                ds.Tables["filee"].Rows.Add(dr);
                da.Update(ds, "filee");
                MessageBox.Show("Data Inserted");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
          finaly
{
con.close()
}
Posted
Updated 21-Jul-11 16:42pm
v2
Comments
Sandeep Mewara 21-Jul-11 23:10pm    
What error?

HI,
In the above code nowhere ur creating any file, and from nowhere ur reading any file.


Here is the solution :

C#
protected void btnCreate_Click(object sender, EventArgs e)
    {
        FileStream createFile = new FileStream(@"C:\Docs\file2.docx", FileMode.OpenOrCreate, FileAccess.ReadWrite);
        createFile.Close();
        StreamWriter sw = new StreamWriter(@"C:\Docs\file2.docx");
        sw.Write("Tajuddin............");
        sw.Close();
        StreamReader sr=new StreamReader (@"c:\docs\file2.docx");
       string fileData=sr.ReadToEnd ();
       sr.Close();
       //after this u can write insert query to store in file column
       string sqlquery = string.Format("insert into filee values('{0}','{1}')", "123", fileData);
       SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Mith;Integrated Security=True");
       con.Open();
       SqlCommand cmd = new SqlCommand(sqlquery, con);
       cmd.ExecuteNonQuery();
       

    }
 
Share this answer
 
v2
Comments
Maciej Los 11-May-12 8:42am    
Code tags added ;)
make your datatype text in place of nvarchar(max)
 
Share this answer
 

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