I am new to asp.net so I dont know the best approach to deal following scenario.
I want to create a dynamic table like following which perform Add Update Delete -
Player SmartGoal Decision Thinking
ABC 10 10 9
PQR 7 9 10
next time table would be -
Player Decision Learning
ABC 10 5
PQR 7 9
I am using c#
I think we have to create databinding to control and control programmatically.
What control should I use?
What is the best approach for this scenario ?
You should use GridView control.
Or for more advanced and neat stuff you probably want to try [telerik][2] controls (not free)
This all needs to be driven by a database.
Just search for two tutorials to get started: how to create a sql server database, and how to use GridView control.
if you need to display hierachical data you can try to use ListView control:
As per my understanding you are fetching data from DB and binding to some control. I thinks the best approach for this to get the dataset from Database and bind to the gridview. You can find many example on net for bind sql datset on internet. Please let me know for any further clarification.
Thanks
Uttam
One Approach is:
Get the column name from Database and add them to a Label.
like lblColumn1.Text = colname1 & so on. Use loop.
Next step is to get the entries and add them to textbox/label again.
similar coding.
Forget not to use Try/catch for exceptions. You never know which column in DB isn't having values or null values.
For Edit/Add/Delete.
Add buttons before each each record to delete and edit
add button should be on top.
**No need to databind or anything like that.
**Basic knowledge of asp will do.
As per my understanding you are fetching data from DB and binding to some control. I thinks the best approach for this to get the dataset from Database and bind to the gridview. You can find many example on net for bind sql datset on internet. Please let me know for any further clarification.
If possible provide some more details. Here i am placing one example where dynamic table is created at runtime
Private Sub BuildSTX9Header()
Dim dtTemp As New DataTable
Dim dr As DataRow
dtTemp.Columns.Add(Me.GetLocalResourceObject("STXLocationID").ToString)
dtTemp.Columns.Add(Me.GetLocalResourceObject("SKU").ToString)
dtTemp.Columns.Add(Me.GetLocalResourceObject("SKU Description").ToString)
dtTemp.Columns.Add(Me.GetLocalResourceObject("MED").ToString)
dtTemp.Columns.Add(Me.GetLocalResourceObject("MSFSupportedProduct").ToString)
dtTemp.Columns.Add(Me.GetLocalResourceObject("Infor365 Product").ToString)
dtTemp.Columns.Add(Me.GetLocalResourceObject("SupportPlan").ToString)
dtTemp.Columns.Add(Me.GetLocalResourceObject("No.Users").ToString)
dtTemp.Columns.Add(Me.GetLocalResourceObject("SAM").ToString)
dtTemp.Columns.Add(Me.GetLocalResourceObject("LocationName").ToString)
dtTemp.Columns.Add(Me.GetLocalResourceObject("SerialNumber").ToString)
dtTemp.Columns.Add(Me.GetLocalResourceObject("Phone").ToString)
For i = 0 To dsData.Tables(0).Rows.Count - 1
dr = dtTemp.NewRow()
dr(0) = dsData.Tables(0).Rows(i)(2)
dr(1) = dsData.Tables(0).Rows(i)(3)
dr(2) = dsData.Tables(0).Rows(i)(4)
dr(3) = dsData.Tables(0).Rows(i)(5)
dr(4) = dsData.Tables(0).Rows(i)(6)
dr(5) = dsData.Tables(0).Rows(i)(7)
dr(6) = dsData.Tables(0).Rows(i)(8)
dr(7) = dsData.Tables(0).Rows(i)(9)
dr(8) = dsData.Tables(0).Rows(i)(10)
dr(9) = dsData.Tables(0).Rows(i)(11)
dr(10) = dsData.Tables(0).Rows(i)(12)
dr(11) = dsData.Tables(0).Rows(i)(13)
dtTemp.Rows.Add(dr)
Next
gvLoadRuntime.DataSource = dtTemp
gvLoadRuntime.DataBind()
ViewState("RowCount") = dtTemp.Rows.Count
ViewState("dvRuntimeData") = dtTemp
Thanks
Uttam
Related
Not done any programming for about 5 years so im a little rusty on this one.
I am building an Asset management system to make my job easier but am struggling on a few things.
What i have so far is the DB setup and populated with a small amount of data, The main site itself is built and the basic select statements to a gridview of any data currently in the system for that particular page/search and i also have an insert statement that is populating the relevant tables based on data filed in on a form on that page. All these functions are working seamlessly.
The problem i have is that one field that needs to be populated for the insert statement needs to populate based upon the selection that is being made from a drop down box in the same form.
i.e.
An Asset that is being registered into the system has an Asset Type (the type details are contained within a parent table Asset_Type) the form has 3 fields for display purposes (one of these fields will need to return an entry to the Asset table in the database from the form, this is working) that are taken from the Asset_Type table, the other fields only relate to the Asset table itself.
What i am trying to achieve is that a user goes to the page and sees a list of Assets registered (this is working) they then need to add a new asset by filling in a form lower down on the page (for is there and writes to DB) on this form is a drop down menu that queries the Asset_Type table and allows the user to select the Type by name (this works)
What i now need to get working is for the 2 other fields to populate based on what Asset type is selected.
These fields are currently textboxes in the form but can be changed if required.
The code i have behind the page is below:
Protected Sub Name_Model_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Name_Model.SelectedIndexChanged
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("AssetManagementConnectionString").ToString())
Dim cmd As New SqlCommand()
cmd.Connection = conn
cmd.CommandText = "SELECT [ID], [Name_Model], [Description_Spec] FROM [Asset_Type] WHERE ([ID] = #ID)"
cmd.CommandType = CommandType.Text
conn.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
If reader.HasRows Then
While (reader.Read())
{
Type_ID.text = (reader["ID"].tostring())
Name_Model.text = (reader["Name_Model"].tostring())
Description_Spec.text = reader["Description_Spec"].tostring
}
End While
End If
End Using
End Sub
So I'm not sure about Steve's comment, but I know he is a lot more familiar with VB based on questions of my own he has answered. However I am going to suggest something.
So "Drop downs" typically have 2 pieces of data associated with them.
A DisplayMember and a ValueMember (also a SelectedValue if it's a ComboBoxColumn)
You can reference either, I believe.
Then, you can do what you please with that value using an If Then or however you plan on using it.
assetValue = YourComboBox.DisplayMember.ToString
YourTextBox.Text = assetValue
or
If ComboBox.ValueMember = YourIndex Then
assetValue = Whatever
ElseIf
....
End If
Give that a try and let me know what you get. I am just throwing ideas out there.
I am new in .Net and I had a question regarding creating dynamic tables.
I am creating a page that adds a new row to a table (First Name, Last Name, Address, etc...) when a user clicks on a button. I have been reading that every time you do a postback to a dynamic table you have to re-create the rows. That is what I am doing.
I have been testing this and about 40 rows have being added already, when I click to add a new row, it runs completely slower and I can only imagine how long it would take to add 100 rows. I am assuming that it is because re-creating the rows takes time.
My questions is there a better approach or another way to accomplish task?
'***Edits Here what im currently doing
This is my table and button control:
Code when button is clicked, which creates the dynamic table:
Dim tblrow As TableRow
Dim tblcell As TableCell
Dim inputText As TextBox
tblrow = New TableHeaderRow
tblcell = New TableHeaderCell
tblcell.Text = tableCount 'variable used to count rows in the table
tblcell.HorizontalAlign = HorizontalAlign.Left
tblrow.Cells.Add(tblcell)
tblcell = New TableCell
inputText = New TextBox
inputText.ID = "txt_" & tableCount
tblcell.Controls.Add(inputText)
tblrow.Cells.Add(tblcell)
table1.rows.add(tblrow)
Now from what I learned and tested so far, everytime I do a postback I have to rebuild this table in order to keep all of the contents I entered into the table.
The next question is why dont you add another row using jquery so that you don't have to do postbacks. I have tried this approach and it worked well UNTIL I needed to put the information entered into the table into a database which required a postback. So I was back at my original problem.
Note, if there is a better way to approach this im all ears. Like I said before I am new to this language and im just trying to learn.
My suggestion for you will be to use the JqGrid it is a free open source jquery plugin but also available commercially. It is fast in data loading and have lots of dynamic features
If you know javascript and jquery then this will be easy for you to use, it comes as Asp.Net, mvc and php component
Are you using windows forms? If you are, use the ListView control and change the view (I believe thats what it's called...) to details. Then you can use one of the many tutorials like this to populate the list:
If not using Windows Forums sorry, I saw the VB.net part and assumed.
asp.net is a server side web technology which means that you are working with a stateless technology (basically). That is why you have to rebuild everything on every postback.
There are several ways of dealing with this:
using the asp.net ViewState and check for Page.IsPostback on PageLoad
using jQuery and clientside Templates like Pure and fetch the Data through webservices
I on your behalf would avoid creating a table manually like you showed above, but instead use templated controls and specify the behavior declaratively.
When you create a DataGrid for example and are using ViewState, you do not have to explicitly recreate the DataGrid, since asp.net is taking care of it when used correctly.
I have a GV and a DV that extract data from the same database. The link between these controls is when a record in the GV is selected, the DV displays more details about that record.
Do I need separate connections? Obviously, I haven't achieved this goal and am working on it. Thanks.
CLARIFICATION so as not to waste your time:
I am asking about connection, not datasource. The reason that I am not sure the same connection can be used is that with GV, the connection doesn't have any parameter. Whereas with the DV, it needs the record ID passed to it. Or am I wrong?
Here's the link to code on the net that makes me wonder:
http://asp.dotnetheaven.com/util/srcview.aspx?path=~/aspnet/samples/data/GridViewMasterDetails.src
I am a novice so am still confused with the terminology. Thanks for being patient.
They can use the same datasource. (Such as an ObjectDataSource or SQLDataSource)
You can use the same data source, i.e. a DataTable. But when the Gridview row is selected you will need to find the index of the selected row and then find the DataRow from the DataTable and rebind your DetailView to that DataRow. Hope this helps.
I think that you are using Visual Studio databinding, and I would say that same Connection(DataSource) object can be used (if that is the way it goes), but I would rather suggest you avoid this design time Visual Studio programming, although it is simple and fast
On the page I have created I have a search facility that if a doctors number is searched it will bring up the doctors details, once search button is clicked the results are displayed in textboxes (I cannot use gridviews because this is not wanted)
sample of code placed on the search button
Query statement = "SELECT DocNumber FROM tblDoctor WHERE DNum LIKE '%"
execute the query and get the result
The result is converted to string and Execute Scalar is used
DocNum.Text = Result1
Query statement = "SELECT DocName FROM tblDoctor WHERE DNum LIKE '%"
execute the query and get the result
The result is converted to string and Execute Scalar is used
DocName.Text = Result2
etc.... there are are 14 other textboxes that I want too display data in, so there is a large amount of repeated lines of code following the structure above. Can anyone suggest a better way of doing this?
Another problem of repetition of code comes from the prev page that is linked to it. The page before has a summary of details of doctors, once the row is clicked it takes you to this page displaying a more detailed view of their personal details. The doctor number selected will be passed onto the more detailed view using a querystring so I have the code
Automatic population of the selected doctors will fill the labels
on page load
Request the query string and store into variable dNum
Query statement = "SELECT DocNumber FROM tblDoctor WHERE DNum = " & dNum"
Get result from query convert to string and use execute scalar
lblDocNum.Text = Res1
Query statement = "SELECT DocNumber FROM tblDoctor WHERE DNum = " & dNum"
Get result from query convert to string and use execute scalar
lblDocNum.Text = Res1
etc...
What I am doing works correctly but the coding style looks poor. Any help would be much appreciated.
Thank you
Why not use a DataReader or DataSet or whatever you prefer to return the whole record, then simple move from column to column and populate the textboxes that way? Instead of returning one value at a time.
If the goal is less code,
SELECT * FROM tblDoctor WHERE xxx
into a DataTable or DataReader, as Thyamine suggested, above.
From there, you could also put the textboxes in an HTML table in a Repeater which you will bind to that datatable. You won't have to individually assign any of the values, the databinding will do it for you!
I know that people think HTML tables are evil but it's the easiest way to line up the labels I assume you will also want.
I also know that the control I suggested is called a Repeater but you only have one record. If you don't tell the compiler, I won't. :)
From parts of your question, it sounds like you're wondering whether to send all of the bits of information along in the querystring but that doesn't sound like a good idea to me because it invites users to mess with the data in the querystring.
You didn't mention - are these textboxes meant for editing? A Save button inside the Repeater would have easy access to all of the controls to build your update statement. You could also put the Save button outside the repeater and refer to the repeater's first Item to find the controls.
If you followed my previous post
var filteredUser = from U in collection
select new {U.fname,U.lname};
gridView.DataSource = filteredUser;
gridView.DataBind();
Now I am trying to do this:
Format the column names based on properties of U. So for example if U.fname chancges to U.FirstName then I want my gridview column name to reflect the same
If I enable paging through the design view, code compiles but when I launch the web app, it fails stating 'The data source does not support server-side data paging'
Edit::Found this for item # 2
link text
1) Are you using AutoGenerateColumns="True" on your GridView or binding them yourself? I would think that (1) would work if AutoGenerateColumns is true. You lose a lot of control over how the columns are displayed, but it ought to work. If you are binding them yourself, I think you will just need to update the bound column names whenever the name of the data field changes or alias the name in the select clause so that it remains the same.
var filteredUser = from U in collection
select new {FirstName = U.fname, LastName = U.lname};
2) Does your collection support IEnumerable<U> or just IEnumerable? I believe that LINQ uses Skip() and Take() to support paging so it would need to support the generic enumerable interface.