7 Replies - 683 Views - Last Post: 12 September 2012 - 08:39 AM Rate Topic: -----

#1 sundog1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 11-September 12

Issue adding row to DataSet

Posted 12 September 2012 - 07:46 AM

Hi Guys, *NEWBIE ALERT*

Even though I have now re-written this thing from the ground up using Gridview instead of list view and made some great advances I'm still having the same problem with the bloody Save button and that infernal Dataset line!

Below is the new code: (I even added the connection the database AGAIN to try and make sure it got filled!)

 private void btnSaveAcc_Click(object sender, EventArgs e)
        {
            //System.Data.OleDb.OleDbCommandBuilder cbp;
            //cbp = new System.Data.OleDb.OleDbCommandBuilder(cashDA);

            //String cashCustomerSQL = "SELECT * FROM cashCustomers";
            //cashDA = new System.Data.OleDb.OleDbDataAdapter(cashCustomerSQL, cashCustom);
            //cashDA.Fill(cashCustomersTable);

            cashCustomersDS.Tables[0].Rows.Add();
            dataGridView1.DataSource = cashCustomersDS;
            dRow = cashCustomersDS.Tables["cashCustomers"].NewRow();
            dRow[0] = txtBoxAccRef.Text;
            dRow[1] = txtBoxName.Text;
            dRow[2] = txtBoxAddr1.Text;
            dRow[3] = txtBoxAddr2.Text;
            dRow[4] = txtBoxTown.Text;
            dRow[5] = txtBoxPostCode.Text;

            cashCustomersDS.Tables["cashCustomers"].Rows.Add(dRow);



Screenshot of Error is included. I think I've used the watch window correctly and its showing nowt. Diddly Squat even when I make it watch something.


Thanks for any help available.

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Issue adding row to DataSet

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9573
  • View blog
  • Posts: 36,253
  • Joined: 12-June 08

Re: Issue adding row to DataSet

Posted 12 September 2012 - 07:51 AM

What are you expecting this to do?

When you are adding a row to the datagridview you need to provide *SOME* sort of input. Check out the method call in either intellisense or MSDN.
http://msdn.microsof...y/75kfezas.aspx

What's is it expecting? A row object. That means create a datagridviewrow object from your table, fill it, and then use it as a parameter in your .add( /*here*/) bit.
Was This Post Helpful? 0
  • +
  • -

#3 sundog1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 11-September 12

Re: Issue adding row to DataSet

Posted 12 September 2012 - 07:58 AM

HI,

I'm expecting it to take the information from the text boxes and added it to the dataset and then add it to the underlying database.

I followed an example tutorial on how to do this and it worked but when trying to do it in my own code it does not seem to work???
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9573
  • View blog
  • Posts: 36,253
  • Joined: 12-June 08

Re: Issue adding row to DataSet

Posted 12 September 2012 - 08:19 AM

Then clearly your wheels fell off somewhere, but your explanation isn't helping find out where.

The gist is you have you grid, right? The user interacts with it.. and the grid displays data. Assuming you didn't lock out "new rows" anything the user adds is flagged as a 'row state = added'. This means you can tell the existing data beforehand from anything new (pretty handy when you want to insert only the new data, amirite?).

Hopefully your grid is tied to a dataset... with a datatable. That makes things simpler (and provides that whole rowstate=added bit).

From here you can follow any numerous tutorials here, on MSDN, or elsewhere but getting the data into the database is about the same.

1. find your rows you want to insert.
2. create your sql INSERT statement in a string variable.
3. write out your connection string.
4. create a sqlconnection object (using the string) and open it.
5. create a sqlcommand object using the sql string and the open sqlconnection object.
6. Add parameters (you *ARE* using a parametrized SQL query, right?) to the sqlcommand object.
7. cycle through your rows you want to insert filing each parameter with the right data
8. use the 'execute non query' function on the sqlcommand object.
9. repeat 7 & 8 until all your data is in
10. close the sqlconnection object
11. dispose of all the important objects.


Though it's best to have 4->9 in a try catch and 10 & 11 in the finally of the try catch.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: Issue adding row to DataSet

Posted 12 September 2012 - 08:21 AM

The line it stopped on:

10 cashCustomersDS.Tables[0].Rows.Add();


You have to tell it WHAT to add. It is expecting parameters in the parenthesis.
I'm sure as soon as you typed the ( - Intellisense popped up help telling you what the method expected. Accept the help your IDE gives you.


10 cashCustomersDS.Tables[0].Rows.Add(theObjectToBeAdded);

View Postmodi123_1, on 12 September 2012 - 08:51 AM, said:

What's is it expecting? A row object. That means create a datagridviewrow object from your table, fill it, and then use it as a parameter in your .add( /*here*/) bit.

Was This Post Helpful? 0
  • +
  • -

#6 sundog1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 11-September 12

Re: Issue adding row to DataSet

Posted 12 September 2012 - 08:32 AM

Ok, let me try and explain what I'm attempting to do here.

I have 7 Text Boxes all assigned to different things:

Acc Ref:
Address 1:
Address 2:
Town:
County:
Post Code:
Name:

Using the code I had in the previous post I was hoping that the Value's within the Text Boxes would be added to the dRow, Added to the data set called 'cashCustomerDS' and then added into the underlying database.

Obviously by the style of your typing and your attitude I've got it a little bit wrong for a Newbie which I am and I don't try to pretend I'm not!

been doing this most of the day and my brains a little fried.

tlhIn`toq -

That line was a mistake and a typo when entering the code on here so you an ignore that. I still get the same null error.
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9573
  • View blog
  • Posts: 36,253
  • Joined: 12-June 08

Re: Issue adding row to DataSet

Posted 12 September 2012 - 08:33 AM

Quote

Using the code I had in the previous post I was hoping that the Value's within the Text Boxes would be added to the dRow, Added to the data set called 'cashCustomerDS' and then added into the underlying database.



Fair enough.. why add it to the dataset when you can just use those values in the parameters (about #7 on my list)... insert the data.. and then just do a refresh of the dataset to your grid!

Tutorials
http://www.dreaminco...ery-a-database/

http://www.dreaminco...iew-using-c%23/

http://www.dreaminco...basics-in-c%23/
Was This Post Helpful? 1
  • +
  • -

#8 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: Issue adding row to DataSet

Posted 12 September 2012 - 08:39 AM

View Postsundog1, on 12 September 2012 - 09:32 AM, said:

tlhIn`toq -

That line was a mistake and a typo when entering the code on here so you an ignore that. I still get the same null error.


Over and over and over we say to copy/paste the code directly from Visual Studio - to avoid these kinds of things.

We don't like wasting our time being sent on wild goose chases due to transcription errors.

Let me just say - If you have these kinds of errors transcribing your own code and don't see the parts that are missing, then you probably are experiencing the same sort of thing when going from your book tutorial and your VS solution. It just stands to reason. I would suggest you slow down, confirm everything, be sure you comprehend everything and aren't just transcribing gibberish.

Best of luck with this. - Bye -
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1