DataTable rows not fetched after adding new row - c++

I added a new row and updated the datatable. The record was inserted into the table. But when I filled with the same datatable again, the result is an empty datatable. Am I missing something? Did I do something wrong? I just learned about using database in .net recently and I don't know what to do with this problem.
MySqlConnection ^connection;
DataTable ^table1;
MySqlDataAdapter ^adapter1;
MySqlCommandBuilder ^cmb;
private: System::Void opentable(String ^sql){
adapter1 = gcnew MySqlDataAdapter(sql,connection);
cmb = gcnew MySqlCommandBuilder(adapter1);
table1 = gcnew DataTable;
adapter1->Fill(table1);
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
connection = gcnew MySqlConnection("XXXXXXXXXX");
connection->Open();
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
opentable("SELECT * FROM ms_per WHERE PerCode='001'");
if(table1->Rows->Count==0){ //table1->Rows->Count is 0
DataRow ^newrow = table1->NewRow();
newrow["PerCode"] = "001";
table1->Rows->Add(newrow);
}
adapter1->Update(table1);
opentable("SELECT * FROM ms_perhist");
dataGridView1->DataSource=table1;
//ms_perhist is not an empty table, but after this the DataTable is empty.
I am seeing if I can wrap these lines into a single procedure so I can use it with other tables/queries:
adapter1 = gcnew MySqlDataAdapter(sql,connection);
cmb = gcnew MySqlCommandBuilder(adapter1);
table1 = gcnew DataTable;
adapter1->Fill(table1);
Note 1: I tried the following line. Data in ms_perhist shows up correctly, so the newrow-update lines may have something to do with the problem.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
opentable("SELECT * FROM ms_per WHERE PerCode='001'");
opentable("SELECT * FROM ms_perhist");
dataGridView1->DataSource=table1;
}

I have tested your code in c# and it's working fine. May be C++ creating new datable instance when you are executing the following line
table1 = gcnew DataTable;
So don't use that line twice in your code. But I am not sure about C++

It appears I was using an outdated MySQL.Data.dll. I checked the modified date and it was from 2008.
I changed the MySQL Connector version to the most recent version and the problem is fixed.

Related

Get Selected Item ID of ListBox C++

Im trying to make one simple program, that list few names in ListBox, and when selecting one of them, and clicking on one button should be load the data in few TextBoxes... All i need is the ID of the selected item of that ListBox because the data is in array and i can't get the info without that id.
So here is my code:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
this->listBox1->Items->Clear();
if (sCount != 0) {
for (int i = 1; i <= sCount; i++) {
String^ entry = gcnew System::String(s[i].Show().c_str());
this->listBox1->Items->Add(entry); //Listing the items from the array: s
}
}
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
s[++sCount].InsertStudent("Name",270,50); //This is how i'm adding items in the array
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
int i = this->listBox1->SelectedItem; //Trying to get the ListBox Item ID
String^ entry = gcnew System::String(s[i].Show().c_str()); //Getting the Item from the array
this->textBox1->Text = entry; //Placing the array item into the textBox1
}
P.S. I want to do it in that way, because the array have class and there i put more than one items in one ID, but in the ListBox is listed only one of them.
Can someone help? Thanks in advance :-)
I found the answer myself :)
Here is what i search, if someone come to this post:
int i = this->listBox1->SelectedIndex;

importing a picture from an openfiledialog into a picturebox, in visual c++

M stuck in importing a picture from an openfiledialog into a picturebox, in visual c++ windows form at Visual studio 2012... i searched it on different forums, and found one possible solution:-
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog op = new OpenFileDialog();
op.Title = "Select a picture";
op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
"JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
"Portable Network Graphic (*.png)|*.png";
if (op.ShowDialog() == true)
{
imgPhoto.Source = new BitmapImage(new Uri(op.FileName));
}
}
But in this solution or other solutions close to this one, does not allow me to create
" new OpenFileDialog(); "
another solution, offered by microsoft for cursor file was...
private:
System::Void button1_Click(System::Object * sender,
System::EventArgs * e)
{
// Displays an OpenFileDialog so the user can select a Cursor.
OpenFileDialog * openFileDialog1 = new OpenFileDialog();
openFileDialog1->Filter = "Cursor Files|*.cur";
openFileDialog1->Title = "Select a Cursor File";
// Show the Dialog.
// If the user clicked OK in the dialog and
// a .CUR file was selected, open it.
if (openFileDialog1->ShowDialog() == DialogResult::OK)
{
// Assign the cursor in the Stream to
// the Form's Cursor property.
this->Cursor = new
System::Windows::Forms::Cursor(
openFileDialog1->OpenFile());
}
}
same problem in this too....
can any one suggest the easiest approach to do the required task
In your first example, you are trying to use C# sintax. In C++/CLI you won't use . but the ->, neither new or * for handles. You actually need gcnew and ^.
Apart from this, look at this simple code:
Note: To be sure this example will compile, create a new project called TestImage, add a button called btnLoad and a picturebox called pbImage.
In c++ it is better to separate your header file (.h ) from your c++ file ( .cpp ). In your header file, declare just the prototype of the click event:
private: System::Void btnLoad_Click(System::Object^ sender, System::EventArgs^ e);
In your .cpp file, you should have something like this:
#include "stdafx.h"
#include "Form1.h"
using namespace TestImage;
System::Void Form1::btnLoad_Click(System::Object^ sender, System::EventArgs^ e) {
OpenFileDialog^ ofd = gcnew OpenFileDialog();
//insert here the filter if you want
//ofd->Filter..
if (ofd->ShowDialog() == System::Windows::Forms::DialogResult::OK) {
pbImage->Image = Image::FromFile(ofd->FileName);
}
}
Remember to resize accordingly to your image pbImage and you will be fine.
Hope this one helps.

Adding data into existing column SQL

Edit:
randomly searching through other languages i found the correct answer if anyone needs:
"update DataBase_Name set Column= '"+value+"' where ID='"+value+"';"
I need to insert data into a column and couldn't find much info about it on the web.
the code is
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
if (this->listBox1->SelectedIndex!=-1)
{
String^ constring = L"datasource=localhost;port=3306;username=root;password=root;";
MySqlConnection^ conDataBase = gcnew MySqlConnection(constring);
MySqlCommand^ cmdDataBase =
gcnew MySqlCommand(insert into products_shop.productname where idProductName='"+this-listBox1->SelectedItem->ToString()+"' ('ProductX','ProductY') values('"+this->textBox2-Text+","+this->textBox3->Text+"');",conDataBase);
MySqlDataReader^ MyReader;
try
{
conDataBase->Open();
MyReader = cmdDataBase->ExecuteReader();
}
catch(Exception^ex)
{
MessageBox::Show(ex->Message);
}
}
else
{
MessageBox::Show(L"Select an item please");
}
}
insert into products_shop.productname where idProductName='"+this->listBox1-SelectedItem-ToString()+"' ('ProductX','ProductY') values('"+this->textBox2-Text+","+this->textBox3-Text+"');
this line is just my randm guess, any help on how the syntax should actually be? or if it works somehow different..
thanks for help

Changing picture box on mouseover, and resetting on mouseleave

I am developing a windows form program and having a hard time finding how to do this in C++. The MSDN has this page, http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.image, but the C++ documentation is lacking compared to the VB.
This is what I have so far. This method is supposed to avoid the common flickering issues but I am not sure where to go from there since I need it to go back to the original image after the mouse leaves.
void InitializeComponent(void)
{
this->btnExit->BackColor = System::Drawing::Color::Transparent;
this->btnExit->BackgroundImageLayout = System::Windows::Forms::ImageLayout::None;
this->btnExit->Image = (cli::safe_cast<System::Drawing::Image^ >(resources->GetObject(L"btnExit.Image")));
this->btnExit->Location = System::Drawing::Point(764, 4);
this->btnExit->Name = L"btnExit";
this->btnExit->Size = System::Drawing::Size(30, 20);
this->btnExit->TabIndex = 3;
this->btnExit->TabStop = false;
this->btnExit->Click += gcnew System::EventHandler(this, &mainForm::btnExit_Click);
}
#pragma endregion
private: System::Void btnExit_OnMouseEnter(System::Object^ sender, System::EventArgs^ e) {
Image^ get ();
void set (Image^ value);
}
Thanks.
private: System::Void btnExit_MouseEnter(System::Object^ sender, System::EventArgs^ e) {
btnExit->Image = Image::FromFile("C:\\Users\\...\\image.png");
}
Works, not sure if it is the proper way to do it.

Referencing TabControls from another Form C++/CLI

I am trying to convert this tabbed browser into C++ from visual basic.
I am trying to reference the Tab Control from Form1.h.
Here is the code on Form1.h:
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
String^ title = String::Concat("TabPage ",(tabControl1->TabCount + 1).ToString());
tab^ newtab = gcnew tab;
newtab->Show();
newtab->TopLevel = false;
newtab->Dock = System::Windows::Forms::DockStyle::Fill;
TabPage^ myTabPage = gcnew TabPage(title);
myTabPage->Controls->Add(newtab);
tabControl1->TabPages->Add(myTabPage);
}
The code on the second form that is trying to create another tab is this:
private: System::Void newTabToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
tab^ newtab = gcnew tab;
newtab->Show();
newtab->TopLevel = false;
newtab->Dock = System::Windows::Forms::DockStyle::Fill;
TabPage^ myTabPage = gcnew TabPage();
myTabPage->Controls->Add(newtab);
tabControl1->TabPages->Add(myTabPage);
}
In visual basic all that is required is to add Form1. to the beginning like so...:
//Original
tabControl1.TabPages.Add(myTabPage);
//New
Form1.tabControl1.TabPages.Add(myTabPage);
How could I do this same thing in C++?
Visual Basic provides a default instance of each class in your project. When you say Form1.tabControl1, you're actually getting a particular global instance of Form1, and accessing the tabControl1 field on that.
Add a way to send the instance of Form1 to the second form, and use that instead of Form1. Something simple like passing the instance of Form1 to the second form in its constructor will probably do the trick.