thukralz

Hello Everyone,

I have a form with 3 tabs and lot of textboxes, dropdowns. In all 50 controls. Now the question is when user clicks save what is the best way to get this data

One Solution : string Name = nameTextBox.text;

and do it for all the fields...

Or Please someone can suggest some different more efficient way to do it.

Thanks,

Harsimrat



Re: Visual C# General Best Practice to get User Entered Data ?

Wiggly

I maybe wrong here or there maybe cooler more eligent ways of doing this. But it attach each field to the right place and ensure it is validated you would need to do each individually. The key part here is validating the users input before it gets put into a database or something similar.

Another possible solution (just thinking out loud as I type this)... could be to get a list of all the controls / objects on the page. Cycle through them getting the name of each object and use reflection to call the correct validator method which would get the text in the correct way and validate it then save it where ever needed. This route means you can have as many controls on your form as you wish.

I presume this is a windows app rather than an aspx web page !

Hope that helps a little






Re: Visual C# General Best Practice to get User Entered Data ?

boban.s

Another way is to use DataBinding. Here is a cool presentation about DataBinding in .NET 2.0:
https://msevents.microsoft.com/CUI/EventDetail.aspx EventID=1032292925&culture=en-US&USBCCAnon=true




Re: Visual C# General Best Practice to get User Entered Data ?

Harsimrat

Yes, this is a Windows Application and ErrorProvider takes care of any validation error. I'm interested in now is getting all the user entered information in a good fashion...



Re: Visual C# General Best Practice to get User Entered Data ?

Zamial

I have no true idea if i'm about to say a load of rubbish so my apologies.

You could create a DataSet programatically or perhaps one big DataTable and insert the values (1 row or a dataset with lots of tables, 1 row each table)

Then bind your controls to the dataset/table columns, then when the user updates the values the underlying dataset/table is updated. You can then pass this back to your method when saving. Essentially doing away with reading each controls text property etc.

To me it sounds quite good off the top of my head i'm not sure how hard it would be in practice. I presume the original data on screen also comes from a database so your queries already return one row results which can go straight into a dataset named appropriately. go aahead and bind it etc as above.

Maybe it's also possible to use some sort of adapter so that the dataset updates are automatcially written back to the DB dynamically saving you lots of work although I doubt it.

In the past I have opted for reading each control individually. Usually no more than 15 though.





Re: Visual C# General Best Practice to get User Entered Data ?

Harsimrat

That presentation deals with the databinding using database, when you are directly connecting it. I have a middle layer or say I want to save everything what user enetered to an Xml file. At that point I need an efficient way to read what user enetered somehow programatically rathen than looking at forexample textbox control and see what user enetred. That will work if you have only 5-6 textboxes, but when you go in 100's there should be some better way to do it



Re: Visual C# General Best Practice to get User Entered Data ?

Harsimrat

Anyone with a better way to do it....



Re: Visual C# General Best Practice to get User Entered Data ?

boban.s

There is no simple solution, where you will do nothing and everything will work. DataBinding for example is simple for developer because all he needs to do is create dataset, do some drag drop operations, eventualy set some properties, and everything is working. But there is big amount of code lines writen, by those actions, to enable that to work like that. So if you don't like databinding, then you can create simular thing by your self. Basicaly it will not require less code to write, maybe just it will be on your way.

Create a method that will loop all form controls, it will depending on their type, textBox, comboBox, dateTimePicker, numericUpDown, or some other, execute coresponding code, and set a property in some class that will be the same as for example Name of control. To be able to do that, first you need to create string indexer of all classes, and by that i mean this:

Customer c = new Customer();
c["Name"] = "Some name"; //property of a class to be accessible by providing property name as a string

When you loop trough form controls, you will discover the name of the property for that control (either using control name, or Tag property which need to be filled), and with that name update some class property with coresponding property of that control, Text, SelectedValue, Date, Value, etc.

If you do this, then the code in the form, for capturing all values from controls will be like one method of 30 lines. But the big code will be in Classes string indexers which need to be developed.

My proposition is not something that i would do, but i mention about as an idea just to be aware that is posible, but in most scenarios is not simpler. Working with one class, should be done only trough one Application form, so it's better to have big code in that single form, then to create indexer in the class.