What are the possibilities of displaying items in a listbox? - c#

I've been trying to figure out for a long time now how to create an interface that can allows users to input several rows of data and pass those entries into an SQL server database all in one shot. I could not get any better ideas so I came up with this (see picture below).
What I envisioned is that the user enters values in the textboxes and hits the "add to list" button. The values are then populated in the list box below with the heading "exhibits lists" and when the add exhibit button is pressed, all values from the list box are passed into the database.
Well, I'm left wondering again if it would be possible to tie these values from the textboxes to the list box and whether I'd be able to pass them into the database.
If it were possible then I'd please love to know how to go about it otherwise I'd be glad if you could recommend a better way for me to handle the situation otherwise I'd have to resolve to data entry one at a time.
I believe there is some useful information from this website that can help solve my problem but I just can't make heads or tails of the article... it seems like I'm almost there and it skids off. Can everyone please read and help me adapt it to my situation? Post below:
http://www.codeproject.com/KB/aspnet/ExtendedGridView.aspx

Yes, it is possible. I have done this twice before.
Check out the "Data Access Tutorials" at http://www.asp.net/web-forms/data for ideas.
Overview
For the user-interface, you want a data-entry-form above a data-grid.
For the back-end, you want a data-adapter (or table-adapter) that loads and saves data to the database in one operation.
You would use a DataSet and DataAdapter to update the database, and manipulate a DataRow for the input and a DataTable for the list-box. Note the list-box is actually a data-grid, repeater, or other control that accepts a DataTable as a DataSource.
The DataAdapter fills the DataSet which contains the DataTable.
The input controls bind to a DataRow created from the DataTable. When the user clicks add-to-list, you add the data-row to the data-table, then create a new data-row for the next item.
When the user clicks add-exhibit, it is a simple matter of using the DataAdapter to update the database. Any changed, deleted, and added data is handled for you.

You could use a transaction to encapsulate all the sql queries you are trying to run, so you can then just 'commit' the transaction in one to the database? Are you still having difficulty with this issue?

Related

Multiple row input in asp.net for editing database table

From time to time, I need to create an input control which allows multiple rows of input, the type of input and number of columns could be different in each case but typically it would be represent a database table being edited, so it should also be possible to store an id value within each row. It should also be possible for the user to add new rows to the input if they have more data to input.
I have tried a couple of approaches to this but they are all very long winded for what seems like such an obvious and common scenario. I'm thinking that there must be a simple way to do this that I have missed.
The way that I usually solve this is by using a list view, enter all of the input controls required within the item template and use a html table for formatting, with each item being a row of the table. If there is existing data to be edited, I would load the data from the database, add a blank object to the results and bind it to the list view. If there is no existing data, I would create a collection with a blank record in it and bind it to the list view. I add a button for adding a new row. When clicked, this button retrieves all of the existing data from the list view by iterating all of the listview items and populating the data into a collection, which I would then add a blank object to and rebind the listview with the results. When saving, I would retrieve the results by iterating the listview items again and using a hidden field within each row to store the record id. This would determine whether a new record was added or an existing record was edited.
This approach works for me but I'm sure there must be simpler ways to achieve this. Looking forward to seeing how other people solve this.
For web forms, use a GridView. You can set its properties to allow editing, deleting, new rows, sorting, and paging.
Check out the MSDN example here that shows how to use it. If you bind a datasource to it, it will figure out the columns and adjust dynamically then, or you can predefine the columns you want for more customability.
You are talking about bulk insert/update. You could use XML insertion/updation for this purpose. Get all the data to a DataSet ds variable. Then use ds.GetXml() method to convert the dataset to XML string. Pass this to an XML parameter into SQL server which has the datatype 'XML'
INSERT INTO YOURTABLE
SELECT
V.VOI.value('(.)[1]', 'int')
FROM
#Input.nodes('/DATASETNAME/DATATABLENAME/') V(VOI)
Use this link to learn more
You need dynamic table with Add, View,Edit and delete operations on each data.
I would suggest using DataTable Jquery component. there are tons of examples on Data operations, and you can plug this with any Server technology including ASP.net
http://editor.datatables.net/

Can i add rows and cells to a table dynamically?

I want the user to submit a message, and that message will be automatically added as a table row. Is it possible?
I want to do this:
Table table=new Table. table.Rows.Add(row)
Then I want to be able to insert a label into that row. For example:
row.Add(table)
But I can't find a way to do that.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.table.aspx
I want the table to grow one row bigger than it used to be with a label of the users newly added message.
I know that forums use tables, but their table seems to be growing with every message submitted.
How can I achieve the same effect with C#?
You did not provide enough details to say for sure, but almost absolutely yes.
If the HTML is dynamically and explicitly generated by you, then yes. Insert it in there.
If you are using a non-constant DataSource, then yes. Insert it in there.
The only reason it would not be possible is if the page is static, or if the data source is constant. And both of those are changeable.

Adding rows programatically to a DataGridView that is data bound?

I have a problem and I can't seem to be able to solve it. I thought someone here will be able to help out.
I have a form that has a DataGridView of customers. Now, I want to add several customers to this DataGridView without actually adding them to the database. This is because the client must be able to create a list of clients and when thats done add them all at once.
I have tried this:
string[] array = {"Microsoft", "Redmond", "Something"}
dataGridView.Rows.Add (array);
Now this can't be done because I get an exception saying something in the lines of you cant add rows programatically to a DataGridView that is data bound.
Now I have also read that this can be solved by using a table adapter to insert the rows instead of directly adding them via. a DGV. But this is also not possible because I use custom headers in the DGV because existing data is fetched via JOIN's so if I add them via a TableAdapter I get an exception that the it doesn't match the databases table schema.
Now I am really lost... Anyone know of a (halfway) elegant solution to this problem?
Thank you
Bind your DGV to a BindingList<YourObject>, where YourObject can be a simple class with properties reflecting your database schema. Initially populate the BindingList from the database, and the DGV will automatically add YourObject instances to the list. When you're ready to commit the changes, do so manually from the BindingList.
In order to show those rows, just do a UNION (-like) action before the databinding. Ie get your list of records, add the special ones and then databind.
Updating will require some special action(s) too. But with a total lack of details about your datastructures it's impossible to say what.

Databinding, dataset with datarelations, and refreshing single DataTable from DB

In a nutshell: I have a DataSet with multiple DataTables inside, which are connected via multiple DataRelations. I've created a form using databinding on this DataSet, and you could say it is pretty complex.
The scenarios is next: the main purpose of the form is the insertion of a new record. For it to be inserted, various parent-child relations must be set. It all works fine with the databinding, but the problem is next: when user wants to enter a new "child" for one-of-those parent-child relations, new form opens, he enters it, yada yada yada, - and it works. Trouble is - how should I get that new data back to the original form, to appear in parent-child combobox?
One way I know of, is to refresh the entire form; but I am looking for a solution to refresh just the DataTable containing that data. I've tried getting a new DataTable with fresh data from DB, and merging it with DataTable in DataSet, but it doesn't work: "A child row has multiple parents" exception is thrown:
// basically just a Select * using dataadapter,
// nothing programmatically added
DataTable table = tableModule.GetPlainTable();
existingDataSet.Tables["tableName"].Merge(table);
The equivalent example of what I'm trying to do is to have a Order form, with Customer combobox on it. On the right of the combobox, there is a "add" button that opens a new "Add customer" form. Just to mention (maybe it is relevant), inside the DB, "Customers" table is in relation with the "Cities" table, and that data is linked in databindning on the Orders form as well...
EDIT: one solution I can think of is to "manually" loop throughout the new DataTable, and find all rows that are not in the original DataTable inside DataSet, but I'm not sure this would work either ...
Any help / suggestion / explanation / push in the right direction would be greatly appreciated :)
Well, i kind of solved the problem with manual row copy upon insertion in second form... I have created an event with object collection (row item array), and in first form registered a handler on that event, in which I manually add the new row via:
data.Tables["tableName"].Rows.Add(e.RowItems);
after which I call AcceptChanges() on that row to make its state Unchanged, rather than Added...
This solution, although quick-and-dirty, works. The problem is, I think there is a better, more "databinding" philosophy oriented one... :=)

Cleanest way to implement collapsable entries in a table generated via asp:Repeater?

Before anyone suggests scrapping the table tags altogether, I'm just modifying this part of a very large system, so it really wouldn't be wise for me to revise the table structure (the app is filled with similar tables).
This is a webapp in C# .NET - data comes in from a webservice and is displayed onscreen in a table. The table's rows are generated with asp:Repeaters, so that the rows alternate colers nicely. The table previously held one item of data per row. Now, essentially, the table has sub-headers... The first row is the date, the second row shows a line of data, and all the next rows are data rows until data of a new date comes in, in which case there will be another sub-header row.
At first I thought I could cheat a little and do this pretty easily to keep the current repeater structure- I just need to feed some cells the empty string so that no data appears in them. Now, however, we're considering one of those +/- collapsers next to each date, so that they can collapse all the data. My mind immediately went to hiding rows when a button is pressed... but I don't know how to hide rows from the code behind unless the row has a unique id, and I'm not sure if you can do that with repeaters.
I hope I've expressed the problem well. I'm sure I'll find a way TBH but I just saw this site on slashdot and thought I'd give it a whirl :)
When you build the row in the databinding event, you can add in a unique identifier using say the id of the data field or something else that you use to make it unique.
Then you could use a client side method to expand collapse if you want to fill it with data in the beginning, toggling the style.display setting in Javascript for the table row element.
just wrap the contents of the item template in an asp:Panel, then you have you have a unique id. Then throw in some jquery for some spice ;)
edit: just noticed that you are using a table. put the id on the row. then toggle it.

Categories

Resources