Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL-Server
this is my code to add all kind of doc,pic,etc.. but now i need to add datetime at which time file added that date and time.....
 
 private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
 
                FileInfo fi = new FileInfo(openFileDialog1.FileName);
                byte[] data = new byte[fi.Length];
                //DateTime dt = fi.CreationTime;

                FileStream fs = fi.Open(FileMode.Open, FileAccess.Read, FileShare.Read);
                fs.Position = 0;
                fs.Read(data, 0, Convert.ToInt32(fi.Length));
                SqlConnection conn = new SqlConnection(string.Format("Data Source=SELVA-T;Initial Catalog=textile;Integrated Security=True"
                        , ServerName
                        , DatabaseName)
                        );
 
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("INSERT INTO bill ([Bill Name],[Data]) "
                        + string.Format("values( '{0}',@Data )"
                            , fi.Name), conn);
                    SqlParameter pdata = new SqlParameter("@Data", SqlDbType.Image);
                    pdata.Direction = ParameterDirection.Input;
                    pdata.SqlValue = data;
                    cmd.Parameters.Add(pdata);
                    cmd.ExecuteNonQuery();
                    long si;
                    si = fi.Length / 1024;
                    MessageBox.Show("u inserted the file size of " + si.ToString() + "kb");
                    textileDataSet.Tables[0].Rows.Add(fi.Name,data);
                    textileDataSet.Tables[0].AcceptChanges();
                    conn.Close();
                }
                catch (Exception)
                {
                    MessageBox.Show("both r same file");
                }
            }
        }
Posted 2-Feb-13 22:24pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try:
                    SqlCommand cmd = new SqlCommand("INSERT INTO bill ([Bill Name],[Data],Date1) VALUES (@File,@Data, @Date)", conn);
                    cmd.Parameters.AddWithValue("@File", fi.FileName);
                    cmd.Parameters.AddWithValue("@Data", pdata);
                    cmd.Parameters.AddWithValue("@Date", DateTime.Now);
                    cmd.ExecuteNonQuery();
 
Forgot the double quotes! Blush | :O
  Permalink  
v2
Comments
selva_1990 at 3-Feb-13 3:57am
   
SqlCommand cmd = new SqlCommand("INSERT INTO bill ([Bill Name],[Data],Date) VALUES (@File,@Data, @Date)", conn);
SqlParameter pdata = new SqlParameter("@Data", SqlDbType.Image);
pdata.Direction = ParameterDirection.Input;
pdata.SqlValue = data;
cmd.Parameters.Add(pdata);
cmd.Parameters.AddWithValue(@File, fi.Name);
cmd.Parameters.AddWithValue(@Data, pdata);
cmd.Parameters.AddWithValue(@Date, DateTime.Now);
cmd.ExecuteNonQuery();
 

i changed but showing 6 error
OriginalGriff at 3-Feb-13 4:12am
   
Get rid of the lines:
SqlParameter pdata = new SqlParameter("@Data", SqlDbType.Image);
pdata.Direction = ParameterDirection.Input;
pdata.SqlValue = data;
cmd.Parameters.Add(pdata);
And put your column name in square brackets - "Date" is an SQL reserved word. By preference, change the column name in your database!
SqlCommand cmd = new SqlCommand("INSERT INTO bill ([Bill Name],[Data],[Date]) VALUES (@File,@Data, @Date)", conn);
If that doesn't cure it, tell me what errors it is reporting! (I can't see your screen...)
selva_1990 at 3-Feb-13 4:19am
   
Boss as u said,
i changed the name in DB col...
SqlCommand cmd = new SqlCommand("INSERT INTO bill ([Bill Name],[Data],Date1) VALUES (@File,@Data, @Date)", conn);
cmd.Parameters.AddWithValue(@File, fi.Name);
cmd.Parameters.AddWithValue(@Data, pdata);
cmd.Parameters.AddWithValue(@Date, DateTime.Now);
cmd.ExecuteNonQuery();
 

still 6 error....
 

Error 1 The best overloaded method match for 'System.Data.SqlClient.SqlParameterCollection.AddWithValue(string, object)' has some invalid arguments
Error 2 Argument '1': cannot convert from 'System.IO.File' to 'string'
Error 3 'System.IO.File' is a 'type' but is used like a 'variable'
Error 4 The name 'Data' does not exist in the current context
Error 5 The name 'pdata' does not exist in the current context
Error 6 The name 'Date' does not exist in the current context
OriginalGriff at 3-Feb-13 4:43am
   
Ooops! I forgot the double quotes, and used the wrong field - I copied your original.
Answer updated!
selva_1990 at 3-Feb-13 4:31am
   
waiting for reply
selva_1990 at 3-Feb-13 5:24am
   
working but one error still
 
Error 1 The name 'pdata' does not exist in the current context
OriginalGriff at 3-Feb-13 5:33am
   
I seriously need another coffee!
Take the "p" off...
cmd.Parameters.AddWithValue("@Data", data);
:doh:
selva_1990 at 3-Feb-13 6:11am
   
ha ha ha coooool BOSS working thank u very much :)
OriginalGriff at 3-Feb-13 6:16am
   
You're welcome!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,
You can also use the
GetDate()
in SQL.
 
INSERT INTO bill (FileAddedTime,[Bill Name],[Data]) "
                        + string.Format("values( GetDate(), '{0}',@Data )" , fi.Name), conn);
Best Regards
Muthuraja
  Permalink  
Comments
selva_1990 at 3-Feb-13 4:08am
   
i got :) :) its working thank u very much :) :)
Muthuraja Irullandi at 3-Feb-13 4:39am
   
Hi,
Is that solution is helped?
selva_1990 at 3-Feb-13 6:31am
   
ya good one thank u
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
selva_1990 at 3-Feb-13 3:41am
   
if i use it showing conversion error so i modified to this code.....
http://www.codeproject.com/Questions/535540/conversionplusfailedpluswhenplusconvertingplusdate
 
if possible can u edit this code
Richard MacCutchan at 3-Feb-13 3:45am
   
I have no idea what code you used or what error you received. Please provide proper detailed information of your problem.
selva_1990 at 3-Feb-13 3:54am
   
my code working properly boss but i need to add current date and time of each insertion in another column.... i have tired but i cant so if u can i need your help
Richard MacCutchan at 3-Feb-13 3:58am
   
I say again: show the code that you have tried and the errors you receive, I cannot guess what the problem is. I have shown you how to get the current date so all you need to do is add that to each database item.
selva_1990 at 3-Feb-13 4:00am
   
http://www.codeproject.com/Questions/535540/conversionplusfailedpluswhenplusconvertingplusdate
Richard MacCutchan at 3-Feb-13 4:04am
   
Use the SQL DateTime type, not strings.
selva_1990 at 3-Feb-13 4:08am
   
i ll try thank u

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

  Print Answers RSS
0 OriginalGriff 190
1 Jochen Arndt 165
2 PIEBALDconsult 150
3 BillWoodruff 148
4 DamithSL 140
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 3 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100