I'm fairly new to C# .Net. We're being taught it at University and are using Visual Studio to create windows forms. As a new part to the subject we're using databases, tables and datasets.
I opened a new Windows Form project and immediately added a new database to it. The table I want to create will have 2 columns - ImageID and the image itself. In what way do i add the image in to the box? I've tried full path, relative path and dragging the image in, but whatever I do i get the same error message....
Invalid Value
The changed value in this cell was not recognized as being valid.
.Net Framework Data Type: Byte[]
Error Message: You cannot use the result pane to set this Field data to
values other than NULL
Type a value appropriate for the data type or press ESC to cancel the
change
How can I have images in there? I just don't know how to use the image data type within the table. Any help is much appreciated.
A simpler approach is to store the image in the file system and only its path in the database. Basically you define a base folder:
string baseFolder = "c:\Program Files\MyApp\Images";
And use it to store relative paths in the database:
INSERT INTO ImagesTable (Name, Path)
Values ('German Shepherd', 'Dogs\german-shepherd.jpg')
Then, when you need to retrieve the image, you can do it like this:
string path = Path.Combine(baseFolder, 'Dogs\german-shepherd.jpg');
Image img = Image.FromFile(path);
In the following SO question you can find more information about the pros and cons of this approach:
Should I store my images in the database or folders?
you can store images in sql server 2008. Just create database table having column datatype "image".
now from .net code use the file upload control to select the image file and then convert the image parameter into byte[] before inserting image data into the database.
Related
I am learning C# including interacting with SQL Server databases. I am stuck however, on adding PictureBox images programmatically to a SQL Server database:
I have created a form with text fields, a picture box, to display/edit/add single records to a database successfully and am successfully using all of the following:
DB Connection = RRConnection
SqlCommand object = playersCommand
SqlDataAdapter object = playersAdapter
dataTable object = playersTable
currencyManager = playerManager
PictureBox1 control = including a button to navigate filesystem and select/load jpeg image into form PictureBox1
Bindings like - txtName.DataBindings.Add("Text", playersTable, "Name") for text fields - I think I need to bind the picturebox1 IMage Byte Array but do not know how to do
When the form closes, the table is loaded into the SQLServer database table tbl_BB into the column named "Image" of type image.
I know I have to convert the image to a byte array to save, but am confused on how to bind the image byte array to the dataTable, allowing it to be uploaded to the database on form close. I've done a lot of googling on it but the solutions do not seem to match the code I am using...
Given the above, can you advise the code necessary for this?
Don't use image it will be removed from MS SQL soon.
https://learn.microsoft.com/en-us/sql/t-sql/data-types/ntext-text-and-image-transact-sql
Just use varbinary.
the general way to do it is:
1) SQL Server will have a varbinary(max) column.
2) Take the jpeg and convert it to a byte array
3) add the byte array as a paramter to your sql command object
4) execute nonquery for insertion.
May I suggest you merely save the image on disk directly, and reference the path to the image in the DB instead? Saving images in a db is considered bad practice.
Eg: (fileIcon)a1.txt , (fileIcon)a2.pdf , (fileIcon)a3.pst Etc...
I have a windows form application, Actually I want to upload some files to listbox with fileIcon with file name and type. I am not getting this task! Help me out.
Thanks....
You can add this line in your OpenFileDialog's Filter under its properties
Image File|*.jpg|Icon files|*.ico
and so on.. It's a set of a file type string and its extension format
Image File|*.jpg
define one type of file type supported, you can use multiple format for a file type name e.g.
Image Files|*.jpg,*.png,*.bmp
Or simply support All files by using following filter, but it is not recommended anyway
All Files|*.*
I have a column in my table stored in MS Access as a Attachment data type. It stores various files such as docx, pdf's etc.
I am trying to display a file from the table using a documentViewer (obtained from XtremeDocumentStudio .NET).
I also have a combo box on my form with a list of employee ID's. When a particular ID is selected from the combo box, I want the associated attachment with that employee to be displayed in the documentViewer.
I am using this query:
SELECT EmployeeAttachment FROM Employee WHERE EmployeeID = 2
I have been stuck on this problem for a while and am not sure on how to implement it. Any help or ideas on how I would do this would be greatly appreciated.
In order to get a faithful copy of the file from the Attachment field in the Access database you need to use the .SaveToFile method of an ACE DAO Field2 object. For details, see the related question:
Extracting files from an Attachment field in an Access database
Once you have extracted the file to disk (e.g., to System.IO.Path.GetTempPath) then you can tell the viewer control where to find it.
CONTEXT: I am writing an application in Visual C# / .Net, where the user can:
Create a new document-file "Untitled.abc"
Add some data/content, e.g., images, text, etc.
Save the document-file as "MyDoc7.abc", where abc would be my app-specific extension.
QUESTION: Is the following method good to implement the above, or is there a simpler/cleaner approach?
I start with a SQL-Server database called MyDb.
Each time a user creates a new document-file, I programatically add a new DB table to MyDb
The newly created DB table stores the data/content from just that particular corresponding document-file created by the user.
When user re-opens the saved document-file in the future, I programatically read the data/content from the attached corresponding DB table.
UPDATE:
Solved. I will be using this Alternative approach:
I start with a SQL-Server database called MyDb with a table called MyTable, including a column called FileId.
Each time a user creates a new document-file, I assign the file a unique id, e.g., FileId = 5.
When the user adds a piece of data/content to the file, I store the piece of data to MyTable, but also store FileId = 5 for that piece of data.
In the future, when the user re-opens the file, I fetch all pieces of data from MyTable where FileId = 5.
That sounds like a particularly bad idea to do that.
You don't want to create tables on the fly
If all the data is in the database, what is the file for?
I would rather choose the approach that Office uses:
The file really is a zip archive which contains:
A folder for the assets (images and the like)
The actual file with the content and relative links to the assets.
I have a column in a SharePoint list that I wish to make a link to a file on the network. The file location is generated in code so I need to write a CAML script to update the column.
Could someone please give me an example of what the value stored in the database would be? In this example the file location is \server\folder\file.txt. I would like the textual name to be the same if possible.
For your link you should use a column of the type "Hyperlink or Picture". This column type can handle a link and a description for it. To set these both values for a this field you would use the following code.
SPFieldUrlValue urlField = new SPFieldUrlValue();
urlField.Description = #"\\server\folder\file.txt";
urlField.Url = #"\\server\folder\file.txt";
yourListItem["yourLinkColumnName"] = urlField;
yourListItem.Update();
SharePoint will automatically convert the link from "\server\folder\file.txt" to "file://server/folder/file.txt". But be aware that SharePoint won't handle the permissions a user needs to access the file. It's just a link.