Hi guys, I am a newbie to perl and keen to learn so would you please help me I try to write the script connecting to sqlserver 2005. I am using the windows NT authentication to set up the DSN The script is working when i run it from the command prompt Part of it is:

#!/usr/bin/perl use CGI qw/:standard/; use strict; use Win32::ODBC; my $db = new Win32::ODBC("DSN=portal-mk2"); my $DSN = "portal-mk2"; if (!($db = new Win32::ODBC($DSN))){ print "Error connecting to $DSN\n"; print "Error: " . Win32::ODBC::Error() . "\n"; exit; } my $SqlStatement = "SELECT Id, StudFirstName, StudLastName, HomeGroup + FROM dbo.StudDetails"; if ($db->Sql($SqlStatement)){ print "SQL failed.\n"; print "Error: " . $db->Error() . "\n"; $db->Close(); exit; }

However when i try to use CGI to connect i have problem to open connection. Here is part of the script:

use CGI qw/:standard :html3 :netscape/; use Win32::ODBC; # Unpack parameters from form $cmd=param('cmd'); #Command to processs $DSN=param('DSN'); #Selected DSN $table=param('table'); #Selected table from DSN @cols=param('cols'); #Selected columns from table $sort=param('sort'); #Column to sort on $rows=param('rows'); #Max number of rows # Off we go... # If no command or DSN then get user to select one. if ($cmd eq '' || $DSN eq ''){ %DSN=Win32::ODBC::DataSources; #Get list of datasources @keys=sort(keys(%DSN)); #put list of DSNs into array &screen_header; #Start HTML response print start_form, hidden(-name=>'cmd',-value=>'table',-force=>1), table( Tr([ td(['Data Source: ', popup_menu(-name=>'DSN',-value=>\@keys)."&nbsp&nbsp&nbsp".s +ubmit(-name=>'GO')]) ]) ), end_form, hr, end_html; exit(0); } # If no table selected then get table name if ($cmd eq 'table' || $table eq ''){ $db=new Win32::ODBC($DSN); #create new instance if (! $db){ perror("Error opening data source $DSN"); exit(0); } @tables=$db->TableList("","","","TABLE"); #Get list of tables

Could you show me where error is?


In reply to Data base connection error by mhoang

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.