Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping! 1

Status
Not open for further replies.

arpan

Programmer
Oct 16, 2002
336
IN
The following ASPX code creates a dynamic DataSet which is then displayed in a DataGrid (using a DataView):

Code:
[COLOR=brown]<script language=VB>
Sub Page_Load(obj As Object,ea As EventArgs)
Dim objDS As New DataSet
Dim objDT As New DataTable("Users")
objDT.Columns.Add("ID",System.Type.GetType("System.Int32"))
objDT.Columns.Add("FName",System.Type.GetType("System.String"))
objDT.Columns.Add("LName",System.Type.GetType("System.String"))
objDT.Columns("ID").AutoIncrement=True

objDS.Tables.Add(objDT)

Dim objDR As DataRow=objDT.NewRow()

objDR(1)="George"
objDR(2)="Michael"

objDT.Rows.Add(objDR)

Dim objDV As New DataView
objDV.Table=objDS.Tables("Users")
myDataGrid.DataSource=objDV
myDataGrid.DataBind()
End Sub
</script>

<asp:DataGrid id="myDataGrid" runat=server/>
[/color]
As such, the above code, when executed in IE 6, gets executed as expected i.e. the page shows a table having 3 columns: ID, FName & LName & only 1 row of record: 0, George, Michael. How do I add more records in this DataGrid? For e.g. I want the 2nd row records as: 1, Andre, Agassi; the 3rd row as: 2, Steffi, Graf; the 4th record as: 3, Carl, Lewis so on & so forth...

Please note that the records are not being retrieved from a database table; they have been (& have to be) added manually as shown above! I am a newbie as well :)

Thanks,

Arpan
 
Code:
Dim objDR As DataRow=objDT.NewRow()
objDR(1)="George"
objDR(2)="Michael"
objDT.Rows.Add(objDR)

objDR=objDT.NewRow()
objDR(1)="Andre"
objDR(2)="Agassi"
objDT.Rows.Add(objDR)

objDR=objDT.NewRow()
objDR(1)="Stefi"
objDR(2)="Graff"
objDT.Rows.Add(objDR)

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
arapn,

Just our of interest why do the rows "have to be added manually"? Is it just that you don't have a db to load them from? If it is have you considered loading them from an XML file as this is very straightforward and will allow you to easily add/remove rows?

Have a look at an example at the follwoing address if you are interested:




----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Thanks, Dazzled, for your reply but to be very honest I was wondering how stupid I can be asking such a stupid question!

Getting back to ca8msm's question-actually your question is justified - why would one have to add records manually? The truth is I have just started with ASP.NET & presently, I am trying to grasp the concepts of DataSet. That is precisely the reason of adding records manually to a DataSet which I have created programatically! I haven't touched ADO.NET or Databases yet!

Thanks once again to both of you,

Regards,

Arpan
 
Hello Dazzled,

One last question please regarding the same ASPX code - I added the following code (which is in red color) in order to define the column ID as a Primary Key:
Code:
<script language=VB>
Sub Page_Load(obj As Object,ea As EventArgs)
Dim objDS As New DataSet
Dim objDT As New DataTable("Users")
objDT.Columns.Add("ID"..blah..blah..blah..)
objDT.Columns.Add("FName"..blah..blah..blah..)
objDT.Columns.Add("LName"..blah..blah..blah..)
objDT.Columns("ID").AutoIncrement=True
objDS.Tables.Add(objDT)
[red]
Dim objDC As DataColumn={objDS.Tables("Users").Columns("ID")}
objDS.Tables("Users").PrimaryKey=objDC
[/red]
Dim objDR As DataRow=objDT.NewRow()
objDR(1)="George"
objDR(2)="Michael"
objDT.Rows.Add(objDR)

objDR=objDT.NewRow()
objDR(1)="Andre"
objDR(2)="Agassi"
objDT.Rows.Add(objDR)
..........
..........
</script>

but the newly added code defining the Primary Key generates the following error:[blue]

Array initializers are valid only for arrays, but the type of 'objDC' is 'System.Data.DataColumn'.[/blue]

pointing to the 1st red code line. If I get rid of the curly brackets in this code line, then another error gets generated which points to the 2nd red code line:[blue]

Value of type 'System.Data.DataColumn' cannot be converted to '1-dimensional array of System.Data.DataColumn'.[/blue]

Where am I going wrong?

Thanks once again,

Regards,

Arpan
 
Hey Dazzled,

You need not answer my follow-up query regarding Primary Key. I have got the solution. Omitted brackets in the "Dimming" line! It should have been
Code:
[red]Dim objDC As DataColumn[b][highlight red][white]()[/white][/highlight][/b]={objDS.Tables("Users").Columns("ID")}[/red]

instead of
Code:
[red]Dim objDC As DataColumn={objDS.Tables("Users").Columns("ID")}[/red]

Thank you very much,

Regards,

Arpan [wavey2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top