ASP .NET Beginner question...

liunx

Guest
Howdy All,

Just a few, extremely basic questions from a true beginner. I've recently started learning HTML, Javascript & the like...and I'm moving into ASP .NET. My basic question (which may turn out not to be so basic) is what's the easiest way to format small amounts of data I've queried from a simple database? (I have limited SQL experience as well :( )

The app I'm attempting to create is simply to load up the information currently in the DB, displaying it, and 'refreshing' the data whenever a refresh button is clicked.

Any & all help/advice is greatly appreciated. Thank you again!Use a control like DataGrid,DataList or Repeater. Each has pros/cons.Thanks afterburn, I used the DataGrid and it worked out pretty well. Thanks for the help. :)Are you using the Auto Generate Fields option? Try using the advanced features by specifying False in the option. If using VS.net you can reach it by clicking on the item, right click-> Properties. In the properties window usually on the right lower hand in VS.net, click the hyperlink for "Property Builder", This will allow you use hyperlink columns and more.Thanks again Afterburn, I did what you said and turned off the auto generate fields option. But, would you mind explaining a bit about the hyperlink columns? Does it simply offer the function of naming your own info in the columns? How about defining the number of columns/rows you want displayed even if the table is blank?

Thanks again! :)The use of hyperlink columns allow you to link to other pages with parameters. There is a option in the dialog for

Text Field: Property allows you to display data from the data source as the linked text.

DataFormatString: Property allows you to format the Url string, such as Query String Parameters

Url Field: Property allows you to specify the Url from the data source.

The Templated Column allows you to format the data exactly like you want it. if you wanted first Name, Last Name then you can do it in this type of column. However you must use the Container.DataBinder() inside of the page to do this.

Usually I create what most call "Business Layer Objects", Its a class that holds the information. Using events to bind the data to a custom Templated Column.Perfect, thanks again. I had read through all the info about the types of columns, etc. just after I replied. Getting the hang of it now.

Just one last question, if that's alright. Say I have a database table that's empty occasionally. I'd still like to see, say 10 rows, if the table is empty. If it's not, then perhaps only show the rows that have data in them...say only 2 or 3 rows. But if the table is empty, I'd still like to see a default set of empty rows. Any way to set this up? I've set all the headers using the Bound Columns (instead of hyperlink or template). If you have any further advice I'd really appreciate it.

Thanks again for helping me through this Afterburn. :thumbup:Using Business Layer Objects and the Event ItemCreated you can dynamically and programmatically add/remove columns. By using the event then doing your custom display logic, if new columns are needed you can use the Page.FindControl() method to then add columns.Okay, if the columns are set...and always will be...is there simply a way to create a default setting that initializes their number of data rows to say 10? I guess I don't need to dynamically create new columns, the columns are set...but if the database table is empty only the DataGrid header is visible...and I was hoping to actually have a visible Grid, even if the items were blank. Make sense? Sorry if I don't explain things too well.On the dataset that is returned.
Use the following code multiple times if needed

DataRow drow = ds.Tables["TableName"].NewRow();
drow["FieldName"] = "0";

for(int i = 0;i<10;i++)
ds.Tables["TableName"].Rows.InsertAt(drow,0);


Really the only way to handle that. I use it to specify a blank value for drop downs then use logic to make sure that its not selected when I insert data because of primary key violations.Gotcha, I think I'm getting it. I'll see if I can get that to work. Thanks again Afterburn. :) Slowly but surely seeing how this all works.

EDIT: Getting an interesting error as I'm working through this. In your: dRow = ds.Tables[" "].NewRow() line, I'm getting a...

'Cannot convert from type DataTableCollection to DataRow'

Any thoughts?Originally posted by RCurrie
Gotcha, I think I'm getting it. I'll see if I can get that to work. Thanks again Afterburn. :) Slowly but surely seeing how this all works.

EDIT: Getting an interesting error as I'm working through this. In your: dRow = ds.Tables[" "].NewRow() line, I'm getting a...

'Cannot convert from type DataTableCollection to DataRow'

Any thoughts?


Are you using the corrected value to the name of the table that you are using.

When you create a data set you can insert multiple "tables" into it. You need to specify the name of the table using the Data Adapter for the db your using. I am using that exact code on my stuff.

Where "ds" is a DataSet type. As you might have noticed that types in C# are Strongly Typed. Meaning that if it does not match the correct type an error will occur.Yep you're right again. I did indeed miss a few of the proper names/ids.

EDIT: Ugh, well I got everything to work. Tuns out I had my one table (of two) pointing to the wrong one. Spent a good couple hours thinking it was something else...change one little letter...and it works great. Figures. At least I've been learning non stop.


Here was my code:

Acon.Open()
Scon.Open()

Dim ADScmd As New SqlDataAdapter(AQry, Acon)
Dim SDScmd As New SqlDataAdapter(SQry, Scon)

Dim ADS As New DataSet()
Dim SDS As New DataSet()

ADScmd.Fill(ADS, "AgentSessionQueue")
SDScmd.Fill(SDS, "StationSessionQueue")

Dim AgentTable As DataTable = ADS.Tables.Item("AgentSessionQueue")
Dim StationTable As DataTable = SDS.Tables.Item("StationSessionQueue")

Dim ADView As DataView
Dim SDView As DataView

Dim i As Integer

ADView = New DataView(AgentTable)
SDView = New DataView(StationTable)

For i = 1 to 3
ADView.AddNew()
SDView.AddNew()
Next

AgentGrid.DataSource = ADView
AgentGrid.DataBind()

StationGrid.DataSource = SDView
StationGrid.DataBind()

Acon.Close()
Scon.Close()



One last question if you're still up for it. I'd only like these new, blank rows to be present if the table I'm working on is initially empty. If there is data in it, I'd like for only the rows with data to show. So I was wondering if there was a way to check for data in any field to see if it's null...if it is, just ignore the For loop I have in there. Something of the form:


If SDS.Tables.Item("StationSessionID") Is "" Then
For i = 1 to 3
SDView.AddNew()
Next
end If



Thanks again Afterburn, I really appreciate the continued help.Dim AgentTable As DataTable = ADS.Tables.Item("AgentSessionQueue")
Dim StationTable As DataTable = SDS.Tables.Item("StationSessionQueue")

Dim drow as DataRow = AgentTable.NewRow()

If AgentTable.Rows.Count <= 0 Then

For i = 1 to 3
AgentTable.Rows.InsertAt(drow,0);
Next

End if




That should be correct. Haven't used add Row on a DataView, also not needed as its just an extra variable and more memory.

Edit: used C# syntax. Corrected it for VB.netThanks Afterburn, you rock!!! Thanks again for all the help! So much to learn about ASP.NET, but definitely worth it. :D

The one issue I get when I attempt to do it this way is inside the For Loop I get the error "This row already belongs to this table" so I must be doing some assignment wrong...? It highlights the line:

AgentTable.Rows.InsertAt(ADRow,0)

For the error. I did however get it to work this way:


Dim ADView As DataView

ADView = New DataView(AgentTable)

If AgentTable.Rows.Count <= 0 Then
For i = 1 to 4
ADView.AddNew()
Next
End If

AgentGrid.DataSource = ADView
AgentGrid.DataBind()


But like you said, it's just extra variables, so I'd like to get it working the way you suggested it. Any thoughts on the error? I'm trying to work it out...DataView might keep the Row's primary key and increment it. So you my have to create the DataRow in the loop.That was it Afterburn. Moving the declaration of DataRow inside the loop works perfectly.


If AgentTable.Rows.Count <= 0 Then

For i = 1 to 3
Dim ADRow As DataRow = AgentTable.NewRow()
AgentTable.Rows.InsertAt(ADRow,AgentTable.Rows.Count)
Next

End if


Thanks again Afterburn. Projects done & couldn't have done it without you.:cool:
 
Back
Top