[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:
Dim a As New SqlCommand("INSERT INTO Table1 VALUES (1, 'hey')", conn)
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?
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..
Re: [2005] cannot save to database
i actually had executenonquery first, and then i changed it to executereader...
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?
Re: [2005] cannot save to database
Re: [2005] cannot save to database
but you are saying if you go and check the database, the record is not there??
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?
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??
Re: [2005] cannot save to database
VB Code:
conn.Open()
Dim m_daStudents As New SqlDataAdapter("SELECT * FROM Table1", conn)
Dim m_DataSet As New DataSet()
m_daStudents.TableMappings.Add("Table", "Table1")
m_daStudents.Fill(m_DataSet)
MsgBox(m_DataSet.Tables("Table1").Rows.Count)
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?! :ehh:
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:
Dim cmd As New SqlClient.SqlCommand("SELECT COUNT(*) FROM table1", conn)
Dim NumOfRecs As Integer
NumOfRecs = Convert.ToInt32(cmd.ExecuteScalar)
Messagebox.Show(NumOfRecs.ToString)
conn.Close()
Re: [2005] cannot save to database
cool, but it still displays 0, any ideas? :(
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?
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.
Re: [2005] cannot save to database
what database are you using?
Re: [2005] cannot save to database
as in the type or the actual file name? if you mean the type then SQL Server
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 :)
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