Sign in to follow this  

Unity [.net] System.IndexOutOfRangeException error

This topic is 4310 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey Community, In sql server, I created a table with around 40 'columbs' in it. When I try to use the System::Data::SqlClient::SqlDataReader class to read it, I get this exception when I try to read anything over 3 columbs with it. Here is the whole procedure:
void DataEngine::FindEntry(DataName ^data, DataEntry^ entry)
{
	System::String^ constr;
	System::String^ querystr;

	constr = "SERVER = .\\SQLEXPRESS; DATABASE = TestDB9; Trusted_Connection = true";
	querystr = "SELECT LastName, FirstName, MiddleInitial, AccountNumber FROM SavingsAccount";

	System::Data::SqlClient::SqlConnection^ con = gcnew System::Data::SqlClient::SqlConnection(constr);
	System::Data::SqlClient::SqlCommand^ com = gcnew System::Data::SqlClient::SqlCommand(querystr, con);
	System::Data::SqlClient::SqlDataReader^ reader;

	con->Open();
	reader = com->ExecuteReader();

	while(reader->Read())
	{
		System::Windows::Forms::MessageBox^ box;

		System::String^ last = reader->GetString(0);
		System::String^ first = reader->GetString(1);
		System::String^ MI = reader->GetString(2);

		//if(last == data->NameLast->ToString())
		//	box->Show("fdsa1");

		if(!(reader->GetString(0)->CompareTo(data->NameLast->ToString())))
		{
			if(!(reader->GetString(1)->CompareTo(data->NameFirst->ToString())))
			{
				if(!(reader->GetString(2)->CompareTo(data->NameMI->ToString())))
				{
					entry->LastName = reader->GetString(0);
					entry->FirstName = reader->GetString(1);
					entry->MiddleInitial = reader->GetString(2);
					entry->AccountNumber = reader->GetDouble(3);
					//entry->BirthDate = reader->GetDateTime(4).ToString();
					//entry->PrimaryAddress1 = reader->GetString(5);
					//entry->PrimaryAddress2 = reader->GetString(6);
					//entry->PrimaryCity = reader->GetString(7);
					//entry->PrimaryState = reader->GetString(8);
					//entry->PrimaryZipcode = reader->GetString(9);

					System::Windows::Forms::MessageBox^ box;
					box->Show(entry->LastName);
				};
			};
		};
	};

	reader->Close();
	con->Close();

	return;
};

Thanks for any help, exorcist_bob

Share this post


Link to post
Share on other sites
Checking the FieldCount property yields that there are only four columbs in the table. Yet when I create the table, I create 39 columbs. And when I write a new record, I don't get any overflow errors.

Table query:

sqlTableSavingsAccount = "CREATE TABLE SavingsAccount"
+ "( LastName varchar(40) NOT NULL,"
+ "FirstName varchar(40) NOT NULL,"
+ "MiddleInitial char(1) NOT NULL,"
+ "AccountNumber float NOT NULL,"
+ "BirthDate datetime NOT NULL,"
+ "PrimaryAddress1 varchar(40) NOT NULL,"
+ "PrimaryAddress2 varchar(40) NOT NULL,"
+ "PrimaryCity varchar(40) NOT NULL,"
+ "PrimaryState varchar(40) NOT NULL,"
+ "PrimaryZipcode varchar(10) NOT NULL,"
+ "SecondaryAddress1 varchar(40) NOT NULL,"
+ "SecondaryAddress2 varchar(40) NOT NULL,"
+ "SecondaryCity varchar(40) NOT NULL,"
+ "SecondaryState varchar(40) NOT NULL,"
+ "SecondaryZipcode varchar(10) NOT NULL,"
+ "SSN1 char(3) NOT NULL,"
+ "SSN2 char(2) NOT NULL,"
+ "SSN3 char(4) NOT NULL,"
+ "PPN1 char(3) NOT NULL,"
+ "PPN2 char(3) NOT NULL,"
+ "PPN3 char(4) NOT NULL,"
+ "WPN1 char(3) NOT NULL,"
+ "WPN2 char(3) NOT NULL,"
+ "WPN3 char(4) NOT NULL,"
+ "OPN1 char(3),"
+ "OPN2 char(3),"
+ "OPN3 char(4),"
+ "MPN1 char(3),"
+ "MPN2 char(3),"
+ "MPN3 char(4),"
+ "Gender varchar(7) NOT NULL,"
+ "EmailAddress varchar(40),"
+ "PlaceOfEmployment varchar(40) NOT NULL,"
+ "Employer varchar(40) NOT NULL,"
+ "EmployerAddress1 varchar(40) NOT NULL,"
+ "EmployerAddress2 varchar(40) NOT NULL,"
+ "EmployerCity varchar(40) NOT NULL,"
+ "EmployerState varchar(40) NOT NULL,"
+ "EmployerZipCode varchar(10) NOT NULL,"
+ "Balance money NOT NULL)";



Inserting account query:


constr = "INSERT INTO SavingsAccount (LastName, FirstName, MiddleInitial, AccountNumber, BirthDate, "
+ "PrimaryAddress1, PrimaryAddress2, PrimaryCity, PrimaryState, PrimaryZipcode, "
+ "SecondaryAddress1, SecondaryAddress2, SecondaryCity, SecondaryState, SecondaryZipcode, "
+ "SSN1, SSN2, SSN3, PPN1, PPN2, PPN3, WPN1, WPN2, WPN3, OPN1, OPN2, OPN3, MPN1, MPN2, MPN3, "
+ "Gender, EmailAddress, PlaceOfEmployment, Employer, EmployerAddress1, EmployerAddress2, "
+ "EmployerCity, EmployerState, EmployerZipCode, Balance)VALUES('" + data->LastName + "','"
+ data->FirstName + "','" + data->MiddleInitial + "','"
+ num.ToString() + "','" + data->BirthDate->ToString() + "','"
+ data->PrimaryAddress1 + "','" + data->PrimaryAddress2 + "','" + data->PrimaryCity + "','"
+ data->PrimaryState + "','" + data->PrimaryZipcode + "','" + data->SecondaryAddress1 + "','"
+ data->SecondaryAddress2 + "','" + data->SecondaryCity + "','" + data->SecondaryState + "','"
+ data->SecondaryZipcode + "','" + data->SSN1 + "','" + data->SSN2 + "','" + data->SSN3 + "','"
+ data->PPN1 + "','" + data->PPN2 + "','" + data->PPN3 + "','"
+ data->WPN1 + "','" + data->WPN2 + "','" + data->WPN3 + "','"
+ data->OPN1 + "','" + data->OPN2 + "','" + data->OPN3 + "','"
+ data->MPN1 + "','" + data->MPN2 + "','" + data->MPN3 + "','"
+ data->Gender + "','" + data->EmailAddress + "','" + data->PlaceOfEmployment + "','"
+ data->Employer + "','" + data->EmployerAddress1 + "','" + data->EmployerAddress2 + "','"
+ data->EmployerCity + "','" + data->EmployerState + "','" + data->EmployerZipCode + "','0.00')";



Please help, and thanks
exorcist_bob

Share this post


Link to post
Share on other sites
You are only selecting four columns in your select query, so the result set it returns will only contain those four columns. You need to either do "SELECT * FROM SavingsAccount" or specify all of the columns that you will need to access.

Share this post


Link to post
Share on other sites

This topic is 4310 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this