Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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],[Date & Time],[Data]) "
                        + string.Format("values( '{0}','{1}',@Data )"
                            , fi.Name
                            , dt.ToString()
                            )
                            , 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");
                    textileDataSet3.Tables[0].Rows.Add(fi.Name, dt, data);
//getting error in this underline 
                    textileDataSet3.Tables[0].AcceptChanges();
                    conn.Close();
                }
                catch (Exception a)
                {
                    MessageBox.Show(a.Message);
                }
            }
        }



only if i insert image and pdf throwing error like this.....
Posted
Updated 27-Jan-13 22:52pm
v4
Comments
[no name] 28-Jan-13 5:03am    
Can you specify the error here? And tell us what is "textileDataSet3"?
selva_1990 28-Jan-13 5:31am    
textiledataset is binding source.... conversion failed when converting date and/or time from character string this is the error when adding image

1 solution

Since Image cannot be stored directly we must store it as a binary data. This article may help for your question
Store Images in SQL Server[^]
 
Share this answer
 
Comments
selva_1990 28-Jan-13 4:51am    
i have converted that
vinayraghavendra.hs 28-Jan-13 6:17am    
I guess textileDataSet3 is dataset where u assigning value to it.
selva_1990 28-Jan-13 6:18am    
ya if i insert some of the image are inserting but some showing that error
vinayraghavendra.hs 28-Jan-13 6:22am    
k can you brief the use of textileDataSet3 in ur project.
selva_1990 28-Jan-13 6:25am    
to navigate between data using binding source and binding navigator

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