Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 16th, 2003, 04:22 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default Adding a Row to a DataGrid

Hello,

I'm trying to add a row of data to the data grid, by updating the data source. The data source is a DataTable object. How I do this is:
Code:
objTable.Rows.Clear()

dim objRow as DataRow = objTable.NewRow()
objRow("Name") = "New Name"
objRow("Text") = "New Text"
objTable.Rows.Add(objRow)

DataGrid1.DataSource = objTable
DataGrid1.DataBind()
DataGrid1.EditItemIndex = 0
However, I'm getting an error when I add the data row. There is another field called ID, which is an identity in SQL Server. It is erroring because it isn't provided. I thought that it would automatically increment the value. How do I specify the ID field to be an autonumber, and how do I specify the auto numbering to start where the data left off?

Thanks,

Brian Mains
__________________
Brian
 
Old December 16th, 2003, 04:52 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

In the Enterprise Manager, open your table in Design View and then click the ID column. In the properties grid for the column, you'll see Identity. Set it to Yes. You can also set the Identity Seed (the start) and the Identity Increment.

HtH,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old December 17th, 2003, 09:19 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hello,

The ID field is already an identity. When I do a Tables.Rows.Add(objRow), an error occurs, because there is no value for ID. I figured it is due to the fact that I have to recreate the primary key identity field; however, how do I ensure that the identity will leave off where the numbering system last was? For example,

the last row of data is:
6 Test The testing environment

How do I ensure that when I insert a new row, that the numbering will start at 7? I want to avoid requerying SQL Server for the last ID because I am required to use stored procedures and I don't want to have to maintain more SP's (already have too many).

Thanks.
 
Old December 17th, 2003, 11:53 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

First of all, don't confuse Identity with the Primary key. When a field is a primary key, it's not necessary an identity and vice versa.

When you have set the Identity and then set the Increment to 1, SQL Server will automatically assign 7 when to a record, when the previous record was 6. IMO, it's dangerous to assume the next number is going to be 7. What about two users adding a record at the same time. One gets 6, the other gets 7. However, the first will then assume 7 is the next record, while in fact that number has already been taken.

You can use @@IDENTITY to return the ID of the record that was last inserted from a sproc. But, for the reasons mentioned, don't use that information to assume the ID of the next record. Have the database handle that number.

What exactly are you doing? Maybe there are other ways to do the same thing....


Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old December 17th, 2003, 12:50 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hello,

I'm using a data grid to do adds, updates, and deletes. I have the ID field specified as:

[ID] [int] IDENTITY (1, 1) NOT NULL

However, I'm editing the data in a disconnected environment, which could the problem be is that it's not connecting back to SQL Server before I try and get the ID number? Or does ADO.NET increment the ID field?

Thanks,

Brian Mains
 
Old December 17th, 2003, 03:03 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Are you using the EditCommandColumn?

 
Old December 17th, 2003, 04:21 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

No. I didn't do it that way, because I didn't know that you could add a column through the data grid functionality. I thought you had to add it to the data source. Is this correct?
 
Old December 17th, 2003, 04:27 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I use the EditCommandColumn and when "edit" is clicked it adds textbox controls to the columns that aren't read only. It also replaces "edit" with "update" and "cancel". When "update" is clicked it takes what is in the textbox controls as parameters and sends the updated data to the database. "cancel" takes away any item for editing in the DataGrid.

 
Old December 17th, 2003, 04:28 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Here is what the HTML looks like:

<asp:DataGrid id="dgDates" CellPadding="2" runat="server" AutoGenerateColumns="False" DataKeyField="id" OnUpdateCommand="dgDates_UpdateCommand" OnEditCommand="dgDates_EditCommand" OnDeleteCommand="dgDates_Delete"
                    OnCancelCommand="dgDates_CancelCommand">
                    <Columns>
                        <asp:BoundColumn DataField="id" ReadOnly="True" Visible="False" />
                        <asp:BoundColumn DataField="event_date" HeaderText="Event Date" />
                        <asp:BoundColumn DataField="event" HeaderText="Event" />
                        <asp:BoundColumn DataField="event_time" HeaderText="Time" />
                        <asp:BoundColumn DataField="location" HeaderText="Location" />
                        <asp:ButtonColumn Text="Delete" CommandName="Delete" />
                        <asp:EditCommandColumn EditText="Edit" UpdateText="Update" CancelText="Cancel"></asp:EditCommandColumn>
                    </Columns>
                </asp:DataGrid>

 
Old December 17th, 2003, 10:35 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alyeng2000
Default

hi Brian Mains,

do you mean that current identity value for a table is increased without doing somthing.

Ahmed Ali
Software Developer





Similar Threads
Thread Thread Starter Forum Replies Last Post
Scrollbar in row of Datagrid Except Header Row Theone84 ASP.NET 2.0 Professional 0 August 11th, 2008 12:10 AM
Adding another row using footer in DataGrid liduwan ASP.NET 1.0 and 1.1 Basics 0 January 16th, 2005 11:18 AM
Manage data row by row in datagrid Dragonist Classic ASP Databases 5 July 29th, 2004 04:17 AM
Adding a button to each row of a datagrid badgolfer ASP.NET 1.0 and 1.1 Basics 1 March 1st, 2004 10:05 AM
Dynamically adding a row to a DataGrid vwindrow ASP.NET 1.x and 2.0 Application Design 2 August 20th, 2003 03:10 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.