Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

c++.net 2005 and ado.net problem

jut26jut26 Member Posts: 2
Hello,
[color=Black]
I am trying to learn how to programatically create a connection to a sql server database on my local machine. My server name is DAEDULUS and database name is RezSys. When ever I try to extract data from the table Hotel it gives me the following error:[/color]
[color=Green]
Connection Sucessfull

Unhandled Exception: System.IndexOutOfRangeException: Index was outside the boun
ds of the array.
at System.Data.SqlClient.SqlDataReader.ReadColumn(Int32 i, Boolean setTimeout
)
at System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)
at main(String[] args) in c:documents and settingsjutmy documentsvisual s
tudio 2005projects est est est.cpp:line 28
at mainCRTStartupStrArray(String[] arguments) in f:
tmvctoolscrt_bldself_
x86crtsrcmcrtexe.cpp:line 324
Press any key to continue . . .
[/color]
[color=Black]I know I should have a try catch block but that will come after.

My source code is below:[/color]
[color=Green]
#include "stdafx.h"

using namespace System;
using namespace System::Data::SqlClient;

int main(array ^args)
{


Console::WriteLine(L"Hello World");

SqlCommand^ comm;

SqlConnection^ myCon = gcnew SqlConnection("server=DAEDULUS;Trusted_Connection=yes;Initial Catalog=RezSys;");
myCon->Open();
comm = gcnew SqlCommand("select [RoomType] from [Hotel]",myCon);
if(comm->ExecuteNonQuery())
{
Console::WriteLine(L"Connection Sucessfull");
}

SqlDataReader^ reader;
reader = comm->ExecuteReader();
while(reader->Read())
{
Console::Write(reader->GetInt32(1));
}
myCon->Close();

return 0;
}
[/color]

[color=Black]
I am not sure what I am doing wrong any help would be appreciated.


Sincerely,
Jut26
[/color]

Comments

  • jut26jut26 Member Posts: 2
    [color=Black]Sorry forgot to include the code for my table[/color]
    [color=Green]
    RoomNum int Unchecked
    RoomType nchar(25) Unchecked
    Arrival smalldatetime Checked
    Departure smalldatetime Checked
    Availibility bit Unchecked

    [/color]

    : Hello,
    : [color=Black]
    : I am trying to learn how to programatically create a connection to
    : a sql server database on my local machine. My server name is
    : DAEDULUS and database name is RezSys. When ever I try to extract
    : data from the table Hotel it gives me the following error:[/color]
    : [color=Green]
    : Connection Sucessfull
    :
    : Unhandled Exception: System.IndexOutOfRangeException: Index was
    : outside the boun
    : ds of the array.
    : at System.Data.SqlClient.SqlDataReader.ReadColumn(Int32 i,
    : Boolean setTimeout
    : )
    : at System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)
    : at main(String[] args) in c:documents and settingsjutmy
    : documentsvisual s
    : tudio 2005projects est est est.cpp:line 28
    : at mainCRTStartupStrArray(String[] arguments) in
    : f:
    tmvctoolscrt_bldself_
    : x86crtsrcmcrtexe.cpp:line 324
    : Press any key to continue . . .
    : [/color]
    : [color=Black]I know I should have a try catch block but that will
    : come after.
    :
    : My source code is below:[/color]
    : [color=Green]
    : #include "stdafx.h"
    :
    : using namespace System;
    : using namespace System::Data::SqlClient;
    :
    : int main(array ^args)
    : {
    :
    :
    : Console::WriteLine(L"Hello World");
    :
    : SqlCommand^ comm;
    :
    : SqlConnection^ myCon = gcnew
    : SqlConnection("server=DAEDULUS;Trusted_Connection=yes;Initial
    : Catalog=RezSys;");
    : myCon->Open();
    : comm = gcnew SqlCommand("select [RoomType] from
    : [Hotel]",myCon);
    : if(comm->ExecuteNonQuery())
    : {
    : Console::WriteLine(L"Connection Sucessfull");
    : }
    :
    : SqlDataReader^ reader;
    : reader = comm->ExecuteReader();
    : while(reader->Read())
    : {
    : Console::Write(reader->GetInt32(1));
    : }
    : myCon->Close();
    :
    : return 0;
    : }
    : [/color]
    :
    : [color=Black]
    : I am not sure what I am doing wrong any help would be appreciated.
    :
    :
    : Sincerely,
    : Jut26
    : [/color]
    :

  • MegabozMegaboz Member Posts: 3
    I'm guessing since this was a while ago that you probably figured it out by now. Just in case though, the offending line looks like when you go run your GetInt32 method from your DataReader - you've only returned 1 column in your SQL statement, and items are 0 indexed on the data reader, so you should be asking for item 0, not 1. That's why it's giving you an out-of-bounds error.

    : Hello,
    : [color=Black]
    : I am trying to learn how to programatically create a connection to
    : a sql server database on my local machine. My server name is
    : DAEDULUS and database name is RezSys. When ever I try to extract
    : data from the table Hotel it gives me the following error:[/color]
    : [color=Green]
    : Connection Sucessfull
    :
    : Unhandled Exception: System.IndexOutOfRangeException: Index was
    : outside the boun
    : ds of the array.
    : at System.Data.SqlClient.SqlDataReader.ReadColumn(Int32 i,
    : Boolean setTimeout
    : )
    : at System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)
    : at main(String[] args) in c:documents and settingsjutmy
    : documentsvisual s
    : tudio 2005projects est est est.cpp:line 28
    : at mainCRTStartupStrArray(String[] arguments) in
    : f:
    tmvctoolscrt_bldself_
    : x86crtsrcmcrtexe.cpp:line 324
    : Press any key to continue . . .
    : [/color]
    : [color=Black]I know I should have a try catch block but that will
    : come after.
    :
    : My source code is below:[/color]
    : [color=Green]
    : #include "stdafx.h"
    :
    : using namespace System;
    : using namespace System::Data::SqlClient;
    :
    : int main(array ^args)
    : {
    :
    :
    : Console::WriteLine(L"Hello World");
    :
    : SqlCommand^ comm;
    :
    : SqlConnection^ myCon = gcnew
    : SqlConnection("server=DAEDULUS;Trusted_Connection=yes;Initial
    : Catalog=RezSys;");
    : myCon->Open();
    : comm = gcnew SqlCommand("select [RoomType] from
    : [Hotel]",myCon);
    : if(comm->ExecuteNonQuery())
    : {
    : Console::WriteLine(L"Connection Sucessfull");
    : }
    :
    : SqlDataReader^ reader;
    : reader = comm->ExecuteReader();
    : while(reader->Read())
    : {
    : Console::Write(reader->GetInt32(1));
    : }
    : myCon->Close();
    :
    : return 0;
    : }
    : [/color]
    :
    : [color=Black]
    : I am not sure what I am doing wrong any help would be appreciated.
    :
    :
    : Sincerely,
    : Jut26
    : [/color]
    :

Sign In or Register to comment.