Save files in sql using InfoPath C# - InfoPath Dev
in

InfoPath Dev

Save files in sql using InfoPath C#

Last post 05-11-2010 12:44 AM by alexia_net. 15 replies.
Page 1 of 2 (16 items) 1 2 Next >
Sort Posts: Previous Next
  • 05-10-2010 03:14 AM

    Save files in sql using InfoPath C#

    Hi. I need to save some files into an Sql database. I  have not done this before. Could someone give me some tips about how to do this? Thank you.

  • 05-10-2010 03:23 AM In reply to

    Re: Save files in sql using InfoPath C#

    check this

    Qazi Anis
    Technical Architect
    Bitwise Inc
  • 05-10-2010 03:45 AM In reply to

    Re: Save files in sql using InfoPath C#

     Thank you. In order to upload files into the sql database is it ok to use the File Attachment control from InfoPath or must i use something else? Thank you!

  • 05-10-2010 03:46 AM In reply to

    Re: Save files in sql using InfoPath C#

    do we have any other option, you will have to use file attachment control

    Qazi Anis
    Technical Architect
    Bitwise Inc
  • 05-10-2010 03:51 AM In reply to

    Re: Save files in sql using InfoPath C#

     I was thinking maybe a text box and when you click on it it would open a window to select the file. Just asking. I do have one question about the attachment control. How do i reat the path of the file from it? Thank you!

  • 05-10-2010 04:53 AM In reply to

    Re: Save files in sql using InfoPath C#

     Hi. The solution you have provided here works nicely. Thank you.

    http://www.infopathdev.com/forums/p/15120/54500.aspx

     

  • 05-10-2010 05:30 AM In reply to

    Re: Save files in sql using InfoPath C#

     But how could i read the full path for the file? Thank you.

  • 05-10-2010 05:44 AM In reply to

    Re: Save files in sql using InfoPath C#

    why do u want the full path, you have the byte stream, store it

    Qazi Anis
    Technical Architect
    Bitwise Inc
  • 05-10-2010 06:09 AM In reply to

    Re: Save files in sql using InfoPath C#

     Hi. I have found another method and I want to try it as well. Here it is:

                   FileStream st = new FileStream(@"C:\ppp.xsn", FileMode.Open);
                   byte[] buffer = new byte[st.Length];
                   st.Read(buffer, 0, (int)st.Length);
                   st.Close();

    Its asking for the full path. Thank you!

  • 05-10-2010 06:26 AM In reply to

    Re: Save files in sql using InfoPath C#

    what this method does is it just reads the file and puts it contents in a byte stream, the code in the other thread, does the same, so u can use that.. no need to again read from the file

    Qazi Anis
    Technical Architect
    Bitwise Inc
  • 05-10-2010 06:27 AM In reply to

    Re: Save files in sql using InfoPath C#

     I am trying to store it using your soluiton, but I get an error regarding the field in the sql database. I have defined the field as varbinary(max). Is this the correct format?

    This is the error i get:

    'Implicit conversion from data type varchar to varbinary is not allowed. Use the CONVERT function to run this query.'

    and this is the way i am trying to store it:

                            objSqlCMD = new SqlCommand("INSERT INTO DeviceConfiguration(Field1, Field2, Field3, " +
                                                       " Field4) VALUES('" + tmp_strNameFDC + "','" + tmp_byteFileNameBytes + "', " +
                                                       "'" + strCode + "', 'Yes')", objSqlCONN);
                            objSqlDR = objSqlCMD.ExecuteReader();
                            objSqlDR.Close();

    I have defined

    byte[] byteFileNameBytes = new byte[209715200];

     and tmp_byteFileNameBytes is the same, the parameter of the function I call to store it.

    Thank you.!

  • 05-10-2010 11:00 PM In reply to

    Re: Save files in sql using InfoPath C#

    i thought you were suppose to store files in Blob not in a varchar field.

    Qazi Anis
    Technical Architect
    Bitwise Inc
  • 05-10-2010 11:09 PM In reply to

    Re: Save files in sql using InfoPath C#

     There is no blob type in sql express 2008. I thought that VarBinary would work. How can I save into varbinary? Or is there a way to use blob into sql express? Thank you!

  • 05-10-2010 11:26 PM In reply to

    Re: Save files in sql using InfoPath C#

    try this. it describes how to store byte array into varbinary

    Qazi Anis
    Technical Architect
    Bitwise Inc
  • 05-10-2010 11:54 PM In reply to

    Re: Save files in sql using InfoPath C#

     Thank you Qazi. I have tried it. This is the command:

                            using ( objSqlCMD = new SqlCommand(" INSERT INTO Table(Field1, Field2, " +
                                " Field3, Field4" +
                                " VALUES ('" + tmp_strNameFDC + "', '" + tmp_byteFileNameBytes + "', '" + strCode + "', 'Yes')", objSqlCONN))
                            {
                               
                                objSqlCMD.Parameters.Add("'" + tmp_byteFileNameBytes + "'", SqlDbType.VarBinary, tmp_intFileSize).Value = tmp_byteFileNameBytes;
                                objSqlCMD.ExecuteNonQuery();
                            }

    I have tried it like this as well:

                            using ( objSqlCMD = new SqlCommand(" INSERT INTO Table(Field1, Field2, " +
                                " Field3, Field4" +
                                " VALUES (@'" + tmp_strNameFDC + "',@ '" + tmp_byteFileNameBytes + "', @'" + strCode + "', @'Yes')", objSqlCONN))
                            {
                               
                                objSqlCMD.Parameters.Add("@'" + tmp_byteFileNameBytes + "'", SqlDbType.VarBinary, tmp_intFileSize).Value = tmp_byteFileNameBytes;
                                objSqlCMD.ExecuteNonQuery();
                            }

     I get these errors.

    An error occured: 'Incorrect syntax near 'System.Byte[]'.

                               Incorrect syntax near the keyword 'VALUES'.

     

    I have defined the byte vector like this:

    byte[] byteFileNameBytes = new byte[209715200];

     If I try it like this:

                            using ( objSqlCMD = new SqlCommand(" INSERT INTO Table(Field1, Field2, " +
                                " Field3, Field4" +
                                " VALUES ('" + tmp_strNameFDC + "', @binaryValue, '" + strCode + "', 'Yes')", objSqlCONN))
                            {
                                // Replace 8000, below, with the correct size of the field
                                objSqlCMD.Parameters.Add("@binaryValue", SqlDbType.VarBinary, tmp_intFileSize).Value = tmp_byteFileNameBytes;
                                objSqlCMD.ExecuteNonQuery();
                            }

    I get only this error:

      Incorrect syntax near the keyword 'VALUES'.

    I do not know what could be wrong here. I need your help please. Thank you!

Page 1 of 2 (16 items) 1 2 Next >
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.