Off Topic > Off Topic

Programming

(1/2) > >>

Erik L:
Anyone do anything in .NET 2005?

Got a snippet of code that is supposed to save to a database. It doesn't. I'll be damned if I can figure it out. Written in VB.

--- Code: ---        'save to the database
        conn = New SqlConnection
        conn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\cyberhack.mdf;Integrated Security=True;User Instance=True"

        Dim sCmdText As String = "INSERT INTO Game ([GameName], [GameDate], [GameTime]) VALUES (@GameName, @Date, @Time)"
        Dim cmd As New SqlCommand(sCmdText, conn)
        Dim iRow As Integer

        cmd.Parameters.Add("@GameName", SqlDbType.VarChar, 50)
        cmd.Parameters.Add("@Date", SqlDbType.DateTime)
        cmd.Parameters.Add("@Time", SqlDbType.DateTime)
        cmd.Parameters("@GameName").Value = txtName.Text
        cmd.Parameters("@Date").Value = "01/01/2068"
        cmd.Parameters("@Time").Value = "12:00"

        MsgBox(conn.ConnectionString)

        'cmd.CommandText = "cyberhack.dbo.GameInsert @GameName, @Date, @Time"
        cmd.CommandType = CommandType.Text
        Try
            cmd.Connection = conn
            conn.Open()
            cmd.Prepare()
            iRow = cmd.ExecuteNonQuery
            MsgBox(iRow)
            conn.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

--- End code ---


iRow has a value of 1, which means the call returned 1 row like it should. It does not throw any exceptions.

TrueZuluwiz:
Were you biting your lip while you wrote it? Sometimes that helps.

Erik L:
I think I was cussing the computer when I was trying to debug it...

RoguePhoenix:
A nice big rubber mallet is usually helpful. We keep one in our cube for the parts database management server. It usually works pretty well too!

And if we're good lil boys and santa is ever so nice this year we might even be able to office space it when the new system comes in MWUHAHAHHAHA

Randy:
Not sure if it makes a difference, but I usually have the connection opened before trying to create a comand from it...

ie do

--- Code: ---conn.open()
--- End code ---
before

--- Code: ---Dim cmd As New SqlCommand(sCmdText, conn)
--- End code ---


But this assumes that your connect string is working. Are you able to perform a select using that connect string?

I generally keep the connection open (unless doing asp.net) as it works a little faster using connection pooling.

I also tend to assemple the sql string rather than using parameters (they get real annoying sometimes


Then again, I'm only using .net 2003 and Access...

Navigation

[0] Message Index

[#] Next page

It appears that you have not registered with Aurora 4x. To register, please click here...
There was an error while thanking
Thanking...
Go to full version