Wrox Programmer Forums
|
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP 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 March 15th, 2004, 11:02 AM
Registered User
 
Join Date: Mar 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default check is column name is correct

Hi

I need to check if a column name exists.

a script like this if exists(objRS("column_name")) = true then.....

obviously there is no such scrit as the one above, but if anyone has an idea or sample code i could try I'd appreciate it very much.

thanks
Ryan
 
Old March 15th, 2004, 11:13 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there.

What you could do is execute a SELECT * query and then loop through the Fields available in the Recordset, comparing their Name property against the value you're looking for. The code below shows what I mean:
Code:
<%
    Dim oField ' As ADODB.Field
    Dim oRecordset ' As ADODB.Recordset
    Dim oConn ' As ADODB.Connection
    Dim nameToCheck
    Dim nameExists

    ' The column name you're looking for
    nameToCheck = "ID"
    nameExists = false

    ' Create connection
    Set oConn = Server.CreateObject("ADODB.Connection")
    oConn.Open "YourConnectionString"

    Set oRecordset = oConn.Execute("SELECT * FROM YourTable")

    For Each oField In oRecordset.Fields
        If oField.Name = nameToCheck Then
            nameExists = True
            Exit For
        End If
    Next

    oRecordset.Close()
    oConn.Close()
    Set oConn = Nothing
    Set oRecordset = Nothing

    Response.Write("Field " & nameToCheck & " found " & nameExists)
%>
I am curious why you need to perform this check. Are you working on generic code, or coding against an unknown database?

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old March 15th, 2004, 02:55 PM
Registered User
 
Join Date: Mar 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

you are correct in that I am coding against an unknown database.
I have an app where a user imports data from a csv file into an sql table that has specific column names.

I want to make sure the user has the column names correct in his csv file and if not let him know which columns he has incorrectly named.

I'll give you code a try.

thanks

 
Old March 15th, 2004, 03:12 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Right, in that case my code should be useful to you.

You'll probably need to set up a loop for the columns in the database, and then inside the code block that checks the name, compare against the name in the CSV file.

You're probably best off with using a counter instead of a For Each statement, as you probably want the order of the columns in the CSV file to match those in the recordset.

Good luck, and let me know if you need any more help with this.

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Connect to VSS check-in Check-out Programatically rhd110 General .NET 6 August 12th, 2007 07:46 AM
check Box column in DataGridView control tiyyob .NET Framework 2.0 0 June 24th, 2006 09:49 AM
check if a column exists in the table sands SQL Server 2000 2 April 21st, 2006 11:08 PM
Help 'Check ListView' vs 'Check ListBox' MikeY C# 1 February 24th, 2005 02:20 PM
email validation check if it's correct? xristina Javascript 2 October 14th, 2004 01:51 PM





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