none
writing to sql database using LINQ RRS feed

  • Question

  • Hi,

    i have the following code to write to a database table:

     

    battleshipsDBDataContext db = new battleshipsDBDataContext();

     

    user newUser = new user();

    newUser.userName = txtUser.Text;

    newUser.firstName = txtFirst.Text;

    newUser.lastName += txtSurname.Text;

    newUser.eMail = txtEmail.Text;

    db.users.InsertOnSubmit(newUser);

    db.SubmitChanges();


    when i run the code it appears to save the data to temorary memory as when i click another button it retrieves the inputted data. however when ifinish debugging and look in my database table, the data is not there.

    would be very grateful for any suggestions

    thank you

    Thursday, May 7, 2009 3:26 PM

Answers

  • thanx, the problem is sorted now, all i had to do was put the Db con string between the barackets of this line

    battleshipsDBDataContext db = new battleshipsDBDataContext();

    only took me a week to sort it out ho hum!
    • Marked as answer by bauer74 Monday, May 11, 2009 6:12 PM
    Monday, May 11, 2009 6:12 PM

All replies

  • Could you post the code for user?
    David Morton - http://blog.davemorton.net/
    Thursday, May 7, 2009 3:38 PM
  • i dont have any code for user, user is the name of the table i am writing to.

    i am extremely new to this, and not 100% sure what im doing, i have followed a few books but am really stuck.

    when i right click on the user in the battleshipsDB.dbml diagram and click view coded this appears:

    namespace

     

    Battleships

    {

     

    partial class battleshipsDBDataContext

    {

    }

    }

    Thursday, May 7, 2009 4:19 PM
  • i dont have any code for user, user is the name of the table i am writing to.
    Sure you do.  Put your cursor over the word "user" and press F12.

    David Morton - http://blog.davemorton.net/
    Thursday, May 7, 2009 4:20 PM
  • ok i got his code:

     

    public user()

    {

    OnCreated();

    }

     

    [

    Column(Storage="_userName", DbType="VarChar(15) NOT NULL", CanBeNull=false, IsPrimaryKey=true)]

     

    public string userName

    {

     

    get

    {

     

    return this._userName;

    }

     

    set

    {

     

    if ((this._userName != value))

    {

     

    this.OnuserNameChanging(value);

     

    this.SendPropertyChanging();

     

    this._userName = value;

     

    this.SendPropertyChanged("userName");

     

    this.OnuserNameChanged();

    }

    }

    }

     

    [

    Column(Storage="_firstName", DbType="VarChar(25) NOT NULL", CanBeNull=false)]

     

    public string firstName

    {

     

    get

    {

     

    return this._firstName;

    }

     

    set

    {

     

    if ((this._firstName != value))

    {

     

    this.OnfirstNameChanging(value);

     

    this.SendPropertyChanging();

     

    this._firstName = value;

     

    this.SendPropertyChanged("firstName");

     

    this.OnfirstNameChanged();

    }

    }

    }

     

    [

    Column(Storage="_lastName", DbType="VarChar(25) NOT NULL", CanBeNull=false)]

     

    public string lastName

    {

     

    get

    {

     

    return this._lastName;

    }

     

    set

    {

     

    if ((this._lastName != value))

    {

     

    this.OnlastNameChanging(value);

     

    this.SendPropertyChanging();

     

    this._lastName = value;

     

    this.SendPropertyChanged("lastName");

     

    this.OnlastNameChanged();

    }

    }

    }

     

    [

    Column(Storage="_eMail", DbType="VarChar(50) NOT NULL", CanBeNull=false)]

     

    public string eMail

    {

     

    get

    {

     

    return this._eMail;

    }

     

    set

    {

     

    if ((this._eMail != value))

    {

     

    this.OneMailChanging(value);

     

    this.SendPropertyChanging();

     

    this._eMail = value;

     

    this.SendPropertyChanged("eMail");

     

    this.OneMailChanged();

    }

    }

    }

     

    [

    Column(Storage="_password", DbType="VarChar(15) NOT NULL", CanBeNull=false)]

     

    public string password

    {

     

    get

    {

     

    return this._password;

    }

     

    set

    {

     

    if ((this._password != value))

    {

     

    this.OnpasswordChanging(value);

     

    this.SendPropertyChanging();

     

    this._password = value;

     

    this.SendPropertyChanged("password");

     

    this.OnpasswordChanged();

    }

    }

    }

     

     

    public event PropertyChangingEventHandler PropertyChanging;

     

     

    public event PropertyChangedEventHandler PropertyChanged;

     

     

    protected virtual void SendPropertyChanging()

    {

     

    if ((this.PropertyChanging != null))

    {

     

    this.PropertyChanging(this, emptyChangingEventArgs);

    }

    }

     

     

    protected virtual void SendPropertyChanged(String propertyName)

    {

     

    if ((this.PropertyChanged != null))

    {

     

    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

    }

    }

    }

    }

    #pragma

     

    warning restore 1591

    Thursday, May 7, 2009 5:05 PM
  • Hi, try to use storedprocedure to better and faster. It's more realiable and easy to use the StoreProcedure using linq to sql.
    Hope This One Can Help.
    Thanks
    To Be Happy Is To Be YourSelf
    Please remember to mark my reply as Proposed as Answers if it's the answer to your question.
    Friday, May 8, 2009 1:06 AM
  • Are you using SQL Express with an SDF file in your project?

    [)amien
    Friday, May 8, 2009 2:48 AM
    Moderator
  • yes SQL Express 2008. Im not sure if im using an SDF file.

    I have tried using a stored procedure with no success.

    I just want to say that this is actually thew first time im using LINQ i am doing this for my degree project. Even my tutors have no experience with LINQ so i'm completely in the dark with this. :-(
    Saturday, May 9, 2009 9:16 AM
  • i know something is working because when i send the data to the database, it seems to work, then i click the button again, i get a primary key violation saying you cannot have a duplicate entry which suggests that the data is written. but when i look in the tabel nothing is there. when i start the program again the same happens - first click, it works, second click  - primary key violation. so im guessing the data is stored in some temporary memory
    Saturday, May 9, 2009 9:22 AM
  • bauer, is this helpful? http://blogs.msdn.com/sqlexpress/archive/2006/07/17/668971.aspx
    Sunday, May 10, 2009 5:27 PM
    Answerer
  • thanx, the problem is sorted now, all i had to do was put the Db con string between the barackets of this line

    battleshipsDBDataContext db = new battleshipsDBDataContext();

    only took me a week to sort it out ho hum!
    • Marked as answer by bauer74 Monday, May 11, 2009 6:12 PM
    Monday, May 11, 2009 6:12 PM