Register for your free account! | Forgot your password?

Go Back   elitepvpers > MMORPGs > Conquer Online 2 > CO2 Private Server
You last visited: Today at 13:55

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



MySql Execute : index was outside the bounds of the array at C#

Discussion on MySql Execute : index was outside the bounds of the array at C# within the CO2 Private Server forum part of the Conquer Online 2 category.

Reply
 
Old   #1
 
abdeen's Avatar
 
elite*gold: 0
Join Date: Mar 2010
Posts: 475
Received Thanks: 15
MySql Execute : index was outside the bounds of the array at C#

hello guys i have no idea why i get this error :

Code:
index was outside the bounds of the array
while i run this code

Code:
MySqlCommand cmd = new MySqlCommand(MySqlCommandType.INSERT);
                cmd.Insert("Entities")
                    .Insert("Name", eC.Name)
                    .Insert("Owner", client.Account.Username)
                    .Insert("Class", eC.Profession)
                    .Insert("ID", client.Entity.ID)
                    .Insert("Hitpoints", client.Entity.Hitpoints)
                    .Insert("Mana", client.Entity.Mana)
                    .Insert("Model", client.Entity.Model)
                    .Insert("Avatar", client.Entity.Avatar)
                    .Insert("HairStyle", client.Entity.HairStyle)
                .Insert("Strenght", client.Entity.Strenght)
                .Insert("Agility", client.Entity.Agility)
                .Insert("Vitality", client.Entity.Vitality)
                .Insert("Spirit", client.Entity.Spirit);
                cmd.Execute();
at this line :

Code:
cmd.Execute();
what's wrong with this code ?
abdeen is offline  
Old 09/18/2013, 19:17   #2
 
elite*gold: 0
Join Date: Sep 2012
Posts: 775
Received Thanks: 329
post a picture of the detailed error, but ingeneral the "Index was outside the bounds of the array" means the following
if you have an array of 4 elements , for instance int[] x = new int[3];
if you was trying to access x[4] it will gives you the same error "Index was outside the bounds of the array" because yes the index at this array is outside of the boundaries of this array

and it always happen when you try to access a non existing element of some array, it could happen in an indirect way but that's what you should be searching for

but most likely in your case that you want to insert something to a non existing column
go for it is offline  
Thanks
1 User
Old 09/18/2013, 20:16   #3
 
abdeen's Avatar
 
elite*gold: 0
Join Date: Mar 2010
Posts: 475
Received Thanks: 15
Quote:
Originally Posted by go for it View Post
post a picture of the detailed error, but ingeneral the "Index was outside the bounds of the array" means the following
if you have an array of 4 elements , for instance int[] x = new int[3];
if you was trying to access x[4] it will gives you the same error "Index was outside the bounds of the array" because yes the index at this array is outside of the boundaries of this array

and it always happen when you try to access a non existing element of some array, it could happen in an indirect way but that's what you should be searching for

but most likely in your case that you want to insert something to a non existing column

here is an image and i`d checked table Fields and they are correct all .
abdeen is offline  
Old 09/18/2013, 20:33   #4
 
elite*gold: 0
Join Date: Sep 2012
Posts: 775
Received Thanks: 329
Quote:
Originally Posted by abdeen View Post
here is an image and i`d checked table Fields and they are correct all .
that did not show any clue of what's wrong
one line after that it should show you where the error is at (which file and what line)
go there , add break point and see it works in action, that should show you what's wrong, if it didn't then give me more material
go for it is offline  
Thanks
1 User
Old 09/18/2013, 20:44   #5
 
shadowman123's Avatar
 
elite*gold: 0
Join Date: Aug 2007
Posts: 1,525
Received Thanks: 230
Posting Execute Method Code would help us understanding whats wrong
shadowman123 is offline  
Thanks
1 User
Old 09/18/2013, 20:50   #6
 
abdeen's Avatar
 
elite*gold: 0
Join Date: Mar 2010
Posts: 475
Received Thanks: 15
Quote:
Originally Posted by go for it View Post
that did not show any clue of what's wrong
one line after that it should show you where the error is at (which file and what line)
go there , add break point and see it works in action, that should show you what's wrong, if it didn't then give me more material
where i shall add this break point ?

at .execute(); or which line i need to add this to get more information about this error ?

i had added it to .execute() and this is a pic if its was in wrong point or place please correct me i never used break points before ...

abdeen is offline  
Old 09/18/2013, 20:53   #7
 
_Emme_'s Avatar
 
elite*gold: 1142
Join Date: Aug 2006
Posts: 2,464
Received Thanks: 1,162
Breakpoints won't help, as the Insert functions are merely to build a query which are then ran by Execute.

Post a picture of your database structure for the table Entities.
_Emme_ is offline  
Thanks
1 User
Old 09/18/2013, 21:03   #8
 
elite*gold: 0
Join Date: Sep 2012
Posts: 775
Received Thanks: 329
Quote:
Originally Posted by 'Emil View Post
Breakpoints won't help, as the Insert functions are merely to build a query which are then ran by Execute.

Post a picture of your database structure for the table Entities.
^ true that, but still you would want to know what types he is trying to cast in the database table, so you still need to know both types of what he is trying to insert and to what type in db structure
go for it is offline  
Thanks
1 User
Old 09/18/2013, 22:33   #9
 
abdeen's Avatar
 
elite*gold: 0
Join Date: Mar 2010
Posts: 475
Received Thanks: 15
Okay guys now i got it .. i don`t get this error if i comment those two lines

Code:
.Insert("Name", eC.CharacterName)
                    .Insert("Class", eC.CharacterClass)
but i get it again if i uncommented them .. i don`t know why is that .. although the character class is correct as its was selected but the name always comes have wrong chars ...

Example :

if i set my char name this : Abdeen

i get it A@^de%n

something like this .. here is my code for entity create :

Code:
public static bool CreateEntity(Network.GamePackets.EnitityCreate eC, Client.GameState client, ref string message)
        {
            try
            {
                if (eC.CharacterName.Length > 16)
                    eC.CharacterName = eC.CharacterName.Substring(0, 16);
                if (eC.CharacterName == "")
                    return false;

                if (InvalidCharacters(eC.Name))
                {
                    message = "The chosen name is contains Invalid characters .";
                    return false;
                }
                var rdr = new MySqlReader(new MySqlCommand(MySqlCommandType.SELECT).Select("cq_user").Where("name", eC.CharacterName));
                if (rdr.Read())
                {
                    rdr.Close();
                    message = "The chosen name is already in use.";
                    return false;
                }
                rdr.Close();
                client.Entity = new Entity(EntityFlag.Player, false);
                client.Entity.Name = eC.CharacterName;
                DataHolder.GetStats((byte)eC.CharacterClass, 1, client);
                client.Entity.ID = Program.EntityUID.Next;
                new MySqlCommand(MySqlCommandType.UPDATE).Update("configuration").Set("EntityID", client.Entity.ID).Where("Server", ServerBase.Constants.ServerName).Execute();
                //client.CalculateStatBonus();
                //client.CalculateHPBonus();
                client.Entity.Hitpoints = client.Entity.MaxHitpoints;
                client.Entity.Mana = (ushort)(client.Entity.Spirit * 5);
                client.Entity.Class = (byte)eC.CharacterClass;
                client.Entity.Model = eC.CharacterModel;
                if (eC.CharacterModel == 1003 && eC.CharacterClass == 10 || eC.CharacterModel == 1003 && eC.CharacterClass == 20 || eC.CharacterModel == 1003 && eC.CharacterClass == 40 || eC.CharacterModel == 1003 && eC.CharacterClass == 100 || eC.CharacterModel == 1004 && eC.CharacterClass == 10 || eC.CharacterModel == 1004 && eC.CharacterClass == 20 || eC.CharacterModel == 1004 && eC.CharacterClass == 40 || eC.CharacterModel == 1004 && eC.CharacterClass == 100)
                {
                    client.Entity.Avatar = (ushort)ServerBase.Kernel.Random.Next(1, 50);
                }
                else
                {
                    client.Entity.Avatar = (ushort)ServerBase.Kernel.Random.Next(201, 250);
                }
                byte Color = (byte)ServerBase.Kernel.Random.Next(4, 8);

                client.Entity.HairStyle = (ushort)(Color * 100 + 10 + (byte)ServerBase.Kernel.Random.Next(4, 9));
                client.Account.EntityID = client.Entity.ID;
                client.Account.Save();

                MySqlCommand cmd = new MySqlCommand(MySqlCommandType.INSERT);
                cmd.Insert("cq_user")
                    .Insert("Name", eC.CharacterName)
                    .Insert("Owner", client.Account.Username)
                    .Insert("Class", eC.CharacterClass)
                    .Insert("ID", client.Entity.ID)
                    .Insert("Hitpoints", client.Entity.Hitpoints)
                    .Insert("Mana", client.Entity.Mana)
                    .Insert("Model", client.Entity.Model)
                    .Insert("Avatar", client.Entity.Avatar)
                    .Insert("HairStyle", client.Entity.HairStyle)
                .Insert("Strenght", client.Entity.Strenght)
                .Insert("Agility", client.Entity.Agility)
                .Insert("Vitality", client.Entity.Vitality)
                .Insert("Spirit", client.Entity.Spirit);
                cmd.Execute();
                client.Account.LoginState = AccountTable.AccountLoginState.Login;
                new MySqlCommand(MySqlCommandType.UPDATE)
                    .Update("accounts")
                    .Set("LoginState", (byte)client.Account.LoginState)
                    .Where("EntityID", client.Entity.ID)
                    .Execute();
                message = "ANSWER_OK";
            }
            catch (Exception cc) { GameServer.ServerBase.ErrorHandler.Catch(cc); message = "A problem when we registering your character , please try again later !!"; return false; }
            return true;
        }
and here is my Create Entity Packet :

Code:
public class EnitityCreate : Interfaces.IPacket
    {
        byte[] Buffer;
        public EnitityCreate(bool Create)
        {
            if (Create)
            {
                this.Buffer = new byte[60];
                WriteUInt16(60, this.Buffer, 0);
                WriteUInt16(0x3E9, this.Buffer, 2);
            }
        }
        public string AccountName
        {
            get
            {
                return Encoding.ASCII.GetString(Buffer, 4, 16);
            }
            set
            {
                Network.Writer.WriteString(value, 4, Buffer);
            }
        }
        public string AccountPassword
        {
            get
            {
                return Encoding.ASCII.GetString(Buffer, 36, 16);
            }
            set
            {
                Network.Writer.WriteString(value, 36, Buffer);
            }
        }
        public string CharacterName
        {
            get
            {
                return Encoding.ASCII.GetString(Buffer, 20, 16);
            }
            set
            {
                Network.Writer.WriteString(value, 20, Buffer);
            }
        }
        public ushort CharacterModel
        {
            get
            {
                return BitConverter.ToUInt16(Buffer, 52);
            }
            set
            {
                Network.Writer.WriteUInt16(value, 52, Buffer);
            }
        }
        public ushort CharacterClass
        {
            get
            {
                return BitConverter.ToUInt16(Buffer, 54);
            }
            set
            {
                Network.Writer.WriteUInt16(value, 54, Buffer);
            }
        }
        public uint AccountUniqueID
        {
            get
            {
                return BitConverter.ToUInt32(Buffer, 56);
            }
            set
            {
                Network.Writer.WriteUInt32(value, 56, Buffer);
            }
        }
        public byte[] Serialize()
        {
            return this.Buffer;
        }
        public void Deserialize(byte[] buffer)
        {
            Buffer = buffer;
        }
        public byte[] ToArray()
        {
            return Buffer;
        }
        public void Send(Client.GameState client)
        {
            client.Send(Buffer);
        }
        public static void ZeroFill(byte[] Buffer, ushort Offset, ushort Count)
        {
            for (ushort i = 0; i < Count; i++)
                Buffer[i + Offset] = 0x00;
        }
        public static void WriteStringWithLength(string Arg, byte[] Buffer, ushort Offset)
        {
            Buffer[Offset] = (byte)Arg.Length;
            Offset++;
            ushort i = 0;
            while (i < Arg.Length)
            {
                Buffer[(ushort)(i + Offset)] = (byte)Arg[i];
                i = (ushort)(i + 1);
            }
        }
        public static void WriteString(string Arg, byte[] Buffer, ushort Offset)
        {
            ushort i = 0;
            while (i < Arg.Length)
            {
                Buffer[(ushort)(i + Offset)] = (byte)Arg[i];
                i = (ushort)(i + 1);
            }
        }
        public static void WriteUInt16(ushort Arg, byte[] Buffer, ushort Offset)
        {
            Buffer[Offset] = (byte)(Arg);
            Buffer[Offset + 1] = (byte)(Arg >> 8);
        }
        public static void WriteUInt32(uint Arg, byte[] Buffer, ushort Offset)
        {
            Buffer[Offset] = (byte)(Arg);
            Buffer[Offset + 1] = (byte)(Arg >> 8);
            Buffer[Offset + 2] = (byte)(Arg >> 16);
            Buffer[Offset + 3] = (byte)(Arg >> 24);
        }
    }
any help with this shit which i don't know why i get it or why i get the name is not which one i inserted ?
abdeen is offline  
Old 09/18/2013, 23:28   #10
 
elite*gold: 0
Join Date: Sep 2012
Posts: 775
Received Thanks: 329
well afaik this characters is acceptable at database string, but as you can notice it increase the length which could be increasing the name length than the boundaries in db (in case you are using limited string type maybe nvarchar(14) or something like that)

im really sorry im not good at this but here is a lame solution and something to look at
first using regax to remove that special characters added
Code:
private static readonly Regex InvalidFileRegex = new Regex(
    string.Format("[{0}]", Regex.Escape(@"<>:""/\|?*")));

public static string SanitizeFileName(string fileName)
{
    return InvalidFileRegex.Replace(fileName, string.Empty);
}
and the best thing to do is looking at the packet structure and it's offsets , also the crypto, maybe compare it to another source of same patch range

im sorry again, i can't figure out what's wrong, maybe someone else will be able to help at this
go for it is offline  
Old 09/18/2013, 23:48   #11
 
abdeen's Avatar
 
elite*gold: 0
Join Date: Mar 2010
Posts: 475
Received Thanks: 15
Quote:
Originally Posted by go for it View Post
well afaik this characters is acceptable at database string, but as you can notice it increase the length which could be increasing the name length than the boundaries in db (in case you are using limited string type maybe nvarchar(14) or something like that)

im really sorry im not good at this but here is a lame solution and something to look at
first using regax to remove that special characters added
Code:
private static readonly Regex InvalidFileRegex = new Regex(
    string.Format("[{0}]", Regex.Escape(@"<>:""/\|?*")));

public static string SanitizeFileName(string fileName)
{
    return InvalidFileRegex.Replace(fileName, string.Empty);
}
and the best thing to do is looking at the packet structure and it's offsets , also the crypto, maybe compare it to another source of same patch range

im sorry again, i can't figure out what's wrong, maybe someone else will be able to help at this
thank you brother for replying and for trying to help me too i a appreciate that
abdeen is offline  
Old 09/19/2013, 00:12   #12
 
shadowman123's Avatar
 
elite*gold: 0
Join Date: Aug 2007
Posts: 1,525
Received Thanks: 230
dude the problem is very expectable now ..the error is in eC.Name .. and the problem is that the Database Cant Save this Name as its string type isnt supported in database .. go to entities Table then Design Table .. then check Name column and make sure that its type is varchar , Character Set is utf8 , and Collation is utf8_general_ci then Debug and see
shadowman123 is offline  
Thanks
1 User
Old 09/19/2013, 00:14   #13
 
_Emme_'s Avatar
 
elite*gold: 1142
Join Date: Aug 2006
Posts: 2,464
Received Thanks: 1,162
Why are you passing ec.CharacterName/Class to the insert, when you already populated the client.entity variables for this? Other than that the code looks fine as far as I can tell, but then again I haven't touched a source in like forever so I can't remember if the name has an encryption on it directly from the packet.

Quote:
Originally Posted by shadowman123 View Post
dude the problem is very expectable now ..the error is in eC.Name .. and the problem is that the Database Cant Save this Name as its string type isnt supported in database .. go to entities Table then Design Table .. then check Name column and make sure that its type is varchar , Character Set is utf8 , and Collation is utf8_general_ci then Debug and see
Not necessarily..

Quote:
Originally Posted by abdeen View Post
Example :

if i set my char name this : Abdeen

i get it A@^de%n
_Emme_ is offline  
Thanks
1 User
Old 09/19/2013, 00:35   #14
 
shadowman123's Avatar
 
elite*gold: 0
Join Date: Aug 2007
Posts: 1,525
Received Thanks: 230
the problem seems from the packet 1001 itself .. make sure you are using the correct method to get the name u Entered it should be like that

Code:
Name = Encoding.Default.GetString(buffer, 24, 16).Replace("\0", "");
shadowman123 is offline  
Thanks
1 User
Old 09/19/2013, 00:41   #15
 
elite*gold: 0
Join Date: Sep 2012
Posts: 775
Received Thanks: 329
true what's 'emil said at not changing the type at db because it won't solve the wrong name you are getting
and shadowman123 is right aswell about the encoding, it could be anything from cryptography to encoding
go for it is offline  
Thanks
1 User
Reply


Similar Threads Similar Threads
Array auf Negativen Index-Zugriff checken?
04/25/2013 - C/C++ - 6 Replies
Hey, Bin grade dabei in c++ Conways Game of Life zu coden zum training, nun wollte ich den Check auf Nachbar-Zellen machen, jedoch crasht das Game zu Beginn wenn man als Start-Zelle im Check nen zu geringen Wert hat. Beispiel ums genauer zu erläutern -> int mapsize=6; //Spielfeldgröße.. 6x6 Spielfeld int map; int nachbar_oben = map; //Zelle über der Zelle an "position"
[Help]Albetros MYSQL execute errors
12/22/2012 - CO2 Private Server - 11 Replies
I have some errors on that source can some1 help me please? 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'skip,1; END ; -- ---------------------------- -- Procedure structure for' at line 5 INSERT INTO spellinfo VALUES ('10359', '30010', 'IceBolt', '0036', '0', '0', '0', '0004', '0000', '0000', '120', '0000', '0100', '0000', '0008', '0020', '0000', '0000', '0', '0000', '0', '0000',...
LUA I/O & mysql (via os.execute)
10/12/2011 - Metin2 PServer Guides & Strategies - 26 Replies
Hallo =) Ich will hier mal was hoffentlich neues Releasen, dass für große Server recht interessant ist.. Ich habe von einiger Zeit mal nach Möglichkeiten gesucht, um bestimmte Daten die in einer Quest benutzt werden woanders als in in der Quest-Tabelle speichern zu lassen? Warum? Ganz einfach: Wenn ich jetzt etwas auslesen will also z.B. bei einem 2 sprachigen Server muss man ja irgendwo abspeichern, welche Sprache der user hat.. Das kann man ja ganz normal per setqf machen.. Aber wenn ich...



All times are GMT +1. The time now is 13:57.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.