Click here to Skip to main content
16,016,022 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
s_reg,s_don are two tables in my database reg_id is primary key of s_reg and don_id is of s_don
i have joined two tables using reg_id of both the tables
but in s_don table the reg_id column taking only zero and not showing the actual reg_id of s_reg

What I have tried:

C#
protected void Button1_Click(object sender, EventArgs e)
{
    using (studentDataContext dt = new studentDataContext())
    {
        var log = (from s in db.s_dons
                   join m in db.s_regs on s.reg_id equals m.reg_id
                   select new
                   {
                       s,m
                   }).FirstOrDefault();
        try
        {
            s_reg reg = new s_reg();
            s_don ureg = new s_don();
            ureg.dtype = DropDownList2.SelectedItem.Text;
            ureg.dtitle = DropDownList1.SelectedItem.Text;
            ureg.dnmame = TextBox1.Text;
            ureg.ddate = TextBox2.Text;
            ureg.demail = TextBox5.Text;
            ureg.dmob = TextBox6.Text;
            ureg.dadd = TextBox7.Text;
            ureg.ddepart = ddlTest.SelectedItem.Text;
            ureg.damt = TextBox3.Text;
            ureg.dcom = TextBox4.Text;
            dt.s_dons.InsertOnSubmit(ureg);
            dt.SubmitChanges();


            var frps = dt.s_dons.Where(w => w.dnmame == TextBox1.Text);//update column
            foreach (var ti in frps)
            {
                ureg.reg_id = reg.reg_id;
                dt.SubmitChanges();
            }

            /*reg.smobile = TextBox6.Text;
            dt.s_regs.InsertOnSubmit(reg);
            dt.SubmitChanges();
            Response.Write("<script>alert('Submited successfuly');</script>");
            */

        }
        catch (Exception ex)
        {
            Response.Write("Error:" + ex.Message);
        }
    }
     }


s_reg:
SQL
CREATE TABLE [dbo].[s_reg](
[reg_id] [int] IDENTITY(2000,1) NOT NULL,
[pass_id] [int] NULL,
[sname] [nvarchar](50) NULL,
[smobile] [nvarchar](50) NULL,
[semail] [nvarchar](50) NULL,
[sdept] [nvarchar](50) NULL,
[scateg] [nvarchar](50) NULL,
[susnm] [nvarchar](50) NULL,
[spass] [nvarchar](50) NULL,
CONSTRAINT [PK_s_reg] PRIMARY KEY CLUSTERED
(
[reg_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

s_log:
SQL
CREATE TABLE [dbo].[s_log](
[log_id] [int] IDENTITY(1000,1) NOT NULL,
[reg_id] [int] NULL,
[susnm] [nvarchar](50) NULL,
[spass] [nvarchar](50) NULL,
CONSTRAINT [PK_s_log] PRIMARY KEY CLUSTERED
(
[log_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Posted
Updated 5-May-18 20:10pm
v3
Comments
Wendelius 5-May-18 13:51pm    
In order to see why the table rejects the foreign key value you should post the CREATE TABLE statements
Member 13809843 5-May-18 14:03pm    
s_reg:
CREATE TABLE [dbo].[s_reg](
[reg_id] [int] IDENTITY(2000,1) NOT NULL,
[pass_id] [int] NULL,
[sname] [nvarchar](50) NULL,
[smobile] [nvarchar](50) NULL,
[semail] [nvarchar](50) NULL,
[sdept] [nvarchar](50) NULL,
[scateg] [nvarchar](50) NULL,
[susnm] [nvarchar](50) NULL,
[spass] [nvarchar](50) NULL,
CONSTRAINT [PK_s_reg] PRIMARY KEY CLUSTERED
(
[reg_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


s_log:
CREATE TABLE [dbo].[s_log](
[log_id] [int] IDENTITY(1000,1) NOT NULL,
[reg_id] [int] NULL,
[susnm] [nvarchar](50) NULL,
[spass] [nvarchar](50) NULL,
CONSTRAINT [PK_s_log] PRIMARY KEY CLUSTERED
(
[log_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Patrice T 5-May-18 20:33pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.

1 solution

As far as I can see the reg_id on s_log is not defined as a foreign key. Most likely because of this, proper attributes have not been created for your model. Another thing is, if I recall correctly, the s_reg should have a collection containing the child entities so that upon insert the newly generated key can be used.

So try to define the foreign key into the database and update the model. Also have a look at The LINQ to SQL Object Model | Microsoft Docs[^]
 
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