Update using c# and LINQ to SQL

PITBULL72
PITBULL72 used Ask the Experts™
on
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_;
        }
   
    }
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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 GoliSystems Development / Support Specialist

Commented:
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

Author

Commented:
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.

Author

Commented:
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.
Technical Analyst
Commented:
The issue is in Page Load Event. You have not used Page.IsPostBack Property while filling the Gridview Control. Please change your Page_Load Event Code to this.

protected void Page_Load(object sender, EventArgs e)
{
     GridView1.Visible = true;

     if(!(Page.IsPostBack))
     {
          getdata();
     }
}

Open in new window


Hope this helps you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial