Results 1 to 17 of 17

Thread: [2005] cannot save to database

  1. #1

    Thread Starter
    Hyperactive Member jonwondering's Avatar
    Join Date
    May 2005
    Posts
    311

    [2005] cannot save to database

    hey, still trying to figure out this database stuff, and now i have a new problem... i connect to the db successfully, then i have the following code, then i close the connection:

    VB Code:
    1. Dim a As New SqlCommand("INSERT INTO Table1 VALUES (1, 'hey')", conn)
    2.         a.ExecuteReader()

    i even check how many records were entered in another button, and it says 1, but when i exit the program, and check again, it's not there... what am i missing?

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] cannot save to database

    try

    a.executenonquery

    executereader is when you are doing a select statement and are returning records into a datareader object..

  3. #3

    Thread Starter
    Hyperactive Member jonwondering's Avatar
    Join Date
    May 2005
    Posts
    311

    Re: [2005] cannot save to database

    i actually had executenonquery first, and then i changed it to executereader...

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] cannot save to database

    executenonquery returns an integer value based on the number of records affected.

    so what value is it returning when you run it?

  5. #5

    Thread Starter
    Hyperactive Member jonwondering's Avatar
    Join Date
    May 2005
    Posts
    311

    Re: [2005] cannot save to database

    returns value one (1)

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] cannot save to database

    but you are saying if you go and check the database, the record is not there??

  7. #7

    Thread Starter
    Hyperactive Member jonwondering's Avatar
    Join Date
    May 2005
    Posts
    311

    Re: [2005] cannot save to database

    ok i have two buttons - button1 puts in the row (or at least supposed to), button2 shows how many rows are in db. i click button1, then i click button2 and it msgbox's me saying there is 1 row. then i exit app, and start it up again. i click button2 and it msgbox's "0".

    btw is there any other way of browsing the db besides right-clicking on Table1 under Data Sources and going to Preview Data?

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] cannot save to database

    You would need a program like Enterprise Manager (I dont think that come with SQL Express if thats what you are using)

    There may be other ways.. but I always use Enterprise Manager (sometimes query analyzer)... these are standard programs that come with SQL Server

    what does the code look like in button2??

  9. #9

    Thread Starter
    Hyperactive Member jonwondering's Avatar
    Join Date
    May 2005
    Posts
    311

    Re: [2005] cannot save to database

    VB Code:
    1. conn.Open()
    2.  
    3.         Dim m_daStudents As New SqlDataAdapter("SELECT * FROM Table1", conn)
    4.  
    5.         Dim m_DataSet As New DataSet()
    6.  
    7.         m_daStudents.TableMappings.Add("Table", "Table1")
    8.         m_daStudents.Fill(m_DataSet)
    9.  
    10.         MsgBox(m_DataSet.Tables("Table1").Rows.Count)
    11.         conn.Close()

    there's probably an easier way of checking, but i am not there yet... do i even have to put it in dataset first?!

  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] cannot save to database

    if you just wanted a count, then no, you could execute a simple scalar query... which returns just a single value.. something like this...

    VB Code:
    1. Dim cmd As New SqlClient.SqlCommand("SELECT COUNT(*) FROM table1", conn)
    2.         Dim NumOfRecs As Integer
    3.         NumOfRecs = Convert.ToInt32(cmd.ExecuteScalar)
    4.         Messagebox.Show(NumOfRecs.ToString)
    5.         conn.Close()

  11. #11

    Thread Starter
    Hyperactive Member jonwondering's Avatar
    Join Date
    May 2005
    Posts
    311

    Re: [2005] cannot save to database

    cool, but it still displays 0, any ideas?

  12. #12
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] cannot save to database

    well what happens when you do view the data in the data sources window in the IDE?

    are there records in there?

  13. #13

    Thread Starter
    Hyperactive Member jonwondering's Avatar
    Join Date
    May 2005
    Posts
    311

    Re: [2005] cannot save to database

    well, it gives me a nice error message: "An attempt to attach an auto-named database for file C:\Very Long Path\MyDatabase.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."

    ... at this point I am starting to like XML more and more ...

    i searched the error message, but didn't find much useful info.

  14. #14
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] cannot save to database

    what database are you using?

  15. #15

    Thread Starter
    Hyperactive Member jonwondering's Avatar
    Join Date
    May 2005
    Posts
    311

    Re: [2005] cannot save to database

    as in the type or the actual file name? if you mean the type then SQL Server

  16. #16
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] cannot save to database

    yeah I meant type but I also meant version.. sorry Im just trying to collect as much info to help you so i dont run you around in circles

  17. #17

    Thread Starter
    Hyperactive Member jonwondering's Avatar
    Join Date
    May 2005
    Posts
    311

    Re: [2005] cannot save to database

    hey i appreciate all the help i can get... i downloaded latest version SQL Express from microsoft's website

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