LINQ textbox update to DB problem

If I read the DB field first, then it won't get updated

Page 1 of 1

3 Replies - 2964 Views - Last Post: 28 April 2009 - 11:25 AM Rate Topic: -----

#1 Elven   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 13-August 08

LINQ textbox update to DB problem

Post icon  Posted 26 April 2009 - 10:04 AM

Hi guys,

I have a problem with LINQ at the moment. Everything is working fine, except for one thing.

Let me explain my situation. I have a table called "Motori" and in there ID.
I want to read from "Motori" where ID equals QueryString and display the name of that motor in my textbox.

That all works fine.

I also want to be able to change the name of the motor by changing what is in the textbox... now that does NOT work... well actually it DOES if there isn't first part.

Let me show you the code which reads the DB and writes in the textbox (it is located in the Page_load)

int Red = Convert.ToInt32(Request.QueryString["Row"]);
		 var motori = from p in kc.Motoris
					  where p.ID_motora == Red
					 select p;
			   foreach (Motori p in motori)
			   Name.Text = p.Info;



And this is the part for inserting textbox text into the database:

 protected void Red_Click(object sender, EventArgs e)
	{
		int Red = Convert.ToInt32(Request.QueryString["Row"]);

		var update = from p in kc.Motoris
					 where p.ID_motora == Red
					 select p;

		foreach (Motori p in update)
		   p.Info = HttpUtility.HtmlEncode(Opis.Value);
	   
		
		kc.SubmitChanges();
		
		GridView1.DataSource = update;
		GridView1.DataBind();


Writing to the DB works if reading from it is commented, and it works if it is reading and only binding to a GridView. But if I read the DB and write it in the textbox, then changing that same textbox won't write new text into the database.

As you can figure out, I am working on a small, compact CMS here, and it all worked fine in ASP.Net 2.0, but I want to make it work with LINQ now.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: LINQ textbox update to DB problem

#2 Elven   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 13-August 08

Re: LINQ textbox update to DB problem

Posted 27 April 2009 - 03:31 AM

Stupid me... the problem was good old "Page.IsPostback", so my textbox was constatly reset and kept updating the same value, regardless of what I type inside
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: LINQ textbox update to DB problem

Posted 28 April 2009 - 04:01 AM

instead of the "foreach" to loop through the one result of the query, you can use...

Name.Text = p.First().Info;


Was This Post Helpful? 1
  • +
  • -

#5 Elven   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 13-August 08

Re: LINQ textbox update to DB problem

Posted 28 April 2009 - 11:25 AM

View Posteclipsed4utoo, on 28 Apr, 2009 - 03:01 AM, said:

instead of the "foreach" to loop through the one result of the query, you can use...

Name.Text = p.First().Info;



Thanks for that! I was thinking about a better way when there is only one result, and I secretly hope that someone would point me out in the right direction as well. Kudos for you mate :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1