Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form in which i have textbox of State,City, and branch.
I have also three tables State[stateid int, States], City[cityid int, city, stateid int], Branch[branchid int, branchname, cityid int].
here stateid is primary key in State Table and forien key in city table.
and cityid is primary key in City Table and forien key in branch table.
Now i want as i enter the value in state text box then the value store in own table[State Table.] and increase by 1 itself. Then as i enter city textbox then cityid increase by 1 and city name and stateid will also show in City Table itself.
Like this

State Table
stateid States
1 Haryana
2 Punjab
3 Rajasthan

City
cityid cityname stateid
1 sirsa 1
2 rohtak 1
3 Barnala 2
4 Hanumangarh 3

like this i want to enter the data by text boxes.
How can i do.

Thanx.
Posted

1 solution

Yes, you can do that.

For inserting, you need to first insert into the Main Table/Primary table, then in the Foreign Key Table.

So, in your example, for State, first you need to insert in State Table and then get the inserted ID and insert into the City Table.

To get the most recently inserted ID, you can use @@IDENTITY or SCOPE_IDENTITY(). Refer one example - Re: How to add values of primary key column into foreign key column of other table?[^]

While inserting into main Tables, if you want that ID should be Auto-Incremented, then you need to set the "Is Identity" property to "Yes" and "Identity Increment" property to the number you want to increment to.

Refer - Auto increment primary key in SQL Server Management Studio 2012[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900