This is a migrated thread and some comments may be shown as answers.

Upload image to SQL as image data

21 Answers 337 Views
Upload
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 02 Mar 2011, 11:20 AM
Hi there.

The tutorials show how you can save images on the server side and then store the image. What I want to do is store the image into an SQL database which has the field type 'image'.

How can I do this?

Thanks a lot.

21 Answers, 1 is accepted

Sort by
0
Koti
Top achievements
Rank 1
answered on 04 Mar 2011, 11:47 AM
Hi,

I am using the Rad Upload control and i followed as same in the example, i am getting the error in the following properties

UploadServiceUrl="{StaticResource UploadServiceUrl}"
TargetFolder="{StaticResource TargetFolder}"

Any one suggest me about the properties.

Regards,
Koti Reddy.
0
Steve
Top achievements
Rank 1
answered on 04 Mar 2011, 11:57 AM
I can help you with that Koti. You need to set those.

Check this out:

http://www.telerik.com/help/silverlight/radupload-features-working-with-radupload.html

But if anyone knows how to upload to an SQL database image type field (using a DDS with Ria) rather than an upload folder, please share this with me.
0
Missing User
answered on 04 Mar 2011, 12:24 PM
Hello all!

I'm using the RadUpload too and i manage how to save a file (any file) as a binary in Sql Server but the RadUpload is saving in my server a local copy of the uploaded file. Can i do something to only save in database not in local server? Thank You.

0
Steve
Top achievements
Rank 1
answered on 04 Mar 2011, 12:36 PM
Hey Joao Paulo.

Could you explain to me how you did that? I'm a bit new, so making the instructions clear would be good, thanks!
0
Missing User
answered on 04 Mar 2011, 12:58 PM
Hi John! Here is my Method who handle the Uploaded File. I'm quiet a noob too so if i can't explain too you.. sorry but i'll try.

private void RadUploadArquivos_FileUploadStarting(object sender, Telerik.Windows.Controls.FileUploadStartingEventArgs e)
        {
             
            RadUploadSelectedFile file = e.SelectedFile as RadUploadSelectedFile;
            //WriteableBitmap wb;
 
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                byte[] buffer;
                using (Stream Source = file.File.OpenRead())
                {
                    int bufferSize = Convert.ToInt32(Source.Length);
                    buffer = new byte[bufferSize];
                    Source.Read(buffer, 0, bufferSize);
                }
                 
                e.NewFileStream = new MemoryStream(buffer);
                PRJ_RDVDespesaAnexo anexo = new PRJ_RDVDespesaAnexo();
                anexo.RDVDespesaAnexoID = Guid.NewGuid();              
 
                MemoryStream wmstream = e.NewFileStream as MemoryStream;
                BinaryWriter bwriter = new BinaryWriter(wmstream);
                bwriter.Write(true);
                byte[] array = wmstream.ToArray();
                bwriter.Close();
                wmstream.Close();
 
                //Here i mount my Database Object with the values
                Binary b = new Binary(); //create a binary instance
                b.Bytes = array; //the b receives the array with the byts of the file
                anexo.Anexo = b;// my Database Object axexo.Anexo receives the file in bynary
                anexo.NomeArquivo = file.Name; //get the name of the file uploaded
                anexo.ExtensaoArquivo = file.File.Extension; //get the extention with the dot (.jpg)
 
                //Here a call my Method on ViewModel to save the File into a database
                (DataContext as AnexosViewModel).AtualizarDespesaAnexo(anexo);
 
            });
 
 
            e.FileParameters.Add("UploadedFiles", this.filenamesContainer);
        }
0
Steve
Top achievements
Rank 1
answered on 04 Mar 2011, 01:31 PM
Hey Joao.

That's some pretty good code there, but some questions if I can. I will number them... so if you can't answer a question feel free to ignore it.

1. What is this BeginInvoke method and why is it used?
2. What is thePRJ_RDVDespesaAnexo class?
3. What is a Guid?

0
Missing User
answered on 04 Mar 2011, 01:48 PM
Hi John!

I'll try to answer..

1- I'm Working in a project and this is already done when i fell on this task, but i made some research and i find that BeginInvoke execute the specified delegate. In this project we use Asyncrounous Methods i think the BeginInvoke is used because this. In your Project you can try to comment this.

2 -  In my project we are using LINQ to Class so i have a .dbml file who is my "virtual database" and the PRJ_RDVDespesaAnexo is a table where i saving the uploaded File, but because we using LINQ To Class this become a Class With the Properties.

3 - A Guid is a Unique Identifyer used as a ID. Is likely a auto increment Field but the GUID is more secure and it will never repeat.

Well.. sorry about the explanation and the English, and if you have any other question fell free to ask.

0
Missing User
answered on 04 Mar 2011, 02:05 PM
Well.. i reach what i'm trying to do, just save the uploaded file to a database and don't save a local copy.

i manage that doing the following code just after saving the file into a database

// Now cancel the real upload, i.e. cancel accessing the upload handler.
            RadUpload uploader = (sender as RadUpload);
            if (uploader != null)
            {
                // Cancel the upload process:
                uploader.CancelUpload();
 
                // Because of the feedback/status information we need this code too.
                // Clear the Upload Title.
                Dispatcher.BeginInvoke(new Action(() => { uploader.CancelUpload(); }));
            }

After saving into a Database this code cancel the upload to a server.

Now.. i'm getting another problem, when i upload files larger than 15kb i get a error

The remote server returned an error: NotFound.

I'm using WCF services and i think is something with the time or the limit of the file. But i don't know how to fix this.
0
Steve
Top achievements
Rank 1
answered on 04 Mar 2011, 04:23 PM
I see you started using the WriteableBitmap there.

What happened? Problems?

In fact let me ask you this if I may. Can you convert e.SeletedFile into a BitmapSource? Also... do you have to wait for the upload to finish before you can convert it to a stream?
0
Missing User
answered on 04 Mar 2011, 06:26 PM
I found some samples with the WriteableBitmap, i give it a try but i don't know much how to work with that so because this i quit using it.

And.. i think no, as you see in my code i convert to a stream in the Starting method.. the file are not uploaded yet.
0
Steve
Top achievements
Rank 1
answered on 07 Mar 2011, 01:28 PM
Hey man.

What is this Binary type and what reference/namespace to I need to use to get it? It says System.Data.Linq in the help files, but if I try to add the reference it tells me it wasn't compiled for Silverlight 4 and won't let me use it.
0
Missing User
answered on 07 Mar 2011, 02:03 PM
It's a Class where i implement a bynary property
public partial class Binary : object, System.ComponentModel.INotifyPropertyChanged {
         
        private byte[] BytesField;
         
        [System.Runtime.Serialization.DataMemberAttribute()]
        public byte[] Bytes {
            get {
                return this.BytesField;
            }
            set {
                if ((object.ReferenceEquals(this.BytesField, value) != true)) {
                    this.BytesField = value;
                    this.RaisePropertyChanged("Bytes");
                }
            }
        }
         
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
         
        protected void RaisePropertyChanged(string propertyName) {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null)) {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }
0
Steve
Top achievements
Rank 1
answered on 07 Mar 2011, 06:01 PM
Hi there.

I come to you with another problem on the same subject, and since you've helped me perhaps you can help me again.

Okay, I have a user table and that type is HydUser. So we have something like:

HydUser user = new HydUser();
user.Name = "Pete";
user.AccessLevel = 2;

Now what I want is a unique ID. I saw you did it before with the Guid, but my UserID field is of type int and it tells me that it can't impicitly convert System.Guid to int. I tried a conversion using Convert.ToInt32, but that didn't work.

Any ideas?
0
Missing User
answered on 07 Mar 2011, 06:13 PM
Hi John!
First things first, i will explain the GUID type for you:

In my table i have a field called PersonID and this field is type of uniqueidentifier.
This kind of type generate a value like this :
6F9619FF-8B86-D011-B42D-00C04FC964FF
It's very secure and it's value never repeat, every time that you do this
user.UserID = Guid.NewGuid();
a New value is created.

You say that you have a field in your table called UserID and it's type is Int correct?
If you are using SQL Server you can create a Identity field and every time that you insert a new User hes values increments 1 by 1 by default

you can do this in SQL using this command
UserID int identity

And about the convertion error, it's since you have a Int type you cannot convert to a Guid. If you use the Identity in your Data Base
you don't need to set the value for the ID because when you insert a new user automaticaly the ID will be generated by SQL Server.

You can use this 2 ways to made a ID field that never will repeat. Visit this page to learn more about Identity Columns
http://www.sqlteam.com/article/understanding-identity-columns

Hope that helps.




0
Steve
Top achievements
Rank 1
answered on 08 Mar 2011, 10:05 AM
You have my thanks yet again.
0
Steve
Top achievements
Rank 1
answered on 08 Mar 2011, 10:45 AM
Hey Joao, I come to you again to ask for your advice. I hope you can offer some more useful information.

Okay, so I have the binary data in the database and I have a Telerik RadGridView on my page. I want to show the images in an image column. I've bound the column to the database entry, but it's given an error. I believe this is because I need an image and not binary data for this column right?

The problem is that the grid view is automated. Can I use an event to convert the data? If so, which one?

Thanks a lot!
0
Missing User
answered on 08 Mar 2011, 03:34 PM
Hi Again!

I create a sample project that show you how to do that. I don't know if this is the better way, because i only need to do this 1 time so..

You can check these Links, to learn more..

http://social.msdn.microsoft.com/forums/en-US/Vsexpressvcs/thread/b818de10-9dbb-4ca4-9c31-8f3305713eee/

http://www.aspfree.com/c/a/ASP.NET/Retrieving-Images-from-a-Database--C---Part-II/

the Project is in WPF but i think you can use the same approach in SilverLight:

http://www.mediafire.com/?ss9gj9kz0o812jv

Hope that Helps.
0
Steve
Top achievements
Rank 1
answered on 08 Mar 2011, 05:26 PM
Hey Joao.

I mean that looks more like ASP.net than Silverlight. Luckily I've had some luck with converters so I think it's okay.

Thanks a lot!
0
Missing User
answered on 08 Mar 2011, 06:07 PM
Hi John

Can you Share with us how you did that?
0
Steve
Top achievements
Rank 1
answered on 11 Mar 2011, 12:36 PM
Hey buddy.

Sorry for not replying. Do you still want an answer? I'll keep my eye on this thread today to see if you reply.
0
Missing User
answered on 11 Mar 2011, 09:32 PM
Heyy!

Sure! Now i need to Download the storade file, i follow this topic:

http://mariaevert.dk/thesis/?p=820

But when i save the file on my Hard Disk the file doesn't open. I try with Images, Docs, PDF always say that the file is corrupted.

Need a little Help hehe..

See ya.
Tags
Upload
Asked by
Steve
Top achievements
Rank 1
Answers by
Koti
Top achievements
Rank 1
Steve
Top achievements
Rank 1
Missing User
Share this question
or