14 Replies - 2021 Views - Last Post: 06 November 2007 - 05:49 AM Rate Topic: -----

#1 jbeannie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 03-October 07

I need help with some code error's.

Posted 05 November 2007 - 02:42 PM

'Imports Data and SqlClient namespaces...
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1

Dim objCOnnection As New SqlConnection _
("server=bnewsome;database=pubs;user id=sa;password=!p@ssw0rd?")

Dim objDataAdapter As New SqlDataAdapter()
Dim objDataSet As New DataSet()

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Load

'Set the SelectCommand properties...
objDataAdapter.SelectCommand = New SqlCommand()
objDataAdapter.SelectCommand.Connection = objCOnnection
objDataAdapter.SelectCommand.CommandText = _
"SELECT au_1name, au_fname, title, price " & _
"FROM authors " & _
"JOIN titleauthor ON authors.au_id = titleauthor.au_id " & _
"JOIN titles ON titleauthor.title_id = titles.title_id " & _
"ORDER BY au_1name, au_fname"
objDataAdapter.SelectCommand.CommandType = CommandType.Text

'Open the database connection...
objCOnnection.Open()

'Fill the DataSet object with data...
objDataAdapter.Fill(objDataSet, "authors")

'Close the database connection...
objCOnnection.Close()

'Set the DataGridView properties to bind it to our data...
grdAuthorTitles.AutoGenerateColumns = True
grdAuthorTitles.DataSource = objDataSet
grdAuthorTitles.DataMember = "authors"

'Clean up"
objDataAdapter = Nothing
objCOnnection = Nothing

End Sub
End Class



Error 1 Declaration expected. 2 5

Error 2 Declaration expected. 3 5

Error 3 Type 'SqlConnection' is not defined. 7 30

Error 4 Type 'SqlDataAdapter' is not defined. 10 31

Error 5 Type 'SqlCommand' is not defined. 17 44

How do I fix these error's?

Please help me out with this, I'm kind of a noob when it comes to .Net.

This post has been edited by jbeannie: 05 November 2007 - 03:26 PM


Is This A Good Question/Topic? 0
  • +

Replies To: I need help with some code error's.

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: I need help with some code error's.

Posted 05 November 2007 - 02:47 PM

I take it you didn't write this code? If you had then you'd probably know the answer to those questions :)
Was This Post Helpful? 0
  • +
  • -

#3 jbeannie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 03-October 07

Re: I need help with some code error's.

Posted 05 November 2007 - 03:10 PM

Am I supposed to self-teach myself, code?

Does anyone write code, without knowing any of it?

Everybody has to start somewhere. Right?
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: I need help with some code error's.

Posted 05 November 2007 - 03:24 PM

I wasn't criticizing you, just trying to verify if this was code you wrote, or some you got online or somewhere else

Okay, right now you have the following outside any function of sub procedure:

Dim objCOnnection As New SqlConnection ("server=bnewsome;database=pubs;user id=sa;password=!p@ssw0rd?")

Dim objDataAdapter As New SqlDataAdapter()
Dim objDataSet As New DataSet()



In order to use these, if you declare then outside your method, is to make them Global, to do that, change the Dim to Private like so:

Private objCOnnection As New SqlConnection _
("server=bnewsome;database=pubs;user id=sa;password=!p@ssw0rd?")

Private objDataAdapter As New SqlDataAdapter()
Private objDataSet As New DataSet()



That should be the only change you need to use the code as it sits right now :)
Was This Post Helpful? 0
  • +
  • -

#5 jbeannie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 03-October 07

Re: I need help with some code error's.

Posted 05 November 2007 - 03:28 PM

Thanks PsychoCoder

You definitely helped me out by changing the dim to Private.

Now I modified the code above, and got a few error's.

Public Class Form1

	'Imports Data and SqlClient namespaces...
Imports System.Data
Imports System.Data.SqlClient

	Private objCOnnection As New SqlConnection _
		("server=bnewsome;database=pubs;user id=sa;password=!p@ssw0rd?")

	Private objDataAdapter As New SqlDataAdapter()
	Private objDataSet As New DataSet()


	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'TODO: This line of code loads data into the 'Xtreme2002DataSet.Customer' table. You can move, or remove it, as needed.
		Me.CustomerTableAdapter.Fill(Me.Xtreme2002DataSet.Customer)

		'Set the SelectCommand properties...
		objDataAdapter.SelectCommand = New SqlCommand()
		objDataAdapter.SelectCommand.Connection = objCOnnection
		objDataAdapter.SelectCommand.CommandText = _
		"SELECT au_1name, au_fname, title, price " & _
		"FROM authors " & _
		"JOIN titleauthor ON authors.au_id = titleauthor.au_id " & _
		"JOIN titles ON titleauthor.title_id = titles.title_id " & _
		"ORDER BY au_1name, au_fname"
		objDataAdapter.SelectCommand.CommandType = CommandType.Text

		'Open the database connection...
		objCOnnection.Open()

		'Fill the DataSet object with data...
		objDataAdapter.Fill(objDataSet, "authors")

		'Close the database connection...
		objCOnnection.Close()

		'Set the DataGridView properties to bind it to our data...
		grdAuthorTitles.AutoGenerateColumns = True
		grdAuthorTitles.DataSource = objDataSet
		grdAuthorTitles.DataMember = "authors"

		'Clean up"
		objDataAdapter = Nothing
		objCOnnection = Nothing


	End Sub
End Class



Error 1 'Imports' statements must precede any declarations. 4 1

Error 2 'Imports' statements must precede any declarations. 5 1

Error 3 Type 'SqlConnection' is not defined. 7 34

Error 4 Type 'SqlDataAdapter' is not defined. 10 35

Error 5 Type 'SqlCommand' is not defined. 19 44

Error 6 Name 'grdAuthorTitles' is not declared. 39 9

Error 7 Name 'grdAuthorTitles' is not declared. 40 9

Error 8 Name 'grdAuthorTitles' is not declared. 41 9

I'm wondering why I got these error's. :huh:

I'll try to figure it out. I'm not really good at figuring out stuff in .NET. :ph34r:

This post has been edited by jayman9: 05 November 2007 - 05:27 PM

Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: I need help with some code error's.

Posted 05 November 2007 - 05:28 PM

The Import statements belong before the class declaration.
'Imports Data and SqlClient namespaces...
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1

Was This Post Helpful? 0
  • +
  • -

#7 jbeannie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 03-October 07

Re: I need help with some code error's.

Posted 05 November 2007 - 07:07 PM

View Postjayman9, on 5 Nov, 2007 - 05:28 PM, said:

The Import statements belong before the class declaration.
'Imports Data and SqlClient namespaces...
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1


Oh, now I feel stupid.
Was This Post Helpful? 0
  • +
  • -

#8 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: I need help with some code error's.

Posted 05 November 2007 - 07:52 PM

Don't feel stupid man, everyone had to start somewhere :)

Call them Growing Pains
Was This Post Helpful? 0
  • +
  • -

#9 jbeannie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 03-October 07

Re: I need help with some code error's.

Posted 05 November 2007 - 08:39 PM

Ok, so, I've been working on this code awhile.

This is what I came up with.

'Imports Data and SqlClient namespaces...
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1

	'TODO: This line of code loads data into the 'Xtreme2002DataSet.Customer' table. You can move, or remove it, as needed.
		Me.CustomerTableAdapter.Fill(Me.Xtreme2002DataSet.Customer)

	Private objCOnnection As New SqlConnection _
	("server=bnewsome;database=pubs;user id=sa;password=!p@ssw0rd?")

	Private objDataAdapter As New SqlDataAdapter()
	Private objDataSet As New DataSet()


	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'TODO: This line of code loads data into the 'Xtreme2002DataSet.Customer' table. You can move, or remove it, as needed.
		Me.CustomerTableAdapter.Fill(Me.Xtreme2002DataSet.Customer)

		'Set the SelectCommand properties...
		objDataAdapter.SelectCommand = New SqlCommand()
		objDataAdapter.SelectCommand.Connection = objCOnnection
		objDataAdapter.SelectCommand.CommandText = _
		"SELECT au_1name, au_fname, title, price " & _
		"FROM authors " & _
		"JOIN titleauthor ON authors.au_id = titleauthor.au_id " & _
		"JOIN titles ON titleauthor.title_id = titles.title_id " & _
		"ORDER BY au_1name, au_fname"
		objDataAdapter.SelectCommand.CommandType = CommandType.Text

		'Open the database connection...
		objCOnnection.Open()

		'Fill the DataSet object with data...
		objDataAdapter.Fill(objDataSet, "authors")

		'Close the database connection...
		objCOnnection.Close()

		'Set the DataGridView properties to bind it to our data...
		grdAuthorTitles.AutoGenerateColumns = True
		grdAuthorTitles.DataSource = objDataSet
		grdAuthorTitles.DataMember = "authors"

		'Clean up"
		objDataAdapter = Nothing
		objCOnnection = Nothing

	End Sub
End Class


The error's are:

Error 1 Syntax error. 8 9

Error 2 Name 'grdAuthorTitles' is not declared. 42 9

Error 3 Name 'grdAuthorTitles' is not declared. 43 9

Error 4 Name 'grdAuthorTitles' is not declared. 44 9

Now I'm wondering how to fix these.

Hmmm...
Was This Post Helpful? 0
  • +
  • -

#10 aceofspades686  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: I need help with some code error's.

Posted 05 November 2007 - 09:13 PM

You have this line outside any functions or sub procedures.
Me.CustomerTableAdapter.Fill(Me.Xtreme2002DataSet.Customer)

Admittedly it has been awhile since I dealt with databases, but that's the first thing I see that doesn't seem right. Also, if I'm reading right you have two different sets of data objects(connection, data adapter, dataset, etc) that you swap back and forth between.

Also, the tags for code are [ code] and [ /code] (without the spaces of course) not [ quote], you got it close though ;)

This post has been edited by aceofspades686: 05 November 2007 - 09:16 PM

Was This Post Helpful? 0
  • +
  • -

#11 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: I need help with some code error's.

Posted 05 November 2007 - 10:10 PM

View Postjbeannie, on 5 Nov, 2007 - 07:39 PM, said:

The error's are:

Error 1 Syntax error. 8 9

Error 2 Name 'grdAuthorTitles' is not declared. 42 9

Error 3 Name 'grdAuthorTitles' is not declared. 43 9

Error 4 Name 'grdAuthorTitles' is not declared. 44 9

Now I'm wondering how to fix these.

Hmmm...


This is saying that there is no control with the name of grdAuthorTitles on this form
Was This Post Helpful? 0
  • +
  • -

#12 jbeannie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 03-October 07

Re: I need help with some code error's.

Posted 05 November 2007 - 10:32 PM

Quote

'Imports Data and SqlClient namespaces...
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1

Private objCOnnection As New SqlConnection _
("server=bnewsome;database=pubs;user id=sa;password=!p@ssw0rd?")

Private objDataAdapter As New SqlDataAdapter()
Private objDataSet As New DataSet()


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Xtreme2002DataSet.Customer' table. You can move, or remove it, as needed.
Me.CustomerTableAdapter.Fill(Me.Xtreme2002DataSet.Customer)

'Set the SelectCommand properties...
objDataAdapter.SelectCommand = New SqlCommand()
objDataAdapter.SelectCommand.Connection = objCOnnection
objDataAdapter.SelectCommand.CommandText = _
"SELECT au_1name, au_fname, title, price " & _
"FROM authors " & _
"JOIN titleauthor ON authors.au_id = titleauthor.au_id " & _
"JOIN titles ON titleauthor.title_id = titles.title_id " & _
"ORDER BY au_1name, au_fname"
objDataAdapter.SelectCommand.CommandType = CommandType.Text

'Open the database connection...
objCOnnection.Open()

'Fill the DataSet object with data...
objDataAdapter.Fill(objDataSet, "authors")

'Close the database connection...
objCOnnection.Close()

'Set the DataGridView properties to bind it to our data...
grdXtreme.AutoGenerateColumns = True
grdXtreme.DataSource = objDataSet
grdXtreme.DataMember = "authors"

'Clean up"
objDataAdapter = Nothing
objCOnnection = Nothing

End Sub
End Class


I fixed all the error's, thanks for all your guy's help.

You guy's are awesome.

:D

Quote

objCOnnection.Open()


Hmmm... I got an sql exception error, from the quote up above.

This post has been edited by jbeannie: 05 November 2007 - 10:45 PM

Was This Post Helpful? 0
  • +
  • -

#13 aceofspades686  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: I need help with some code error's.

Posted 05 November 2007 - 11:13 PM

Quote

Hmmm... I got an sql exception error, from the quote up above.

As a safety precaution, its usually a good idea to wrap anything involving a database connection in a Try...Catch block.
Try
objDataAdapter.SelectCommand = New SqlCommand()
objDataAdapter.SelectCommand.Connection = objCOnnection
objDataAdapter.SelectCommand.CommandText = _
"SELECT au_1name, au_fname, title, price " & _
"FROM authors " & _
"JOIN titleauthor ON authors.au_id = titleauthor.au_id " & _
"JOIN titles ON titleauthor.title_id = titles.title_id " & _
"ORDER BY au_1name, au_fname"
objDataAdapter.SelectCommand.CommandType = CommandType.Text

'Open the database connection...
objCOnnection.Open()

'Fill the DataSet object with data...
objDataAdapter.Fill(objDataSet, "authors")

'Close the database connection...
objCOnnection.Close()
Catch ex as Exception
'Will pop open a message box containing the exception, could use console.writeline() or anything really.
Messagebox.Show(ex.Message)
End Try

I believe that's the proper syntax, though I'm blanking a little right now. That will help you identify what's throwing the exception, and possibly clear it up. As a side note, you probably shouldn't leave the exception showing in the messagebox in the final release, it could contain information that you don't want the user to know and could compromise security. This is just the way I personally do it when debugging.
Was This Post Helpful? 0
  • +
  • -

#14 jbeannie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 03-October 07

Re: I need help with some code error's.

Posted 06 November 2007 - 02:16 AM

The setting's for SQL server does not allow remote connection's.

How do I change the SQL server setting's?

Quote

grdXtreme.DataMember = "author"


Also got a argument exception was unhandled, in the quote above.

The quote beneath this text, is new modified code.

Quote

'Imports Data and SqlClient namespaces...
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1

Private objCOnnection As New SqlConnection _
("server=bnewsome;database=pubs;user id=sa;password=!p@ssw0rd?")

Private objDataAdapter As New SqlDataAdapter()
Private objDataSet As New DataSet()


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Xtreme2002DataSet.Customer' table. You can move, or remove it, as needed.
Me.CustomerTableAdapter.Fill(Me.Xtreme2002DataSet.Customer)

Try
objDataAdapter.SelectCommand = New SqlCommand()
objDataAdapter.SelectCommand.Connection = objCOnnection
objDataAdapter.SelectCommand.CommandText = _
"SELECT au_1name, au_fname, title, price " & _
"FROM authors " & _
"JOIN titleauthor ON authors.au_id = titleauthor.au_id " & _
"JOIN titles ON titleauthor.title_id = titles.title_id " & _
"ORDER BY au_1name, au_fname"
objDataAdapter.SelectCommand.CommandType = CommandType.Text

'Open the database connection...
objCOnnection.Open()

'Fill the DataSet object with data...
objDataAdapter.Fill(objDataSet, "authors")

'Close the database connection...
objCOnnection.Close()
Catch ex As Exception
'Will pop open a message box containing the exception, could use console.writeline() or anything really.
MessageBox.Show(ex.Message)
End Try

'Set the DataGridView properties to bind it to our data...
grdXtreme.AutoGenerateColumns = True
grdXtreme.DataSource = objDataSet
grdXtreme.DataMember = "author"

'Clean up"
objDataAdapter = Nothing
objCOnnection = Nothing

End Sub
End Class

This post has been edited by jbeannie: 06 November 2007 - 02:21 AM

Was This Post Helpful? 0
  • +
  • -

#15 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: I need help with some code error's.

Posted 06 November 2007 - 05:49 AM

You got the unhandled argument exception because the connection to the database failed to open, then at this line:
grdXtreme.DataMember = "author" it was looking for something that didn't exist.

You cant change the settings for the SQL Server, unless you are its administrator. You need to talk to your System Administrator, or DBA (Database Administrator) about making sure you can connect to the database from within your application. It sounds as if they have it locked down for connections outside the network, you need to find this out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1