Hi, First off, what I'm using Visual Studio 2005 (SP1) C++.Net Windows Application (CLR? not MFC) Also tried in C# I'm creating a project where a user can edit a form and the data is saved in a MS Access db. The records already exist and the user will only need to edit them. I have databound text boxes and check boxes to the database through design mode (i.e. Visual Studio created the dataSet, tableAdapter and such) I had no troubles reading from the database and when I changed position it would read the right record. However, I'm having trouble updating the database, but I don't really know where the problem is. Below is the C# version of a little demo I built trying to isolate the problem.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'tBCalDataSet.stations' table. You can move, or remove it, as needed.
this.stationsTableAdapter.Fill(this.tBCalDataSet.stations);
}
private void button1_Click(object sender, EventArgs e)
{
this.stationsTableAdapter.Update(this.tBCalDataSet.stations);
}
}
}
The text box is bound to a column in stations and for now, the default value when the program starts (the first record in that column) is all I want to edit. The value displays, and I can select and edit the value in the text box. When I push the button (button1), the text box loses focus (me thinking that it has actually updated). When I close the program and re-run it, the value in the text box has reverted to the old value (meaning the dB was never updated). Out of curiosity, I tried it with a datagrid and it worked fine but the data grid display is too complicated for my target users. Having both on the form I started playing around.
- When I made a change in the datagrid and lost its focus, the text box would update. Pressing the button would update the dB
- When I made a change in the textbox and hit the button, nothing would happen
- When I made a change in the textbox, then just selected the same field in the data grid, the value would update in the datagrid, but pressing the button would do nothing
From these results, I'm not sure if the dataset is not updating or if the tableAdapter is doing its job. According to Visual Studio, the connection test succeeds. I have found plenty of material on simple databinding. But they only state how to make the text box display the value. I have found basically no information on implementing an update from a text box. I have no clue what is going wrong and I have spent weeks trying to get this to work to no avail. I may be missing some chunk of code but my experience is in programming microcontrollers in C so I don't often encounter object related problem in C++. PLEASE HELP. | | ScubaS2 Thursday, April 30, 2009 12:05 AM | Hi Kira, You may have marked Nitin's response as an answer, but the problem is that it didn't answer my question. After messing around I discovered a work around. This is to merely change the position of the binding source. As far as I know, it causes the dataset to validate the information. If anyone can explain why this is the case and if there is a more appropriate method to achieve this, it would be helpful. Thanks for all your suggestions. ScubaS2 - Marked As Answer byScubaS2 Wednesday, May 20, 2009 1:54 AM
-
| | ScubaS2 Wednesday, May 20, 2009 1:54 AM | Finally an answer that works...
//Updates the database with error detection
try{
this->Validate();
this->stationsBindingSource->EndEdit();
this->stationsTableAdapter->Update(this->TBCalDataSet);
//MessageBox::Show("Update Successful");
}
catch(...){
//MessageBox::Show("Update Failed");
}- Marked As Answer byScubaS2 Friday, July 31, 2009 1:40 AM
-
| | ScubaS2 Friday, July 31, 2009 1:40 AM | | | meghanalohit Thursday, April 30, 2009 6:09 AM | Hi Meghana, I tried both accepting changes on the dataset as well as the stuff on the link and still nothing was updated. They don't throw any errors. See code below from my C++ version (the language I prefer). Mind you, I get a warning at debug saying C4101: 'e': Unreferenced local variable... But that's how it's written in the example.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
this->TBCalDataSet->AcceptChanges();
try
{
this->Validate();
this->stationsBindingSource->EndEdit();
this->stationsTableAdapter->Update(this->TBCalDataSet->tablestations);
MessageBox::Show("Update Successful");
}
catch(System::Exception ^e)
{
MessageBox::Show("Update Sucks");
}
}
Still not working... Is it worth upgrading to VS2008? Will this solve the problem? | | ScubaS2 Friday, May 01, 2009 3:38 AM |
//try this
try
{
stationsTableAdapter.Update(tBCalDataSet);
MessageBox.show("Updation Successful");
}
//If your code comesunder catch block, check again your connection status, and validate the data which you are passing via controls to your database.
catch (SqlException ex)
{
MessageBox.Show("Updation failed", ex.Message);
}
Please mark the replies as answers if they help and unmark if they don't.
//this is an example, try to do like this.
cn.Open();
SqlDataAdapter da;
SqlCommandBuilder cmdbuilder;
SqlCommand command = new SqlCommand("select * from icup where ID=@id", cn);
command.Parameters.Add("@id", SqlDbType.Int);
command.Parameters["@id"].Value = textBox1.Text;
da = new SqlDataAdapter(command);
cmdbuilder= new SqlCommandBuilder(da);
da.Fill(n99PROJECTSDataSet, "N99");
n99PROJECTSDataSet.Tables["N99"].Rows[0]["ID"] = textBox1.Text;
n99PROJECTSDataSet.Tables["N99"].Rows[0]["Name"] = textBox2.Text;
n99PROJECTSDataSet.Tables["N99"].Rows[0]["Age"] = numericUpDown1.Value;
n99PROJECTSDataSet.Tables["N99"].Rows[0]["Gender"] = comboBox1.SelectedItem;
n99PROJECTSDataSet.Tables["N99"].Rows[0]["Address"] = textBox3.Text;
n99PROJECTSDataSet.Tables["N99"].Rows[0]["Country"] = textBox5.Text;
n99PROJECTSDataSet.Tables["N99"].Rows[0]["EmailID"] = textBox4.Text;
try
{
icupTableAdapter.Update(n99PROJECTSDataSet);
}
catch (SqlException pnn)
{
MessageBox.Show("Could not open connection, Please try Again or Check Database connectivity", pnn.Message);
}
// textBox1.Text = System.Convert.ToString(dr[0]);
da.Update(n99PROJECTSDataSet,"N99");
MessageBox.Show("Updation complete");
}
- Proposed As Answer byNitin Chaudhary Friday, May 01, 2009 4:33 AM
- Unproposed As Answer byScubaS2 Friday, May 01, 2009 8:22 AM
- Unmarked As Answer byScubaS2 Friday, July 31, 2009 1:41 AM
- Marked As Answer byKira QianMSFT, ModeratorWednesday, May 06, 2009 4:56 AM
-
| | Nitin Chaudhary Friday, May 01, 2009 4:28 AM | Hi ScubaS2,
Base on my understanding, you want to call Update method to submit your changed data into MS Access database. The update sql should be generated inside the TableAdapter. So please do not call AcceptChanges. Otherwise the "RowState" will be changed back to "Unchanged" and the data won't be updated.
If you have anything unclear, please feel free to tell me.
Sincerely, Kira Qian
Please mark the replies as answers if they help and unmark if they don't. | | Kira Qian Monday, May 04, 2009 3:16 AM | Hi Kira, You may have marked Nitin's response as an answer, but the problem is that it didn't answer my question. After messing around I discovered a work around. This is to merely change the position of the binding source. As far as I know, it causes the dataset to validate the information. If anyone can explain why this is the case and if there is a more appropriate method to achieve this, it would be helpful. Thanks for all your suggestions. ScubaS2 - Marked As Answer byScubaS2 Wednesday, May 20, 2009 1:54 AM
-
| | ScubaS2 Wednesday, May 20, 2009 1:54 AM | Finally an answer that works...
//Updates the database with error detection
try{
this->Validate();
this->stationsBindingSource->EndEdit();
this->stationsTableAdapter->Update(this->TBCalDataSet);
//MessageBox::Show("Update Successful");
}
catch(...){
//MessageBox::Show("Update Failed");
}- Marked As Answer byScubaS2 Friday, July 31, 2009 1:40 AM
-
| | ScubaS2 Friday, July 31, 2009 1:40 AM |
|