[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problems with Datagridview and datasets

Posted on 2008-11-09
8
Medium Priority
?
621 Views
Last Modified: 2012-05-05
I will try to not make this too confusing.
I have the following which works fine.
this.addressTableAdapter.Fill(this.dataSet1.Address); //loads all db info into dataGridView1
this.addressTableAdapter.Update.(this.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.
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;

Open in new window

0
Comment
Question by:casit
  • 4
  • 4
8 Comments
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 22919648
Hi casit,

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
0
 

Author Comment

by:casit
ID: 22919666
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.
0
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 22919704
casit

See my post for how you need to Update from manually created objects.

Wayne
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:casit
ID: 22919715
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.OdbcException 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.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
       at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
       at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
       at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
       at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
       at FedEx_Shared_Addy.frmSharedAddy.saveDataToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Users\chris\Documents\Visual Studio 2005\Projects\FedEx Shared Addy\Form1.cs:line 79
       at FedEx_Shared_Addy.frmSharedAddy.savechanges_Click(Object sender, EventArgs e) in C:\Users\chris\Documents\Visual Studio 2005\Projects\FedEx Shared Addy\Form1.cs:line 89
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at FedEx_Shared_Addy.Program.Main() in C:\Users\chris\Documents\Visual Studio 2005\Projects\FedEx Shared Addy\Program.cs:line 17
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
0
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 22919721
Please post the code segment which caused the error.

Wayne
0
 

Author Comment

by:casit
ID: 22919758
Here is my whole form.cs file
That error gets thrown when it hits function
saveDataToolStripMenuItem_Click -> da.Update(dt);
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);
        }
    }
}

Open in new window

0
 
LVL 48

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 2000 total points
ID: 22919828
Try specifying the QuotePrefix and QuoteSuffix properties of the CommandBuilder. There could be fields with spaces in them, which the Insert, Update and Delete commands won't like.

Wayne
OdbcCommandBuilder cmbuilder = new OdbcCommandBuilder(da);
cmbuilder.QuotePrefix = "[";
cmbuilder.QuoteSuffix = "]";

Open in new window

0
 

Author Comment

by:casit
ID: 22919876
Thank you thank you.  I would have never ever gotten that :)  works beautifully.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month19 days, 3 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question