Results 1 to 9 of 9

Thread: SQL Express not saving anything. Help!

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2006
    Posts
    63

    SQL Express not saving anything. Help!

    OK. I've asked a few questions here and have had a few problems along the way, but I think my problems might be due to visual basic, not my programming! I am using VB Express 2005 with a SQL 2005 database.

    At first, I tried programming the database as code. I can get the new rows to add to memory. When I use the update command, it returns a number equal to the number of rows I'm trying to add. It looks like everything is working fine, but the data is never added to the database.

    I can use the "database explorer" and add data manually and it works OK there.

    Next, I tried not writing any code and just creating a new database, attaching it as a datasource, and dragging it over to my form as a datagrid. Same thing. I can add files to the datagrid but nothing is ever saved back to the .mdf file.

    I've tried reinstalling VB and all SQL components but it hasn't helped. I don't know if there's some sharing violation that it can't write to the file? I've tried creating new apps and new databases. Are there other files I need to delete in addition to uninstalling the app? I don't know what else to try. Any help at all would be appreciated!

  2. #2
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: SQL Express not saving anything. Help!

    Ok. First post the code where the part of not saving your data is mentioned. I read you were updating something.
    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2006
    Posts
    63

    Re: SQL Express not saving anything. Help!

    Code:
    Dim myCon As SqlConnection = New SqlConnection(My.Settings.ConnString)
    Dim myDA As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM Rescue", myCon)
    Dim myDS As DataSet = New DataSet
    myDA.Fill(myDS, "Rescue")
    
    Dim myCB As SqlCommandBuilder = New SqlCommandBuilder(myDA)
    
    Dim myRow As DataRow = myDS.Tables("Rescue").NewRow
    myRow.Item("CallNum") = "33"
    myDS.Tables("Rescue").Rows.Add(myRow)
    myDA.Update(myDS, "Rescue")
    The connection string was auto-generated. It is:
    Code:
    Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\CallLog.mdf;Integrated Security=True;User Instance=True
    The last line of code with the update command returns a 1 meaning 1 row added successfully. I can put this code in a button and run it multiple times adding 1 row each time.

    Looking at myDS.Tables("Rescue").Rows.Count will show me the total rows I've added. If I try to view the data in "Database Explorer" it doesn't exist. Once I stop the execution and restart it, the data no longer exists.

    It would be helpful if somebody could paste the code in a button and create a database with 1 table "Rescue" and 1 field "CallNum" and tell me what happens.

    Greg
    Last edited by ghall426; Jan 8th, 2007 at 11:36 PM.

  4. #4
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: SQL Express not saving anything. Help!

    Try a
    "myDS.AcceptChanges"
    before updating.

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2006
    Posts
    63

    Re: SQL Express not saving anything. Help!

    Quote Originally Posted by sapator
    Try a "myDS.AcceptChanges" before updating.
    It still doesn't work. The Update command now returns 0.

    Greg

  6. #6
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: SQL Express not saving anything. Help!

    Another 1.
    Try updating without the table
    myDA.Update(myDS)
    also there is a parameter myda.endinit, try to use it before adding any row to the dataset.
    Sorry i got to go (it's 4 in the morning )

  7. #7

    Thread Starter
    Member
    Join Date
    Sep 2006
    Posts
    63

    Re: SQL Express not saving anything. Help!

    Quote Originally Posted by sapator
    Another 1.
    Try updating without the table
    myDA.Update(myDS)
    That generates an error:
    Update unable to find TableMapping['Table'] or DataTable 'Table'.

    So I need the table name.

    Can somebody please try executing my block of code to see if it's my computer or VB at fault or indeed a problem with the coding. Please?

    Greg

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2006
    Posts
    63

    Re: SQL Express not saving anything. Help!

    Ok. I solved my own problem. Now that I know what the problem is, I searched for it and found it to be a very common problem. Here's one thread:
    http://www.vbforums.com/showthread.p...=copy+if+newer

    Basically the database in the app directory gets copied to the bin/debug folder every time you run or debug the application. You need to change the "copy always" to "copy if newer".

    Also, if viewing the data in "database explorer", be sure to link up to the bin/debug temporary database. The one in your main application directory will never change.

    Greg

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Express not saving anything. Help!

    So this is SQL SERVER EXPRESS - not MS SQL SERVER 2005 - right?

    You should download the free management tool for SQL SERVER EXPRESS.

    It's the third download at this site:

    http://msdn.microsoft.com/vstudio/express/sql/

    That way you will be able to "view" the server operations - actually see yourself connected to a database - see what the properties are to that database.

    I'm guessing that you are attaching the database at runtime - as opposed to having the server be "always" attached to the database. Is there a reason you are doing it this way?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width