Introduction
This is my small contribution from what I learnt in my walk towards VC++ .NET. It's a really simple and very often used scenario: working with DataGrid. Here, I developed a small application which creates database and stores the data (image), and also retrieves data from the database and displays it in the DataGrid.
Code Explanation
Requirements
- MS Visual Studio .NET 2003
- Windows XP
- SQL Server 2000
I used SQL Server for database operations. The application shows how the values (image) are stored in the database and the grid is updated when changes are made in the database. This is the snapshot of my application.

Controls used
Buttons: Create Database, Select Image, Save to Database
PictureBox: PictureBox1
DataGrid: Datagrid1
Create the database
When you click the button 'Create Database', the table called Picture would be created in the SQL Server NorthWind database. If it is already present, it would be deleted and created again.
String* strSQL =
S"IF EXISTS (SELECT * FROM northwind.dbo.sysobjects WHERE NAME "
"= 'Picture' AND TYPE = 'u')\r\nBEGIN\r\nDROP "
"TABLE northwind.dbo.picture\r\nEND\r\nCREATE TABLE "
"Picture (PictureID Int IDENTITY(1,1) NOT NULL,[FileName] "
"Varchar(255) NOT NULL,Picture Image NOT NULL,"
"CONSTRAINT [PK_Picture] PRIMARY KEY CLUSTERED(PictureID))";
Try
{
SqlConnection* conCreate = new SqlConnection(strconString);
SqlCommand* sqlCmd = new SqlCommand(strSQL,conCreate);
conCreate->Open();
sqlCmd->ExecuteNonQuery();
conCreate->Close();
MessageBox::Show(S"Table is Created Successfully",S"Table
Creation",MessageBoxButtons::OK,MessageBoxIcon::Information);
}
catch(System::Exception* ee)
{
MessageBox::Show(ee->Message,S"Error",MessageBoxButtons::OK,
MessageBoxIcon::Information);
}
Place the code to create the table and declare the appropriate variables in the program.
Select Image
The open file dialog is used to select the particular image. When the Select Image button is clicked, the open file dialog would be displayed to select the image (BMP/GIF/JPG).
openFileDialog1->InitialDirectory = S"C:\\";
openFileDialog1->Filter = S"All
Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg";
openFileDialog1->FilterIndex = 2;
if(openFileDialog1->ShowDialog()== DialogResult::OK)
{
pictureBox1->Image = Image::FromFile(openFileDialog1->FileName);
pictureBox1->SizeMode = PictureBoxSizeMode::CenterImage;
pictureBox1->BorderStyle = BorderStyle::Fixed3D;
lblImage_Path->Text = openFileDialog1->FileName;
}
Place the code in the Select Image button Click event.
Save to Database
When the Save to Database button is clicked, the selected image would be stored into the Picture table by means of converting it into a stream of bytes. After storing into database, refresh the DataGrid.
MemoryStream* mst = new MemoryStream();
pictureBox1->Image->Save(mst,pictureBox1->Image->RawFormat);
Byte BinaryImg[] = mst->GetBuffer();
mst->Close();
Then, insert the image name to the database.
conNwnd = new SqlConnection(strconString);
String* strSQL = S"INSERT INTO Picture"
" (Filename, Picture)VALUES (@Filename, @Picture)";
sqlcmd = new SqlCommand(strSQL,conNwnd);
sqlcmd->Parameters->Add(new SqlParameter(S"@Filename",
SqlDbType::NVarChar,50))->Value = strImgFile[0];
sqlcmd->Parameters->Add(new SqlParameter(S"@Picture",
SqlDbType::Image))->Value = BinaryImg;
conNwnd->Open();
sqlcmd->ExecuteNonQuery();
conNwnd->Close();
RefreshTable();
MessageBox::Show(String::Concat(strImgFile[0],S" saved to Database."),
S"Image Status",MessageBoxButtons::OK,MessageBoxIcon::Information);
Here, RefreshTable function is used to refresh the grid.
RefreshTable()
conNwnd = new SqlConnection(strconString);
SqlDataAdapter* sqlDA = new SqlDataAdapter(S"Select PictureID,Filename,"
"Picture FROM Picture",conNwnd);
TableData->Clear();
sqlDA->Fill(TableData,INPUT_TABLE);
dataGrid1->DataSource = TableData->Tables->Item[INPUT_TABLE];
On the form Load call, this RefreshTable function displays the values from the database.
Conclusion
Using this small application, a beginner can understand the DataGrid and about database connections, and how to store and retrieve values from the database using VC++ .NET. Your comments are always welcome which helps me to sharp my knowledge more.
| You must Sign In to use this message board. |
|
|
 |
|
 |
hi sir i am using vc++.net(mfc application)in visual studio 2005 but i am not getting datagrid control. but when i am adding a form then this control appear in contols but a message comes like "your project will be converted to CLR project" what is this plz help........
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hello, I am doing a similar application in Visual C++.Net 2003. I would like to know the following My UI has the following Name : abc Select image (button) Print (button)
When the user clicks Select image then the image should appear and it should be editable. Then the user can make changes to the image if needed then he should be able to print it or convert to pdf.
Before printing the name, here "abc" should also be included in the image file. Thanks a lot in advance for your help.
Minad
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hello there
Say I have a table (in an SQL database) with several INDEX tags, each with a name... Is there a way to find out which COLUMNS are taking part in each of these Indexes? using ADO of course...
I don't see a function like GetColumnsInIndex("IndexName") or something like that...
Cheers Alex
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Sir, can you tell me whether I can use ado.net in MFC? recently ,i will do it ,but know little about it ! thanks!
|
| Sign In·View Thread·PermaLink | 1.75/5 |
|
|
|
 |
|
 |
Hi, Im new to codeproject, I was hoping to get some help. Im tryin to convert the Image into Byte stream and send it to MySQL database. (Hence, so I can send image to database)
MemoryStream^ mst = gcnew MemoryStream(); viewPictureBox->Image->Save(mst,viewPictureBox->Image->RawFormat); Byte BinaryImg[] = mst->GetBuffer(); <- doesnt like this line
MySqlCommand^ cmd = gcnew MySqlCommand("SELECT * FROM Patient", Mycon); cmd = gcnew MySqlCommand("INSERT INTO patient(Leftimage) VALUES" + "('"+viewPictureBox+"')", Mycon); I get the following error: c:\documents and settings\hasan bajis\desktop\lei_patient_new\lei_patient\lei_patient\Form1.h(1230) : error C2440: 'initializing' : cannot convert from 'cli::array ^' to 'unsigned char []'
Can someone plz give me hand.
thanks alot in advance
hbjs
makaveli
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi... Could you please tell me how to format Datagrid in VC++.NET.. My requirement is, i have to use a datagrid with three columns. I am not going to connect with any database. just i will enter some data in grid at runtime and will save it in a text file... I am new to vc++.net. when i tried to use datagrid, i am not able to format it(creating required no. of columns and rows). Please help..
Salai
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
 | Code?  young1 | 8:32 20 Jan '05 |
|
 |
I download the code, however I did not find anything you described here. there are files like form1.cpp, assemblyinfor.cpp, are you sure these are the code? I am really eager to see your complete code. and hope from there to find out how you did you job?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
hai Young1,
Download the Demo version. there u can find the actual source code. Start the Sql server and run the applicaiton. it will be working fine..
Regards Arul.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi Fri
Iam looking for datagrid code that add,update or delete directly to database. Any changes made on datagrid cell and click add or update button... it should effect to database..
My mail id (pravinms07@yahoo.co.in)
.... Pravin
M.S.Praveen
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
A simplified coding work! I noticed that in DevStudio 6.0 MemoryStream is a class defined in SDK for Java.
Be smart!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
This was a good article to show how simple it is to create a database, look for files, convert images to byte streams and save to the database. It didn't say much at all about the datagrid. But in fact that is part of the beauty of the datagrid. Not much needs to be said to get it working. However, it is also a very powerful object that can be manipulated in many ways.
"It is a profitable thing, if one is wise, to seem foolish." (Aeschylus)
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
|
 |
|
 |
Respected Sir, Sir can u plz send me the important source code for implementing Network packet Analyzer.i saw ur profile and i know that u r a encyclopedia in VC++.Net Sir can u plz send me the source code for implementing this project using VC++.Net Is database needed to implement this project. if so then which all b the necessary tables. I hope u will surely help me out.Kindly do not ignore this mail and find sum time to reply my mail at rlekshmip@gmail.com
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
|