I am working in dot net with c# and LINQ and SQL server as database
I have three tables s_reg,s_log,s_pass
s_reg has reg_id primary key,s_log has log_id and reg_id as primary and foreign key resp.,s_pass has pass_id and log_id as primary and foreign key
I want to update password of a ID in s_reg and keep the old password in the s_pass table and also update new password in s_log table with reg_id
i have updated password column in s_reg but it is only accepting reg_id(as it is on auto increment) and new password instead of old and showing remaining fields as NULL
also changes are taking place in s_log and s_pass tables but both are not accepting reg_id, the reg_id column is showing either NULL or 0
What I have tried:
protected void Button1_Click(object sender, EventArgs e)
{
using (studentDataContext ps = new studentDataContext())
{
var check = (from m in ps.s_regs
join s in ps.s_logs on m.reg_id equals s.reg_id
where s.susnm == TextBox1.Text && s.spass == TextBox2.Text
select new { s, m }).FirstOrDefault();
if (check != null)
{
try
{
s_log log = new s_log();
using (studentDataContext database = new studentDataContext())
{
var up = ps.s_regs.Where(a => a.spass == TextBox2.Text).FirstOrDefault();
if (up != null)
{
s_reg reg = new s_reg();
reg.spass = TextBox2.Text;
ps.s_regs.InsertOnSubmit(reg);
ps.SubmitChanges();
Response.Write("<script>alert('save successfuly');</script>");
}
else
{
up.spass = TextBox3.Text;
log.reg_id = reg.reg_id;
ps.SubmitChanges();
Response.Write("<script>alert('Update successfuly');</script>");
}
}
s_pass ureg = new s_pass();
var chk = (from m in ps.s_passes
join n in ps.s_regs on m.reg_id equals n.reg_id
select new { m.ppass, n.sname }).FirstOrDefault();
if (chk != null)
{
try
{
ureg.pname = TextBox5.Text;
ureg.puname = TextBox1.Text;
ureg.ppass = TextBox2.Text;
ureg.reg_id = reg.reg_id;
ps.s_passes.InsertOnSubmit(ureg);
ps.SubmitChanges();
}
catch (Exception ex)
{
Response.Write("Error:" + ex.Message);
}
var query = (from ord in ps.s_logs
join trd in ps.s_regs
on ord.reg_id equals trd.reg_id
where trd.susnm == TextBox1.Text
select new { ord, trd }).FirstOrDefault();
if (query != null)
{
try
{
log.susnm = TextBox1.Text;
log.spass = TextBox3.Text;
log.reg_id = reg.reg_id;
ps.s_logs.InsertOnSubmit(log);
ps.SubmitChanges();
}
catch (Exception ex)
{
Response.Write("Error:" + ex.Message);
}
}
}
else
{
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "str", "alert(' please check user name and password ');", true);
}
}
catch (Exception ex)
{
Response.Write("Error:" + ex.Message);
}
}
}
}