none
linq insert statements... RRS feed

  • Question

  • Im trying to create a program that tracks cards, basically you insert the player name, brand, year, and condition, along with the image and the linq statements i've tried haven't been working... I can query the database just fine, but if I want to insert a new row (basically saying a new card), the code shows no errors but after running and testing, the database doesn't show the row I added.....


    private void SAVEbtn_Click(object sender, EventArgs e)
    {
    CardsSQLtoLinqDataContext db = new CardsSQLtoLinqDataContext();

    Card car = new Card
    {
    Player = textBox1.Text,
    Brand = textBox2.Text,
    Year = textBox3.Text,
    Condition = comboBox1.SelectedText

    };

    label1.Text = textBox1.Text; //
    label2.Text = textBox2.Text; //
    label3.Text = textBox3.Text; //these are for testing purposes...


    db.Cards.InsertOnSubmit(car);
    db.SubmitChanges();

    }

    what am I doing wrong?

    I've also tried this inside the click event...

    CardsSQLtoLinqDataContext db = new CardsSQLtoLinqDataContext();
    Card cs = new Card();

    cs.Player = textBox1.Text;
    cs.Brand = textBox2.Text;
    cs.Year = textBox3.Text;
    cs.Condition = comboBox1.SelectedText;

    db.Cards.InsertOnSubmit(cs);
    db.SubmitChanges();

    Im using Visual C# Express 2008.... I'm just starting LINQ statements
    Tuesday, June 9, 2009 3:58 PM

Answers

All replies

  • Questions:
    1. Does the card table in the DB have a primary key ID which auto increments?
    2. Are there any foreign key realtionships you haven't mentioned which might need to be done before the card can be inserted?

    William Wegerson (www.OmegaCoder.Com)
    Tuesday, June 9, 2009 7:07 PM
    Moderator
  • There is a CardID that is an identity field

    (Is Identity) - Yes
    Identity Increment - 1
    Identity Seed - 1

    There is only one table. I have a linq to sql class called "CardsSQLtoLinq.dbml"(Which ive updated through the design view plenty of times)

    and the database is a sql database file on my local drive (CardsDB.mdf).
    Tuesday, June 9, 2009 7:16 PM
  • Can you pull data from the database when you use:

    CardsSQLtoLinqDataContext db = new CardsSQLtoLinqDataContext();

    ??
    William Wegerson (www.OmegaCoder.Com)
    Tuesday, June 9, 2009 7:30 PM
    Moderator
  •  

    var

     

    cardq = from o in db.Cards

     

     

    select o;


    cardq.(Intellisense shows no columns in the table)



    BUT


     

    var cardq = from o in db.Cards

     

    select new{

    o.CardID,
    o.Brand,
    };

    works..

    Tuesday, June 9, 2009 7:38 PM
  • I have manually inserted things into the database and queried items from it while running....

    Tuesday, June 9, 2009 7:51 PM
  • The access to the database used by the context, does that have write permissions?

    Has the table changed since you setup the dbml context? Maybe delete the table and re-add it.

    William Wegerson (www.OmegaCoder.Com )
    Tuesday, June 9, 2009 8:00 PM
    Moderator
  • I have deleted and re-added the table to the dbml file, how do i check if the data context has write permissions?
    Tuesday, June 9, 2009 8:21 PM
  • If your talking about the Access field when I'm highlighting the table Data Class in the designer view of the dbml, It is Public.

    Tuesday, June 9, 2009 8:27 PM
  • I have deleted and re-added the table to the dbml file, how do i check if the data context has write permissions?

    How are you logging on to the db? Could the account not have write privlidges? (Off chance but have to ask)
    William Wegerson (www.OmegaCoder.Com)
    Wednesday, June 10, 2009 1:04 AM
    Moderator
  • windows authenication.

    I created the db, and its not hosted on a server, its a database file that resides in the same folder as the project/solution. So I should have write priv.
    Wednesday, June 10, 2009 10:46 AM
  • Is this the only project it is happening in?

    William Wegerson (www.OmegaCoder.Com)
    Wednesday, June 10, 2009 3:00 PM
    Moderator
  • Well, this is the only project I have have open at the moment...

    I have no other projects. But I could try to make another project and test.

    I dont think this is matters, but Im running Visual C# Express 2008 over Remote Desktop on another computer (which I have complete access to), so nothing is written on MY computer but the computer Im accessing.

    Wednesday, June 10, 2009 3:04 PM
  • I tried it strictly on my computer. One thing I've noticed is that the database connection icon will go from the plug (connected) to the X (disconnected) on the Database explorer whenever I run the program.... I also had to add a database by rightclicking the solution and Add... New... Service-based Database, instead of the usual way I create a database, which is strictly through the database explorer (right click and "Create New SQL Server Database"

    But I still cannot write to the database.
    Thursday, June 11, 2009 1:15 PM
  • I'm guessing .mdf isn't fully supported for express editions....

    All my context is right (supposedly), but the database just isnt being written.

    I've even tried a datagrid view with a dataset bound, and enabled insert/adding and it still does not save.

    Thursday, June 11, 2009 1:50 PM
  • See this thread and let us know if it helps: http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/df93ab2a-67c3-4fc2-965d-0a42387f97a8
    Blog - http://blogs.rev-net.com/ddewinter/ Twitter - @ddewinter
    • Marked as answer by Mikeman22886 Tuesday, June 23, 2009 1:10 PM
    Thursday, June 11, 2009 3:56 PM
    Answerer
  • that makes sense, but how should do I view the contents of the database in the release folder? do I add it as a connection?
    Thursday, June 11, 2009 4:56 PM
  • Thats why our database values were updated as soon as we exit the program in school where we use a dedicated SQL server. How am I supposed to test a program that uses a service-based database(.mdf) if I don't have the database on the server, but rather have in inside my solution? I get that i can change the copy output directory, but how would I view it?
    Friday, June 12, 2009 9:35 PM