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 21: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
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,518
1 Tadit Dash 330
2 Manas Bhardwaj 319
3 OriginalGriff 243
4 Sergey Alexandrovich Kryukov 205
0 Sergey Alexandrovich Kryukov 9,530
1 OriginalGriff 5,716
2 Peter Leow 4,345
3 Maciej Los 3,540
4 Abhinav S 3,373


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