Link to home
Avatar of PITBULL72
PITBULL72Flag 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_;
        }
   
    }
}
Avatar of Anil Golamari
Anil Golamari
Flag of United States of America image

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.
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
Avatar of 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.
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
Avatar of Kaushal Arora
Kaushal Arora
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial