Avatar of PITBULL72
PITBULL72
Flag for United States of America asked on

Update using c# and LINQ to SQL

Hello, been trying to update my table using LINQ to SQL via C#. I'm able to insert new records, but I cant edit them, plus i'm having issues again with the date and time.

My code is below.

Thanks

P

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Collections;


public partial class Search : System.Web.UI.Page
{
    LDDataClassesDataContext db = new LDDataClassesDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.Visible = true;
        getdata();
    }
   
    protected void BtnSrch_Click(object sender, EventArgs e)
    {
       
    }
    protected void BtnReset_Click(object sender, EventArgs e)
    {
        GridView1.Visible = false;
        GridView1.DataBind();
        DtRcvTxt.Text = null;
        RqAgy.SelectedIndex = -1;
        RnkReqAgy.SelectedIndex = -1;
        LnamReqAgy.Text = null;
        LdTxtbx.Text = null;
        LrTxtbx.Text = null;
        CmplntTxt.Text = null;
        CmntsTxt.Text = null;
        CoDDL.SelectedIndex = -1;
        SbmtTxt.Text = null;
        Cse.Text = null;
        LgTxt.Text = null;
        DtReqFx.Text = null;
        GrpDDL.SelectedIndex = -1;
        RnkRecReqDDL.SelectedIndex = -1;
        LnamRecAgy.Text = null;
        FivDDL.SelectedIndex = -1;
        OGtxt.Text = null;
     }

    protected void BtnUpdt_Click(object sender, EventArgs e)
    {
        var qry = from m in db.LDRFs
                  where m.ID == Convert.ToInt32(Request.QueryString["ID"])
                  select m;
        foreach (var q in qry)
        {
            q.LnamRec = LnamRecAgy.Text;
            q.LglReq = LgTxt.Text;
            //q.DtAgyRqRec = DateTime.TryParse(DtRcvTxt.Text, out dt) ? dt : (DateTime?)null;
            //q.SubmtBy = DateTime.TryParse(SbmtTxt.Text, out dt) ? dt : (DateTime?)null;
            //q.DtReqFx = DateTime.TryParse(DtReqFx.Text, out dt) ? dt : (DateTime?)null;
            q.ReqAgy = RqAgy.SelectedItem.Text;
            q.RnkReq = RnkReqAgy.SelectedItem.Text;
            q.LnamReq = LnamReqAgy.Text;
            q.LwDpt = LdTxtbx.Text;
            q.LglReq = LrTxtbx.Text;
            q.Cmplnt = CmplntTxt.Text;
            q.Cmnt = CmntsTxt.Text;
            q.CrtOrdr = CoDDL.SelectedItem.Text;
            q.Case_ = Cse.Text;
            q.Log_ = LgTxt.Text;
            q.Grp_ = GrpDDL.SelectedItem.Text;
            q.RnkRec = RnkRecReqDDL.SelectedItem.Text;
            q.LnamRec = LnamRecAgy.Text;
            q.Finv = FivDDL.SelectedItem.Text;
            q.OG_ = OGtxt.Text;
            Response.Redirect("Update.aspx");
        }
        db.SubmitChanges();
    }
    void getdata()
    {
        var qry = from m in db.LDRFs
                  where m.ID == Convert.ToInt32(Request.QueryString["ID"])
                  select m;
        foreach (var q in qry)
        {
            LnamRecAgy.Text=q.LnamRec;
            LgTxt.Text = q.LglReq;
            //q.DtAgyRqRec = DateTime.TryParse(DtRcvTxt.Text, out dt) ? dt : (DateTime?)null;
            //q.SubmtBy = DateTime.TryParse(SbmtTxt.Text, out dt) ? dt : (DateTime?)null;
            //q.DtReqFx = DateTime.TryParse(DtReqFx.Text, out dt) ? dt : (DateTime?)null;
            RqAgy.SelectedItem.Text=q.ReqAgy;
            RnkReqAgy.SelectedItem.Text=q.RnkReq;
            LnamReqAgy.Text=q.LnamReq;
            LdTxtbx.Text=q.LwDpt;
            LrTxtbx.Text=q.LglReq;
            CmplntTxt.Text=q.Cmplnt;
            CmntsTxt.Text=q.Cmnt;
            CoDDL.SelectedItem.Text=q.CrtOrdr ;
            Cse.Text=q.Case_;
            LgTxt.Text=q.Log_;
            GrpDDL.SelectedItem.Text=q.Grp_;
            RnkRecReqDDL.SelectedItem.Text=q.RnkRec;
            LnamRecAgy.Text=q.LnamRec;
            FivDDL.SelectedItem.Text=q.Finv;
            OGtxt.Text=q.OG_;
        }
   
    }
}
C#ASP.NET.NET Programming

Avatar of undefined
Last Comment
Kaushal Arora

8/22/2022 - Mon
Anil Golamari

http://www.codeproject.com/Articles/26657/Simple-LINQ-to-SQL-in-C

Can you tell where exactly are you getting an issue because you commented out all datetime variables.
Anuradha Goli

Can you check whether the datatype of the 3 lines is datetime

//q.DtAgyRqRec = DateTime.TryParse(DtRcvTxt.Text, out dt) ? dt : (DateTime?)null;
 //q.SubmtBy = DateTime.TryParse(SbmtTxt.Text, out dt) ? dt : (DateTime?)null;
//q.DtReqFx = DateTime.TryParse(DtReqFx.Text, out dt) ? dt : (DateTime?)null;

instead of setting to null try making it to min value
PITBULL72

ASKER
I The three lines are date time. I commented them out becasue I was also having issues with trying to get them to update. I was trying to tackle one problem at a time, but if I do remove the slash marks They are underlined inred. But my other problem is that I cannot update my table through my code, my changes won't stick.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
PITBULL72

ASKER
I think I fixed my date time issue, by adding datetime dt; I left that out by mistake. However I'm unable to update my table.
ASKER CERTIFIED SOLUTION
Kaushal Arora

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question