.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
MarieAdela
Imran Ghani
Post New Web Links

Get primary key value from the table.

Posted By:      Posted Date: October 27, 2010    Points: 0   Category :ASP.Net
 

I am inserting data into two tables on a button click.

I need to insert the primary key generated for Table A into the Table B as a foreign key.


How do i get the primary key value and insert into Table B?




View Complete Post


More Related Resource Links

Using table with no primary key in CTP4

  

Hi,


Does anyone knows if CTP 4.0 supports table with no primary key. I know Entity framework does.



Can't perform Create, Update or Delete operations on 'Table(outbox)' because it has no primary ke

  

 Hi,

I'm a beginner in asp.net and I have some problem inserting data into sql server 2005 with linq.

 

my code is : 

 

        Dim Result As New SMS.SendResult
        If Result.status = 0 Then
            Label1.Text = "Your Message Sent Successfuly"
           
            Dim db As New IGSSDataContext
            Dim out As New outbox With {.ClientID = intRandomNumber, .From = User.Identity.Name, .TO = TextBox1.TabIndex, _
            .Time = Now, .Message = Message}
            out.ID = (ID)
            db.outboxes.InsertOnSubmit(out)
            db.SubmitChanges()
           
            TextBox1.Text = ""
   &nb

Error Message: The referenced table must have a primary or candidate key. [ FK Name = FK_tblA_tblB ]

  
An Entity Data Model is used to generate the script to create  a SQL CE database.  It creates all of the 10+ tables and their primary keys without any problem. A lot of foreign key creations generate errors like the following: SQL Execution Error. Executed SQL statement ALTER TABLE [tblA] ADD CONSTRAINT [FK_tblA_tblB] FOREIGN KEY([A], [B], [C]) REFERENCES [tblB]([A], [BBB], [C]) Error Source: SQL Server Compact ADO.NET Data Provider Error Message: The referenced table must have a primary or candidate key. [ FK Name = FK_tblA_tblB ] The primary keys are there and correct. Could anyone offer some tips on how to figure out what is wrong?hz

Two Primary key for table???

  
i HAVE FOLLOWING SCRIPT TO CREATE TABLE. create table Grade_report (STUDENT_NUMBER SMALLINT NOT NULL, SECTION_ID SMALLINT NOT NULL, GRADE CHAR(1), CONSTRAINT stno_secid PRIMARY KEY (STUDENT_NUMBER, SECTION_ID)) tHE BOLD line shows that it has two primary key. would that be possible to create two primary key for a single table??  If not then what does it for?? may be unique key??? anyone can explain me please.     Thanks,      

adding Primary key to a data table

  
Hi All, I have a data table this table saves values from a form's add button and binds to a gridview on the final submit button adds to the data base. See my Data table below    DataTable dt = new DataTable();                   if (ViewState["CompDetails"] == null)            {                 dt.Columns.Add("CourseCode");                dt.Columns.Add("CourseName");                dt.Columns.Add("Fees");            }            else                dt = (DataTable)ViewState["CompDetails"];            DataRow dr = dt.NewRow();                                &n

Joins on non-indexed fields and table primary keys.

  

I have two tables (a,b) each with a primary key (which are clustered indexes). When I issue the t-sql statement

delete XX from t1 XX inner join t2 on (xx.A2 = t2

Update records related to primary table in sql

  

HI All,

For example

I have 2 tables  Person and Person_Child.

In Person table i have columns

Id, PersonId,PersonName,PersonAdrress,PersonAge,RowId

1,101,JAck,NJ,30,12

rowid is identity

 

and in the person_child table--here parentrowid refers to parent table rowid and again rowid is identity here

Id, PersonId,PersonName,PersonAdrress,PersonAge,ParentRowid,RowId

1,101,JAck,NJ,30,12,1

1,101,null,null,null,12,2

 

I need to update the child table values as it is in parent tables where it is null.

I have to do for many tables.

Is there any way to do it dynamically.

 

Thanks,

JR

 

 

 


What is the best way to get the primary key column name of a table in a stored procedure ?

  
Hi,

I'm writing a stored procedure which manipulates a table whose name must be given as a parameter. The stored procedure needs the primary key column name (by design the primary key must have only one column), and I thought I could get this from the system table instead of having to provide it as a parameter. So I need to put the primary key column name in a variable.

The best way I found so far is this :

declare @keyname nvarchar(1024)
create
table #keys (table_qualifier nvarchar(255),table_owner nvarchar(255),table_name nvarchar

Partitioned Table vs Primary Key aggrevation

  
I've got a large fact table that contains a TimeStamp column and an ID column.   The table is partitioned on the TimeStamp column.  Up until now it has never had a Primary Key.  However, now I would like to create a PK on the ID column, because I am building another table that has a FK that references the ID column of my existing table.

And it looks like I can't.

I can't add a PK or Alternate Key to the partitioned table unless the key includes the partitioning column.   If I include the Partitioning column in the Key, I have to include it in the FK of the other table, which isn't an option.. at least, not one I'm willing to take.

Please tell me there's a work-around for this, somebody... I already know that I could enforce RI by other means;  but I'm hoping for a work-around that allows me to put a PK on this partitioned table.

Select Primary Key Column of Table returns 0 rows

  

I have three tables: Design, Box, Wall.

Design has a column DesignId that is a GUID. It does not allow nulls, is unique, and is the primary key. The identity is set to false for compatibility with Entity Framework v4.

Box has  a column BoxId that is a GUID. It does not allow nulls, is unique, and is the primary key. The identity is set to false as well.

Box also has a column DesignId that is a GUID. It does not allow nulls, is not unique and is not the primary key.

Wall has  a column WallId that is a GUID. It does not allow nulls, is unique, and is the primary key. The identity is set to false as well.

Wall also has a column DesignId that is a GUID. It does not allow nulls, is not unique and is not the primary key.

Wall also has a column BoxId that is a GUID. It does not allow nulls, is not unique and is not the primary key.

Box.DesignId has been set as a ForeignKey relating to Design.DesignId.

Wall.DesignId has been set as a ForeignKey relating to Design.DesignId

Wall.BoxId has been set as a ForeignKey relating to Box.BoxId.

 

If I execute      SELECT DesignId FROM Design     I see the correct number of rows.

If I execute     SELECT BoxId From Box        I see zer

Mapping table without primary key in Nhibernate

  

How can we map a table without having primary key in nHibernate.

I have a table which does not contain any primary key, so how can i map this table in .hbm.xml file.


Please advice.


How to set Primary Key to imported Access Table into SQL Server?

  
Ive already imported database from Access to SqlServer.
Tried to set a primary key and save it but the thing is SqlServer states "Saving changes is not permitted".
How can I make it work?
Any ideas . Please help. Many thanks

How to set Primary Key to imported Access Table into SQL Server?

  

Ive already imported database from Access to SqlServer.
Tried to set a primary key and save it but the thing is SqlServer states "Saving changes is not permitted".
How can I make it work?
Any ideas . Please help. Many thanks

the column in table tbl_table1 do not match an existing primary key or UNIQUE constaint

  

tbl_table1
ID primarykey (autoinc)
UserId uniqueidentifier
IdNumber primarykey int

 I'd like to have a relationship between two tables.

Where is the problem?

thanks.




Create partition on a table with difference in partition key and primary key

  

Hi All,

Can we create a partition on a table which is having primary key different and partition key different? Because in one of my table Id is primary key and I have to do a partition on date wise if possible how we can, I am new to partition

Please find the below code which i was trying to create a partitions

I have three different File groups [PRIMARY], FG2

error when I try and drop a primary key on a table

  

I have a table which is populated with data. It has a primary key defined on one of the columns. I need to first
drop the primary key constraint, and then add it back on the next line, with another column, so that their will be two
primary keys for the table instead of one. (Otherwise known as a Composite primary key)

The problem is that I cannot drop the primary key using sql. My drop code is on line 1 below.

Here is my sql

1    ALTER TABLE [dbo].[ShipTest] DROP CONSTRAINT TruckId_PK

ERROR: line 1: TruckId_PK is not a constraint


2    ALTER TABLE [dbo].[ShipTest] ADD PRIMARY KEY ([TruckId],[VehicleId])

Why am I getting this error message? I can't find an answer or hint on google either?

 

USE [shippingDev]
GO
SET ANSI_NULLS ON
GO
CREATE TABLE [dbo].[ShipTest] (
 [TruckId] [int] NOT NULL,
 [VehicleId] [int] NOT NULL,
 [VehicleName] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
PRIMARY KEY CLUSTERED
(
[TruckId] ASC,
[VehicleId] ASC
) WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]


Saving primary key constraint when creating new table using SELECT INTO statement - SQL Server 2008

  

Using SQL Server 2008:

We have a main database - MAIN.   For monthly data extracts, I create new tables with relevant data in another database called EXTRACT.

I use SELECT INTO statements to create the tables in the EXTRACT schema.  How do I preserve the primary key constraints in the EXTRACT tables?  Do I need to write separate queries to set them?

 


Categories: 
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend