Best approach... Reposting...

A

AMeador

I have a form that should come up blank (no data in the text boxes,
etc...). I want the user to be able to fill in the contols on the form
and then save the data to a SQL Server database. My plan up to this
point is to use a dataset and have it bound to the controls in the
form. I want to do this so I can take advantage of Undoing changes
easily. The problem is I don't know how to deal with the dataset when
an initial record hasn't been retrieved to populate the controls (to
allow the user to fill in the controls for a new record). Do I create a
dataset that witn no records in it when the form loads? If so, how
would you do this? Or, do nothing initially, let the user fill in stuff
and when they click on my Save button, this will call the add new
record method? I don't know - I'm confused. I hope I'm making sense
here. Please advise - Thanks!

(Sorry for the re-posts for anyone that has already seen this message,
but I have posted this before late at night, and it seems like by the
time everyone gets to looking at things the next day, it has been
pushed way down on the list and thus nobody has responded)
 
B

Bob Powell [MVP]

Use the data-adapter wizard to create an adapter with the correct statements
to select, delete, update and insert data to the table you're interested in.

Use the select command to get all or some subset of the data into a dataset
using the fill command. You don't even need to use a real select, just
something to populate the dataset with the columns required.

When you're ready to post the data, Add a new row to the dataset, populate
the columns and call Update on the dataset to insert the new row.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
A

AMeador

The form I'm working with will have a persons data (name, address,
etc...) with a datagrid that lists events for that person
(appointments, visits, referals, etc...). I want the form fields to be
blank when the form comes up. If the user clicks the Find button,
another form will come up that will let them pick a person from a list
(that will include the presons name and ID number). Once they pick the
person they want, the main form will be filled with the data for that
person (using a dataset). However, if the user is entering a new
person, they should just enter the data in the fields on the form. When
they are done, they will have to click Save to store the data.
I understand that the dataset will keep track of the original values
and the current values for data in a given row. I want to give the user
the ability to scrap their changes and re-populate the data fields with
the original values in the dataset. I was thinking of binding the
dataset columns to the approprite form fields. However this is
confusing me in a few ways. Can I even do this? Can I bind the
datarows, current value to the form fields? If I can, how would I push
the original values into the form fields if the user clicks on Reset to
Origianl button? Would I just have to manually do this, or change some
property, or can I just not do it like this? The other problem is that
when the form first comes up, there is no data in a dataset, as no
Person has been selected yet. This is what was confusing me about the
issue of binding on a blank form. Since there is no data being pulled
into the dataset, how can the values being entered be dealt with in
regards to the dataset? If I understand you right, are you saying just
don't try to do binding at all?
When I do have a dataset, it will be based off of a select that
should only return one row, the Person that they did a search for.
Would I just manually add the row's individual column values to each
form field manually? I guess I could watch the controls focus events
and see if the datarow has changed and if it has, re-enable the Reset
button. I don't know - I have read enough about the datasets and such
that I get basically what they do, I'm just having a hard time seeing
how to apply them to the form fields - especially if they start out
blank.
Again, I appreciate your response and time on this issue - I know
its not an easy topic for short newgroup postings.

--- Andrew
 
B

Bob Powell [MVP]

The blank dataset will have all the column mappings set up. If you don't do
a fill command then there will be no data in the dataset. The grid will show
an empty row and you can fill out the information and call the data
adapter's update command.

I did a quick test of this on one of my DB's and entered a couple of new
records just fine.

#1 drag a table onto your form. This will create the adapter and the
connection.
#2 create a dataset for this table
#3 bind it to the datagrid by setting the dataset as the datasource.
#4 add a button that calls the data adapter update command.

IF you are editing a row create a select statement to get the row otherwise
just don't call the dataadapter fill command at-all.

Given the second case, if you run the form the grid will be blank but
populated with the column headings for your fields, add some data and press
the button. You will have a new row in the database.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
A

AMeador

Thanks Bob, I appreciate your time and help. I will try doing this.
Again, thanks!

--- Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top