Author Topic: Programming  (Read 1446 times)

0 Members and 1 Guest are viewing this topic.

Offline Erik L (OP)

  • Administrator
  • Admiral of the Fleet
  • *****
  • Posts: 5657
  • Thanked: 372 times
  • Forum Admin
  • Discord Username: icehawke
  • 2020 Supporter 2020 Supporter : Donate for 2020
    2022 Supporter 2022 Supporter : Donate for 2022
    Gold Supporter Gold Supporter : Support the forums with a Gold subscription
    2021 Supporter 2021 Supporter : Donate for 2021
Programming
« on: October 23, 2007, 04:08:28 PM »
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: [Select]
       '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


iRow has a value of 1, which means the call returned 1 row like it should. It does not throw any exceptions.
« Last Edit: December 31, 1969, 06:00:00 PM by Erik Luken »
 

Offline TrueZuluwiz

  • Zulu
  • Warrant Officer, Class 1
  • *****
  • Posts: 97
(No subject)
« Reply #1 on: October 23, 2007, 05:52:30 PM »
Were you biting your lip while you wrote it? Sometimes that helps.
« Last Edit: December 31, 1969, 06:00:00 PM by TrueZuluwiz »
Expecting the Spanish Inquisition
 

Offline Erik L (OP)

  • Administrator
  • Admiral of the Fleet
  • *****
  • Posts: 5657
  • Thanked: 372 times
  • Forum Admin
  • Discord Username: icehawke
  • 2020 Supporter 2020 Supporter : Donate for 2020
    2022 Supporter 2022 Supporter : Donate for 2022
    Gold Supporter Gold Supporter : Support the forums with a Gold subscription
    2021 Supporter 2021 Supporter : Donate for 2021
(No subject)
« Reply #2 on: October 23, 2007, 06:39:32 PM »
I think I was cussing the computer when I was trying to debug it...
« Last Edit: December 31, 1969, 06:00:00 PM by Erik Luken »
 

Offline RoguePhoenix

  • Chief Petty Officer
  • ***
  • R
  • Posts: 30
  • Gold Supporter Gold Supporter : Support the forums with a Gold subscription
(No subject)
« Reply #3 on: October 23, 2007, 07:54:32 PM »
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
« Last Edit: December 31, 1969, 06:00:00 PM by RoguePhoenix »
Abject Destruction: Because a bored engineer is the second most dangerous person in the world ... right behind a bored politician.

No matter which way you step in complete darkness it is always towards the light.
 

Offline Randy

  • Sub-Lieutenant
  • ******
  • Posts: 146
  • Thanked: 1 times
(No subject)
« Reply #4 on: October 24, 2007, 12:14:29 AM »
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: [Select]
conn.open() before
Code: [Select]
Dim cmd As New SqlCommand(sCmdText, conn)

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...
« Last Edit: December 31, 1969, 06:00:00 PM by Randy »
 

Offline Erik L (OP)

  • Administrator
  • Admiral of the Fleet
  • *****
  • Posts: 5657
  • Thanked: 372 times
  • Forum Admin
  • Discord Username: icehawke
  • 2020 Supporter 2020 Supporter : Donate for 2020
    2022 Supporter 2022 Supporter : Donate for 2022
    Gold Supporter Gold Supporter : Support the forums with a Gold subscription
    2021 Supporter 2021 Supporter : Donate for 2021
(No subject)
« Reply #5 on: October 24, 2007, 08:06:33 AM »
Quote from: "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: [Select]
conn.open() before
Code: [Select]
Dim cmd As New SqlCommand(sCmdText, conn)
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...


Can't say I'd done much data access in 2003. This is a slightly modified version of the example they've got in MSDN. One would think it'd work.

Prior, I had the connection created in the app startup, and destroyed in the app shutdown.

I'd tried the simple SQL string too. That didn't work either. I am starting to think it is a SQL Express issue, but I'll be damned if I know what. The DBA here is confused as well.
« Last Edit: December 31, 1969, 06:00:00 PM by Erik Luken »