Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Please help with a Problem When Adding new data row
 

Please help with a Problem When Adding new data row

I have a SQL 2005 Express database that has a table with the fields :

Primary (which is my Primary Key and it AutoInc)

RecsordID (is an Int and allows Null

WorkOrder (is a varChar(50)

I have created a form that has a DataGridView, Bindingsource, TableAdptor, and a BingingNavigator all through the Designers in VB Express 2008

When I add a new row to the table I am wanting to in code take the highest RecordID number and add 1 to it and make that the new rows RecordID value but I can't seem to figure out how to access the last RecordID value in the table.

I can move to that rowwith bindingsource.movelast

I can add values to the cells with the DefaultValuesNeeded Method

I can get it in a textbox but I need it in a variable as an Int so I can do the math

Thank you for any help provided

DarrylTrucker  Wednesday, March 05, 2008 8:25 PM

This should help:

Dim LastValue As Integer = (From Record In DataSet.Table Select Record.ID).Count + 1

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2843894&SiteID=1

Se3k385  Saturday, March 08, 2008 7:47 PM
Hi

Notice that you are working with a ConnectionLess Object. This means that the DataAdapter will persist the data back to the SQL database. This also means that you don't have to take the auto Increment into account.

If you have a DataGridView, AND you have a auto incrementing number, you will notice that the Primary Key will be raised automatically. This is done by the DataTable logic.

To save every record to the database you can e.g. call the Update statement of your table adapter after your line has validated in the DataGridView.

To add a new line programmatically you can e.g. use this code

// Create a new row and add it to the dataset
myDataSet.KeywordRow row = (myDataSet.KeywordRow)myDataSet.Keyword.NewRow();

row.KeywordName = newValue;
row.KeywordActive = true;
myDataSet.Keyword.Rows.Add(row);

// Persist row to database
this.taKeyword.Update(dsSalesBooster.Keyword);

// get the new Id
int myID = row.KeywordID;

Hope it helps

DamPee  Wednesday, March 05, 2008 11:46 PM

I am new to database work so please explain a little more.

To get my new RecordID I think I need to get the RecordID of the last row before adding the new row and add 1 to it. RecordID cell does not auto increment. Also I am sorry but I forgot to say I am using VB Express 2008. I do understand

most for the example though, what do you mean by the term "Keyword".

Thank you for your help

DarrylTrucker  Thursday, March 06, 2008 12:14 AM

If you're using an Identity column, you can return @@identity in your INSERT sp.

Adam

Adam D. Turner  Saturday, March 08, 2008 7:45 PM

This should help:

Dim LastValue As Integer = (From Record In DataSet.Table Select Record.ID).Count + 1

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2843894&SiteID=1

Se3k385  Saturday, March 08, 2008 7:47 PM
This last example is LINQ (so only usable with .Net 3.5)
DamPee  Saturday, March 08, 2008 7:50 PM

You can use google to search for other answers

Custom Search

More Threads

• Run-time connectionString Problem
• control.databindings
• Understanding Custom DataGridViewCell edit and display values
• Dataset designer: optional parameter problem
• Cell Data Changed
• Best Practises with Remote SQL Server
• datagrid
• DataView, CType Function & Page_Load procedure: How can I create a derived variable?
• In search for the "fast" Data and Business Layers with TableAdapters and VS2005
• Datagridviews Button Click Event???