niggz

I managed to get it to work. The thing I changed was this:

[code=c#]

var club = Baza.Clubs; //Baza is dataset, and Clubs is table
var schedule = Baza.Schedule; // same as above

for (int i = 0; i < schedule.count; i++) // THIS WAS REPLACED
{
if(schedule[i].day == 2)
{
//Do something..
}
}

[/code]

AND MOST IMPORTANTLY... I added this in form load:
[code=c#]
scheduleTableAdapter.Fill(Baza.schedule);
[/code]

I had some database issues, so I had to delete it, and add again, and thats why this error was created. Thanks guys!

niggz

[QUOTE=MikeyIsMe;1734878]Surely there is just a lack of rows in whatever you are pulling as you are looking for the row (2) so the third row in. Run a debug and use a break point to see what is contained within the schedule and see if that helps to solve the issue?

Im making assumptions but the error as Thines01 said could either be that it is null or that the schedule actually contains no rows.[/QUOTE]
I've been through locals, and it contains only columns, but no rows. I'm not sure why. Why do the other tables work properly?

niggz

[QUOTE=thines01;1734531][CODE]
var schedule = Baza.Schedule; // same as above
if(null == schedule)
{
//Something is wrong before it got to here
}
[/CODE][/QUOTE]
No, thats okay..

niggz

[QUOTE=thines01;1734506]Is Schedule null when it is assigned?[/QUOTE]
Sorry, not sure what you mean.. Where is it assigned? At 'var schedule = Baza.schedule'?

And btw, Baza.Clubs has 10 rows according to Locals, but Baza.Schedule does not have any..

niggz Light Poster

Hi again!

I have a database and some tables filled with data.

[code=c#]
var club = Baza.Clubs; //Baza is dataset, and Clubs is table
var schedule = Baza.Schedule; // same as above

        for (int i = 1; i <= 90; i++)
        {
            if(schedule[i].day == 2) // BOOM! Breaks at first pass..
            {
                //Do something..
            }
        }

[/code]

When I run my app it throws me this error: There is no row at position 0
and throws me to dataSet.Designer.cs to this part of code:

[code=c#]
public ScheduleRow this[int index] {
get {
return ((ScheduleRow)(this.Rows[index]));
}
}
[/code]

Any ideas why this happens?

niggz

Oh shoot... Thank you! Player has the field with which it is connected with a club, so I can check the ID of it... Yea that's right!

But do you think it would be a problem to go through the whole database everytime I want to access a certain player of a certain club? (it has only 250 records)

niggz

When I included the database into my c# project, it automatically created the table in dataset. But I can't find it always, but only when clubsTableAdapter and playersTableAdapter are included on the same form. (sorry about not giving that info)

niggz

It exsists. Still not sure what you think..

niggz Light Poster

Hello.

I have two tables in a database - Clubs and Players. Each Club consists of 25 players. So a virtual table ClubsPlayers is created.

I would like to access this tables row or cell, but I don't know how.

As you can access real tables like this:
[code=C#]
xDataSet.Clubs[1].IdClub;
[/code]

niggz

I mean, each of variables inside F increases every time I hover over it in the locals. Like something's running in the background..

niggz Light Poster

Hello again.

I have a property 'F' in class 'Club'. I realized that it doesn't function properly while going through debugging and locals. At first look it had normal, wanted values.
BUT than every time I hovered over class->property F it increased by *2 or seomthing like that.

Here's the code:

[CODE=C#]
class formation
{
private int[] frm = new int[7];
private int def, dmf, sb, smf, mid, amf, st;

    public int[] Formation
    {
        get {
            frm[0] = Sb;
            frm[1] = Def;
            frm[2] = Dmf;
            frm[3] = Smf;
            frm[4] = Mid;
            frm[5] = Amf;
            frm[6] = St;
            return frm; 
        }
    }
    public int Smf
    {
        get { return smf; }
        set { smf = value; }
    }
    public int Sb
    {
        get { return sb; }
        set { sb = value; }
    }
    public int St
    {
        get { return st; }
        set {
            st = value; 
            }
    }
    public int Amf
    {
        get { return amf; }
        set {
            amf = value; 
            }
    }
    public int Mid
    {
        get { return mid; }
        set {
            mid = value; 
            }
    }
    public int Dmf
    {
        get { return dmf; }
        set {
            dmf = value; 
            }
    }
    public int Def
    {
        get { return def; }
        set {
            def = value; 
            }
    }

}

//////////////////////////////////////
class Club
{
private formation f = new formation();
public formation F
{
get
{
for (int i = 0; i <= 10; i++)
{
switch (PlayersBL[i].Position)
{
case "SB":
f.Sb++; break;
case "DC":
f.Def++; ...

niggz

Okay, it works just fine.. :)

[CODE=C#]
private static bool readDatabase(ref string strError, Club[] liga)
{
bool blnRetVal = true;

        //Club[] liga = new Club[10];
        // Define the Select statement.
        string selectSQL = "SELECT * FROM Players";
        string selectClub = "SELECT * FROM Clubs";

        try
        {
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                conn.Open();
                using (OleDbDataReader rdr = (new OleDbCommand(selectClub, conn)).ExecuteReader())
                {
                    int inx = 0;
                    while (rdr.Read()) // Fill clubs
                    {
                        Club club = new Club(); //Moved from the top of the function, to here
                        club.Id = int.Parse(rdr["IDclub"].ToString());
                        club.clubName = rdr["ClubName"].ToString().Trim();
                        club.Pld = int.Parse(rdr["Pld"].ToString());
                        club.W = int.Parse(rdr["W"].ToString());
                        club.D = int.Parse(rdr["D"].ToString());
                        club.L = int.Parse(rdr["L"].ToString());
                        club.Gf = int.Parse(rdr["Gf"].ToString());
                        club.Ga = int.Parse(rdr["Ga"].ToString());
                        club.Gd = int.Parse(rdr["Gd"].ToString());
                        club.Money = int.Parse(rdr["Money"].ToString());
                        liga[inx] = club;
                        inx++;
                    }
                    rdr.Close();
                }
                using (OleDbDataReader rdr = (new OleDbCommand(selectSQL, conn)).ExecuteReader())
                {
                    while (rdr.Read()) // Fill players
                    {
                        Player player = new Player();//Moved from the top of the function, to here
                        player.Name = rdr["name"].ToString().Trim();
                        player.Surname = rdr["surname"].ToString().Trim();
                        player.Position = rdr["position"].ToString().Trim();
                        player.Age = int.Parse(rdr["age"].ToString().Trim());
                        player.DaysInj = int.Parse(rdr["daysInj"].ToString().Trim());
                        player.ClubPlayingFor = int.Parse(rdr["Club"].ToString());
                        player.IdPlayer = int.Parse(rdr["IDplayer"].ToString());

                        player.Creativity = int.Parse(rdr["creativity"].ToString());
                        player.Shooting = int.Parse(rdr["shooting"].ToString());
                        player.Passing = int.Parse(rdr["creativity"].ToString());
                        player.Speed = int.Parse(rdr["speed"].ToString());
                        player.Jump = int.Parse(rdr["jump"].ToString());
                        player.Head = int.Parse(rdr["head"].ToString());
                        player.Defence = int.Parse(rdr["goalkeeping"].ToString());
                        player.Creativity = int.Parse(rdr["creativity"].ToString());

                        foreach (Club c in liga)
                        {
                            if (player.ClubPlayingFor == c.Id)
                            {
                                c.PlayersBL.Add(player);
                            }
                        }
                    }
                    rdr.Close();
                }
                conn.Close();
            }
        }
        catch (Exception exc)
        {
            blnRetVal = false;
            strError = exc.Message;
        }

        MessageBox.Show(strError.ToString());
        return blnRetVal;
    }

[/CODE]

niggz Light Poster

Hello. I couldn't think of any better name for the title.
To the point...

I have this database reader method, and In the while loop, where the liga[] is being populated, all its values are the last that is read from DB..
Clubs ID are from 1 - 10
Any suggestions?

[CODE=C#]
private static bool readDatabase(ref string strError, Club[] liga)
{
bool blnRetVal = true;
Player player = new Player();
Club club = new Club();
string selectSQL = "SELECT FROM Players";
string selectClub = "SELECT
FROM Clubs";

        try
        {
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                conn.Open();
                using (OleDbDataReader rdr = (new OleDbCommand(selectClub, conn)).ExecuteReader())
                {
                    while (rdr.Read()) // THIS IS WHERE THE PROBLEM STARTS
                    {
                        club.Id = int.Parse(rdr["IDclub"].ToString());
                        club.clubName = rdr["ClubName"].ToString().Trim();
                        club.Pld = int.Parse(rdr["Pld"].ToString());
                        club.W = int.Parse(rdr["W"].ToString());
                        club.D = int.Parse(rdr["D"].ToString());
                        club.L = int.Parse(rdr["L"].ToString());
                        club.Gf = int.Parse(rdr["Gf"].ToString());
                        club.Ga = int.Parse(rdr["Ga"].ToString());
                        club.Gd = int.Parse(rdr["Gd"].ToString());
                        club.Money = int.Parse(rdr["Money"].ToString());
                        liga[club.Id-1] = club;
                    }
                    rdr.Close();
                }
                using (OleDbDataReader rdr = (new OleDbCommand(selectSQL, conn)).ExecuteReader())
                {
                    while (rdr.Read()) // Fill players
                    {
                        player.Name = rdr["name"].ToString().Trim();
                        player.Surname = rdr["surname"].ToString().Trim();
                        player.Position = rdr["position"].ToString().Trim();
                        player.Age = int.Parse(rdr["age"].ToString().Trim());
                        player.DaysInj = int.Parse(rdr["daysInj"].ToString().Trim());
                        player.ClubPlayingFor = int.Parse(rdr["Club"].ToString());
                        player.IdPlayer = int.Parse(rdr["IDplayer"].ToString());

                        player.Creativity = int.Parse(rdr["creativity"].ToString());
                        player.Shooting = int.Parse(rdr["shooting"].ToString());
                        player.Passing = int.Parse(rdr["creativity"].ToString());
                        player.Speed = int.Parse(rdr["speed"].ToString());
                        player.Jump = int.Parse(rdr["jump"].ToString());
                        player.Head = int.Parse(rdr["head"].ToString());
                        player.Defence = int.Parse(rdr["goalkeeping"].ToString());
                        player.Creativity = int.Parse(rdr["creativity"].ToString());

                        foreach (Club c in liga)
                        {
                            if (player.ClubPlayingFor == c.Id)
                            {
                                c.PlayersBL.Add(player);
                            }
                        }
                    }
                    rdr.Close();
                }
                conn.Close();
            }
        }
        catch (Exception exc)
        {
            blnRetVal = false;
            strError = exc.Message;
        }

        MessageBox.Show(strError.ToString());
        return blnRetVal;
    }

[/CODE]

niggz

[QUOTE=thines01;1712115]Inside the while(rdr.Read()), you can use:

[CODE]
string strMyField = rdr["FIELDNAME"].ToString().Trim();
[/CODE]

So, depending on the structure of the object "Player", you can query the values like this directly into the fields of Player;[/QUOTE]

Okay. Thank you very much!

niggz

[QUOTE=thines01;1712011]You have the words "Data Source" twice in your connection string.

You could also change your function so it can [I]also[/I] be used outside of a WinForms app sorta like this:.........[/QUOTE]

Thanks, this is perfect.
I know I'm a pain in the a$$, but please tell me one more thing.

How to get a value from a certain field in the table?

niggz

[QUOTE=thines01;1711194]Have you looked into the IDataReader types, such as OdbcDataReader?[/QUOTE]
Okay, I wrote some code, like this:

[CODE=c#]
private string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; data source=" + "Data Source='D:\MyDatabase.accdb'";

    private void readDatabase() {
        // Define the Select statement.
        string selectSQL;
        selectSQL = "SELECT name, surname FROM Players";

        // Define the ADO.NET objects.
        OleDbConnection con = new OleDbConnection(connectionString);
        OleDbCommand cmd = new OleDbCommand(selectSQL, con);
        OleDbDataReader reader;

        // Try to open database and read information.
        try
        {
            con.Open();
            reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                player.Name = reader["name"].ToString();
                uc.PlayersBL.Add(player);
                MessageBox.Show("Players name is " + uc.PlayersBL[0]);
            }
            reader.Close();
        }
        catch (Exception err)
        {
            MessageBox.Show(err.Message,"An error occured! Description:\n\n" + err.Message);
        }
        finally
        {
            con.Close();
        }
    }

[/CODE]

But now I get this error:
Format of the initialization string does not conform to specification starting at index 34.

niggz Light Poster

Hello masters!

I have a project and need a bit of your help.
I would like to access/get values from certain fields from a database and assing that value to a variable.

Database is included into my project along with DataSet.

Is there any simple way to access certain field?

Roughly, something like:

String myString = "";
Database db = "C:\myDatabase.accdb";

myString = db.Tables["Table1"].Row[0].Cells[1];

niggz

I have solved my problem.

The players array contained players, but it was not full.

It looked something like this:

  1. John Doe
  2. Mark Markins
  3. Bill Billy
  4. null
  5. null

so I just inserted one if:

[code=c#]
private void cmbTacticsAddPlayers()
{

        foreach (var cmb in cmbPlayers)
        {
            foreach (var player in league[0].Players)
            {
                if (player == null)// solved the problem
                {
                    continue;
                }
                else
                    cmb.Items.Add(player.Name); // THREW EXCEPTION
            }
        }
    }

[/code]

niggz

@thines01: No. The problem still exists. It seems like it goes trough the first time (adds player name in combo box 1), but than stops.

@Momerath: I'm sorry that I didn't provide this information, but league array is filled with Club objects using another method.

I've tried numerous ways, without success. But I realize something in this try:

[code=c#]
private void cmbTacticsAddPlayers()
{

        foreach (var cmb in cmbPlayers)
        {
            foreach (var player in league[0].Players)
            {
                cmb.Items.Add(player.Name); // THROWS EXCEPTION
            }
        }
    }

[/code]

I've gone trough Locals, and it says that variable 'player' is Null. How come? How to set it to 'new Player()'? I tried it outside, inside the loop, without success.

niggz Light Poster

Hello! I have a little problem. I don't understand how to solve this issue:

Object reference not set to an instance of an object.

Thank you!

This is my code:

[code=c#]
BindingList cmbPlayers = new BindingList();
Club[] league = new Club[10];

// league contains class Club
// class Club contains property Player[] (set as new Player[30])
// Players contains class Player
// Player has property String Name

    private void cmbTacticsAddPlayers() {

        foreach (var cmb in cmbPlayers)
        {
            for (int i = 0; i <= league[0].Players.Length;i++ )
            {
                cmb.Items.Add(league[0].Players[i].Name); // THROWS EXCEPTION
            }
        }
    }

[/code]

niggz

Okay. Great!

You guys are of a great help for us beginners.

Third problem solved since joining your forum :)

niggz Light Poster

Hello.

I have a problem writing to a file using for loop.

here is the code:

[CODE=cpp]
/* the code should save numbers in a file something like this

1 5
2 8
3 7
4 6
*/

//instead it gives just the last numbers it catches
/*

4 6

*/
for(int i=0;i<=4;i++){
//some code before

int iCom = (rand()%3+5);

ofstream played("played.txt");
if (played.is_open()){
    played << i << " " << iCom;
    played << endl;
    played.close();                                               
}
else cout << "Unable to save" << endl;

}
[/CODE]

Thanks

niggz

Yeah. Great guys.

Thanks a lot!

Hope it helps other beginners

niggz Light Poster

Hi masters!

I have a problem with my while loop.
I realised that my code does not work the way I want.
It has to check if sum of def, mid and att variables is different from 10 or if style variable is different from multiple strings. So if any of them does not meet the requirement, repeat the code in {} .

My style condition which is repeated is interfering with other style conditions.

So my question is - How to write this while loop (or it doesn't have to be while) to give me the thing I want?

Is there any way to write it like:

[code=cpp]
while(style!={"contain","defensive","counter"}){
//do something
}
[/code]

Here is the code:
[code=cpp]

char znak; // just takes "-" in the line like this: 4-4-2
string style = "standard"; // default style

cout << "--- MATCH PREPARATION ---" << endl;
cout << "Choose formation (like 4-4-2): "; cin >> def >> znak >> mid >> znak >> fwd;
cout << endl;
cout << "Choose style (contain,defensive,counter,standard,control,attacking,overload): " << endl;
cin >> style;
cout << endl;

/////////Problematic while loop////////////////////
while((def+mid+fwd)!=10 || style!="contain" || style!="defensive" || style!="counter" || style!="standard" || style!="control" || style!="attacking" || style!="overload")
{
cout << "Unknown style or formation! Please check your spelling..." << endl;
cout << "Choose formation (like 4-4-2): ";
cin >> def >> znak >> mid >> znak >> fwd;
cout << endl;
cout << "Choose style(contain,defensive,counter,standard,control,attacking,overload): " << endl;
cin >> style;
cout << endl;
}
[/code]

niggz

@Arbus: oh, yeah. You're right. And I've been searching for solution for a few hours...

@Narue: it is probably a good idea, but it's a bit too advanced for my course..
coms[] is a class type.

Here is more info about the code:

Club coms[9]; // Club is a class
userClub uc; // userClub is a class
coms[].pts is an integer;
coms[].name is a string;
coms[]

niggz Light Poster

Hello everyone.

I am working on my final project at university for C++, and I got stuck a little.
I have an array inside an array. And I want to access that inner array's member and print it with cout.

Here is the code:

[code=c]
void standings(userClub uc){
cout << endl;
cout << "First Division" << endl;

int table[10];

for(int i=0; i<=9; i++){
    table[i] = coms[i].pts;
}

table[9]=uc.pts;
sort(table, table+10);

int y=9;
while(y>=0){
    cout << table[coms[y].name] << "\t"; // HERE I WANT TO ACCESS
    cout << table[y] << endl;            // coms array
    y--;
}

}
[/code]

Please have in mind that I'm not a pro... Thanks x)