Adding data into existing column SQL - c++

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

Related

DataTable rows not fetched after adding new row

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.

C++ UI Invoke not Working

When debugging,I found my program have stopped after
myServer->Invoke(myServer->myShowMessage);
I tried to find where it goes,and I set several breaks but I didn't find where it was going.However,I also set a break on ShowMessageMethod where it should go but I am sure it wasn't going to there.It was just look like dealing with other things and trapping in it.I suspect there was something wrong with the UI thread,but I have little knowledge about the underlying in Windows Message. I have create some other threads in main thread, is that makes things all different? I just followed the Invoke method code on msdn. And Here are my codes:
Server::Server(){
InitializeComponent();
myShowMessage = gcnew ShowMessageDelegate(this,&Server::ShowMessageMethod);
}
System::Void Server::start_Click(System::Object^ sender, System::EventArgs^ e){
Thread^ myThread = gcnew Thread(gcnew ThreadStart(this,&Server::ListenThreadFunc));
myThread->Start();
}
void Server::ShowMessageMethod(){
String^ message = "Get Successful";
this->APTBX->AppendText(message);
}
void Server::ListenThreadFunc(){
ServerListen^ mySL = gcnew ServerListen(this);
mySL->ListenThread();
}
ServerListen::ServerListen(Server^ _s){
myServer = _s;
}
void ServerListen::ListenThread(){
array<Object^>^myStringArray = { convert.toStringDelegate(information) };
Object^ re = myServer->Invoke(myServer->myShowMessage);
}
thanks in advance.

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;

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.