casit
asked on
Problems with Datagridview and datasets
I will try to not make this too confusing.
I have the following which works fine.
this.addressTableAdapter.F ill(this.d ataSet1.Ad dress); //loads all db info into dataGridView1
this.addressTableAdapter.U pdate.(thi s.dataSet1 .Address); //saves the changed info back to db
Below is some code I wrote to do a manual query. The data shows up in the same dataGridView1. and also using same dataSet1. I just can't get it to save.
I have the following which works fine.
this.addressTableAdapter.F
this.addressTableAdapter.U
Below is some code I wrote to do a manual query. The data shows up in the same dataGridView1. and also using same dataSet1. I just can't get it to save.
OdbcConnection con = new OdbcConnection("DSN=ShipData");
string sqlQuery = "SELECT * FROM Address where " + searchfield.Text.ToString() + " LIKE \'%" + searchvalue.Text.ToString() + "%\'";
OdbcCommand com = new OdbcCommand(sqlQuery, con);
OdbcDataAdapter da = new OdbcDataAdapter(sqlQuery, con);
DataSet1 ds = new DataSet1();
da.Fill(ds, "Address");
dataGridView1.DataMember = "Address";
dataGridView1.DataSource = ds;
ASKER
Sorry just really starting out at C#. I got the fill and update stuff from the wizard :) The other stuff is stuff I came up with myself. However my stuff doesn't update the db.
casit
See my post for how you need to Update from manually created objects.
Wayne
See my post for how you need to Update from manually created objects.
Wayne
ASKER
Alrighty I got some of it figured out now.
However now I'm getting the following error. I'm so close now :)
System.Data.Odbc.OdbcExcep tion was unhandled
Message="ERROR [07002] [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 22."
Source="odbcjt32.dll"
ErrorCode=-2146232009
StackTrace:
at System.Data.Common.DbDataA dapter.Upd atedRowSta tusErrors( RowUpdated EventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataA dapter.Upd atedRowSta tus(RowUpd atedEventA rgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataA dapter.Upd ate(DataRo w[] dataRows, DataTableMapping tableMapping)
at System.Data.Common.DbDataA dapter.Upd ateFromDat aTable(Dat aTable dataTable, DataTableMapping tableMapping)
at System.Data.Common.DbDataA dapter.Upd ate(DataTa ble dataTable)
at FedEx_Shared_Addy.frmShare dAddy.save DataToolSt ripMenuIte m_Click(Ob ject sender, EventArgs e) in C:\Users\chris\Documents\V isual Studio 2005\Projects\FedEx Shared Addy\Form1.cs:line 79
at FedEx_Shared_Addy.frmShare dAddy.save changes_Cl ick(Object sender, EventArgs e) in C:\Users\chris\Documents\V isual Studio 2005\Projects\FedEx Shared Addy\Form1.cs:line 89
at System.Windows.Forms.Contr ol.OnClick (EventArgs e)
at System.Windows.Forms.Butto n.OnClick( EventArgs e)
at System.Windows.Forms.Butto n.OnMouseU p(MouseEve ntArgs mevent)
at System.Windows.Forms.Contr ol.WmMouse Up(Message & m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Contr ol.WndProc (Message& m)
at System.Windows.Forms.Butto nBase.WndP roc(Messag e& m)
at System.Windows.Forms.Butto n.WndProc( Message& m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.OnMessa ge(Message & m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.WndProc (Message& m)
at System.Windows.Forms.Nativ eWindow.De buggableCa llback(Int Ptr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Unsaf eNativeMet hods.Dispa tchMessage W(MSG& msg)
at System.Windows.Forms.Appli cation.Com ponentMana ger.System .Windows.F orms.Unsaf eNativeMet hods.IMsoC omponentMa nager.FPus hMessageLo op(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Appli cation.Thr eadContext .RunMessag eLoopInner (Int32 reason, ApplicationContext context)
at System.Windows.Forms.Appli cation.Thr eadContext .RunMessag eLoop(Int3 2 reason, ApplicationContext context)
at System.Windows.Forms.Appli cation.Run (Form mainForm)
at FedEx_Shared_Addy.Program. Main() in C:\Users\chris\Documents\V isual Studio 2005\Projects\FedEx Shared Addy\Program.cs:line 17
at System.AppDomain._nExecute Assembly(A ssembly assembly, String[] args)
at System.AppDomain.ExecuteAs sembly(Str ing assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.Hos tingProces s.HostProc .RunUsersA ssembly()
at System.Threading.ThreadHel per.Thread Start_Cont ext(Object state)
at System.Threading.Execution Context.Ru n(Executio nContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHel per.Thread Start()
However now I'm getting the following error. I'm so close now :)
System.Data.Odbc.OdbcExcep
Message="ERROR [07002] [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 22."
Source="odbcjt32.dll"
ErrorCode=-2146232009
StackTrace:
at System.Data.Common.DbDataA
at System.Data.Common.DbDataA
at System.Data.Common.DbDataA
at System.Data.Common.DbDataA
at System.Data.Common.DbDataA
at FedEx_Shared_Addy.frmShare
at FedEx_Shared_Addy.frmShare
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Nativ
at System.Windows.Forms.Unsaf
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at FedEx_Shared_Addy.Program.
at System.AppDomain._nExecute
at System.AppDomain.ExecuteAs
at Microsoft.VisualStudio.Hos
at System.Threading.ThreadHel
at System.Threading.Execution
at System.Threading.ThreadHel
Please post the code segment which caused the error.
Wayne
Wayne
ASKER
Here is my whole form.cs file
That error gets thrown when it hits function
saveDataToolStripMenuItem_ Click -> da.Update(dt);
That error gets thrown when it hits function
saveDataToolStripMenuItem_
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
namespace FedEx_Shared_Addy
{
public partial class frmSharedAddy : Form
{
public frmSharedAddy()
{
InitializeComponent();
}
OdbcConnection con = new OdbcConnection("DSN=ShipData");
OdbcDataAdapter da = new OdbcDataAdapter();
DataTable dt = new DataTable();
private void frmSharedAddy_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'dataSet1.Address' table. You can move, or remove it, as needed.
//this.addressTableAdapter.Fill(this.dataSet1.Address);
// this gets all rows from db.
//this.addressTableAdapter.Fill(this.dataSet2.Address);
}
private void search_Click(object sender, EventArgs e)
{
string sqlQuery = "SELECT * FROM Address where " + searchfield.Text.ToString() + " LIKE \'%" + searchvalue.Text.ToString() + "%\'";
OdbcCommand com = new OdbcCommand(sqlQuery, con);
da.SelectCommand = com;
OdbcCommandBuilder cmbuilder = new OdbcCommandBuilder(da);
da.Fill(dt);
dataGridView1.DataSource = dt;
}
private void saveDataToolStripMenuItem_Click(object sender, EventArgs e)
{
dataGridView1.BindingContext[dt].EndCurrentEdit();
da.Update(dt);
}
private void delete_btn_Click(object sender, EventArgs e)
{
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you thank you. I would have never ever gotten that :) works beautifully.
How are you doing the update? With your code snippet, the DataAdapter is declared at procedure level only. You'll need that same DataAdapter ('da') to be able to perform the Update, so declare it at class level (ie, outside any procedure), as well as the DataSet. Then you can perform the update like this....
da.Update.(ds);
Regards,
Wayne