Click here to Skip to main content
15,905,566 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.....

C#
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

Try:
C#
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! :O
 
Share this answer
 
v2
Comments
selva_1990 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 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 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 3-Feb-13 4:43am    
Ooops! I forgot the double quotes, and used the wrong field - I copied your original.
Answer updated!
selva_1990 3-Feb-13 4:31am    
waiting for reply
Hi,
You can also use the
SQL
GetDate()
in SQL.

SQL
INSERT INTO bill (FileAddedTime,[Bill Name],[Data]) "
                        + string.Format("values( GetDate(), '{0}',@Data )" , fi.Name), conn);

Best Regards
Muthuraja
 
Share this answer
 
Comments
selva_1990 3-Feb-13 4:08am    
i got :) :) its working thank u very much :) :)
Muthuraja Irullandi 3-Feb-13 4:39am    
Hi,
Is that solution is helped?
selva_1990 3-Feb-13 6:31am    
ya good one thank u
 
Share this answer
 
Comments
selva_1990 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 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 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 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 3-Feb-13 4:00am    
http://www.codeproject.com/Questions/535540/conversionplusfailedpluswhenplusconvertingplusdate

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