Results 1 to 7 of 7

Thread: [RESOLVED] Doing Query and Update for SQL Database

Threaded View

  1. #1

    Thread Starter
    Fanatic Member EntityX's Avatar
    Join Date
    Feb 2007
    Location
    Omnipresence
    Posts
    798

    Resolved [RESOLVED] Doing Query and Update for SQL Database

    Here's something I've been stuck on for some time. I used code from the ADO.NET Tutorial and tried to translate it so it applies to SQL and the database on my website. I set the Configuration Manager for Debug and so I can see a detailed error message.

    Here's my code:


    Code:
    Option Strict On
    Imports System.Web.AspNetHostingPermission
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Data.SqlTypes
    Imports System.Data.SqlDbType
    Imports System.Data.Sql
    Imports System.IO
    Imports System.Text
    
    Public Class Form1
        Dim intCurrentIndex As Integer = 0
        Dim conn As New SqlConnection()
        Dim ds As New DataSet()
        Dim da As New SqlDataAdapter()
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            conn.ConnectionString = "Network Library=DBMSSOCN;" & "Data Source=174.120.212.3,1433;" & "Initial Catalog=Database1;" & "User ID=myuserID;" & "Password=MyPassword"
            Try
                conn.Open()
                da.SelectCommand = New SqlCommand("SELECT ActivationID, EmailAddress FROM Ray Rover Activation")
                da.SelectCommand.Connection = conn
                MessageBox.Show("Connection Opened Successfully")
                DoStuff()
                conn.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            Finally
                conn.Dispose()
            End Try
        End Sub
    
        Sub DoStuff()
            Try
                da.UpdateCommand = New SqlCommand("UPDATE Ray Rover Activation SET EmailAddress = @EmailAddress WHERE ActivationID = @ActivationID")
    
                '    da.SelectCommand = New SqlCommand("SELECT ActivationID, EmailAddress FROM Ray Rover Activation")
                '    da.SelectCommand.Connection = conn
    
                '  da.UpdateCommand = New SqlCommand("UPDATE Ray Rover Activation SET EmailAddress = @EmailAddress WHERE ActivationID = @ActivationID")
                '  da.UpdateCommand.Connection = conn
                '  da.UpdateCommand.Parameters.Add("@EmailAddress", NVarChar, 100, "EmailAddress")
    
                '  da.InsertCommand = New SqlCommand("INSERT INTO Ray Rover Activation(EmailAddress) VALUES(@EmailAddress)")
                '   da.InsertCommand.Connection = conn
                '  da.InsertCommand.Parameters.Add("@EmailAddress", NVarChar, 100, "EmailAddress")
    
                '  da.DeleteCommand = New SqlCommand("DELETE FROM Ray Rover Activation WHERE ActivationID = @ActivationID")
                '  da.DeleteCommand.Connection = conn
                '  da.DeleteCommand.Parameters.Add("@EmailAddress", NVarChar, 100, "EmailAddress")
    
                da.Fill(ds)
    
                If ds.Tables(0).Rows.Count > 0 Then 'Check to see if the table is empty
                    FillFields()
                End If
            Catch ex As Exception
                MessageBox.Show(ex.ToString, "", MessageBoxButtons.OK, MessageBoxIcon.Warning) '
            End Try
        End Sub
    
        Private Sub FillFields()
            TextBoxEmailAddress.Text = ds.Tables(0).Rows(intCurrentIndex).Item("EmailAddress").ToString()
        End Sub
    I have most of the lines in the DoStuff Sub commented out right now but I get the same error message either way so it's responding to the first line below Try.

    Here's the error message I see. Line 52 is :
    da.Fill(ds)

    The table in Database1 that I'm trying to access is called Ray Rover Activation.
    Attached Images Attached Images  
    Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.

    "Persistence is the magic of success." Paramahansa Yogananda

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