Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 11th, 2005, 08:35 AM
Authorized User
 
Join Date: Apr 2005
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default Getting quote ID from Database - Urgent

Hey all, I'm trying to get the QuoteID displayed in a message box, what it is is that when the user clicks on button1, the data on the webform gets saved with a unique number issued (QuoteID) it is this number that I need to obtain, any suggestions. Note all the details get saved in a database.

As always all help is greatly appreciated

D

 
Old May 11th, 2005, 08:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

So how do you want to get the QuoteID. Do you want to pull it from the database or store it in a variable and grab it from there?
 
Old May 11th, 2005, 09:04 AM
Authorized User
 
Join Date: Apr 2005
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I would of thought pulling it straight out from the database would be better, by the way QuoteID is a column, and not linked with row.
 
Old May 11th, 2005, 02:07 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Is QuoteID the primary key of the database? Is this Access, SQL Server, etc...? If it is SQL are you using Identitys??? These are a couple of things we need to know.

Or, if you are generating a unique key prior to inserting this into the database it seems to make more sense to set it equal to a variable because otherwise you would have to write more SQL to retrieve it after it is inserted.

J
 
Old May 12th, 2005, 03:07 AM
Authorized User
 
Join Date: Apr 2005
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry J, no this is a MS Access Database, and the QuoteID is a autonumber generated when the user saves the quote to the Database. It is the Primary key for that table.

D

 
Old May 12th, 2005, 03:47 AM
Friend of Wrox
 
Join Date: Apr 2005
Posts: 186
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi,

You can go for select max(Id) from yourTable
Immideatly after inserting , also put the both in a transction.

This will work in any back end DBMS

Prashant



 
Old May 12th, 2005, 03:56 AM
Authorized User
 
Join Date: Apr 2005
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How would I write the code for that

Sub QuoteID
    Code to connect to DB
    max(ID)' refering to the QuoteID
End Sub

and if I want to call that in my msgbox

msgbox("Quote=" &QuoteID& )

is that how I would approach it, of if I could see some sample code that would be great.

D

 
Old May 12th, 2005, 04:30 AM
Authorized User
 
Join Date: Apr 2005
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay just gave the following code a try and I got a compiler error, any suggestions,

Function Quote(ByVal quoteID As Integer) As System.Data.DataSet
        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=\\boston.co.uk\"& _
"storage\users\Dhwiren\quote.mdb"
        Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString )

        Dim queryString As String = "SELECT MAX(QuoteID) FROM Quotes "
        Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        Dim dbParam_quoteID As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
        dbParam_quoteID.ParameterName = "@QuoteID"
        dbParam_quoteID.Value = quoteID
        dbParam_quoteID.DbType = System.Data.DbType.Int32
        dbCommand.Parameters.Add(dbParam_quoteID)

        Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
        dataAdapter.SelectCommand = dbCommand
        Dim dataSet As System.Data.DataSet = New System.Data.DataSet
        dataAdapter.Fill(dataSet)

        Return dataSet
    End Function

 msgbox("Quote Saved Successfully, QuoteID =" & Quote &)

the compiler error is;

Compiler Error Message: BC30455: Argument not specified for parameter 'quoteID' of 'Public Function Quote(quoteID As Integer) As System.Data.DataSet'.

Source Error:



Line 401: (CType(DropDownList15.SelectedItem.Value, String)), _
Line 402: (CType(Additional.Text, String)))
Line 403: msgbox("Quote Saved Successfully, QuoteID =" & Quote &)
Line 404: End Sub
Line 405:

Line 403 is showing the error

 
Old May 12th, 2005, 05:10 AM
Friend of Wrox
 
Join Date: Apr 2005
Posts: 186
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

No parammerters, Use ExecuteScalar

Code:
        Dim queryString As String = "SELECT MAX(QuoteID) FROM Quotes "
        Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        dbConnection.open
        string strID = dbCommand.ExecuteScalar()
Prashant

 
Old May 12th, 2005, 05:16 AM
Authorized User
 
Join Date: Apr 2005
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Prashant,
my code now looks like below
Code:
Function Quote(ByVal QuoteID As Integer) As System.Data.DataSet
        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=\\boston.co.uk\"& _
"storage\users\Dhwiren\quote.mdb"
        Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)

        Dim queryString As String = "SELECT MAX(QuoteID) FROM Quotes "
        Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        dbConnection.open
        string strID = dbCommand.ExecuteScalar()

        Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
        dataAdapter.SelectCommand = dbCommand
        Dim dataSet As System.Data.DataSet = New System.Data.DataSet
        dataAdapter.Fill(dataSet)

        Return dataSet
    End Function
but why am I still getting a compiler error for the messagebox (see below)

Compiler Error Message: BC32017: Comma, ')', or a valid expression continuation expected.

Source Error:



Line 401: (CType(DropDownList15.SelectedItem.Value, String)), _
Line 402: (CType(Additional.Text, String)))
Line 403: msgbox("Quote Saved Successfully, QuoteID =" Quote)
Line 404: End Sub
Line 405:

Error on line 403






Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the LAN ID and insert into database ? kutel ASP.NET 2.0 Professional 1 August 15th, 2007 03:51 AM
Getting Unique ID from Database Nicky_uk Classic ASP Databases 9 January 26th, 2005 04:45 PM
How to Auto Generate ID (Primary Key) SQL Database havering SQL Server ASP 1 December 9th, 2004 05:33 AM
Urgent please help to find Max ID and add 1 to it nhatrang71 Access 5 June 28th, 2004 10:24 PM
open a database depending on session id gtmm9 Classic ASP Databases 1 October 22nd, 2003 01:52 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.