0

I've done a few hours of research but nothing seems to apply to my current situation.

Using: Visual Studio 2010 .net 4.0, Language: C#

Issue:

I created a form and then drag and dropped the Specific Table from my Datasources onto the form to have VS2010 create the data table for me.

When I attempted to change or add values to the database using the data table, I get the error listed above

"ExecuteReader: CommandText property has not been initialized".

As from my previous research, NO, I have not defined a CommandText nor do I know where to create one considering VS2010 created all the datatable code and does not list it in the .cs file itself.

Code obtained from form by hitting f7

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace mineral_monitor.Manual_edits
{
    public partial class mineral_stock : Form
    {
    public mineral_stock()
    {
        InitializeComponent();
    }

    private void mineralsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
    {
        this.Validate();
        this.mineralsBindingSource.EndEdit();
        this.tableAdapterManager.UpdateAll(this.ore_stockDataSet1);
    }

    private void mineral_stock_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'ore_stockDataSet1.minerals' table. 
        // You can move, or remove it, as needed.
        this.mineralsTableAdapter.Fill(this.ore_stockDataSet1.minerals);
    }
   }
}

This was resolved by creating an update string manually in the data designer.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Sirusx69
  • 21
  • 1
  • 4
  • CommandText = ???? please post your code.. CommandType = ??? – MethodMan Feb 14 '12 at 15:09
  • It is the adapter's responsibility to pass command text to the command object. Thus, you lack the `SelectCommand` in your adapter probably. – Wiktor Zychla Feb 14 '12 at 15:49
  • @WiktorZychla - When I view the TableAdapter properties in the Data Designer it does state that it has a Select, Insert and Update Command. – Sirusx69 Feb 14 '12 at 15:52

1 Answers1

0

When you create a data source, select command is automatically created for you by VisualStudio, but update, insert and delete commands are not. You will have to specify them manually or using SqlCommandBuilder. Check out this link.

Aleksandar Vucetic
  • 14,715
  • 9
  • 53
  • 56
  • I took your advice and looked into that MSDN site. I did some messing around and now I'm getting "Update requires a valid UpdateCommand" which is a pretty straight forward error. After my research though everyone says that the CommandString builer cannot build something to update ALL to the table. How would one make the save button update all changes made via the DataGridViewer? – Sirusx69 Feb 14 '12 at 16:15
  • If you don't have a single table, but some Join or something, you will have to specify your DataAdapter's UpdateCommand manually. In other cases, SqlCommandBuilder should auto generate update command for you. Check out this link: http://stackoverflow.com/questions/588361/update-requires-a-valid-updatecommand-when-passed-datarow-collection-with-modifi – Aleksandar Vucetic Feb 14 '12 at 16:20
  • It is a single table. As i've said all I did was drag/drop from the Data Sources window onto the Windows Form and it auto-designed the datagridview and modification buttons (open/save etc); I also don't quite understand the page you linked me too. I already have a Data Adapter as mineralsTableAdapter but when running the "mineralsTableAdapter.Fill(this.ore_stockDataSet1.minerals)" it still returns that I do not have an "update command" set. – Sirusx69 Feb 14 '12 at 18:15
  • Try deleting everything and start all over again...new winforms project, add datasource, select your table (or try with some other table), drag and drop your dataset. Does it work then? – Aleksandar Vucetic Feb 14 '12 at 18:58
  • I created a new Windows Form project and setup the data connection. When I drag/drop the table onto the form it produces the following code: http://pastebin.com/A8uguLqW The program seems to break at "this.tableAdapterManager.UpdateAll(this.ore_stockDataSet);" giving the "update command not set" error. – Sirusx69 Feb 14 '12 at 22:22
  • So I did some more digging around and I created an "Update CommandText": http://pastebin.com/B8F3ULkN Problem with this is it now updates ALL fields in the database. – Sirusx69 Feb 14 '12 at 22:46
  • you are missing WHERE part in your updatecommand. That where part should contain fields that are part of your primary key. – Aleksandar Vucetic Feb 14 '12 at 23:44