retrieving data in a column using datarow / datatables - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

retrieving data in a column using datarow / datatables

glenharvyglenharvy Posts: 5Member
Hi,
using C#;
using VS2005;

I have a dataset that contains several datatables. What I wish to do is search each row in one of the tables and read/copy the value of a column into a variable. I will then manipulate the variable as needed. Specifically, the dataTable is "MembershipDetails" and the column is "MembershipNumber". The dataset is called "myclubDataSet".

The purpose of all this is to determine the next sequential membership number.

If someone could be so good enough to write the code for me I would very much appreciate it because I can't find a simple straightforward example anywhere on the net to learn from.

I know how to do a sql command to do this but I don't want to do it on the database. I wish to achieve it using the dataset.

Thanks in advance.
Glen Harvy

Comments

  • tsagldtsagld Posts: 621Member
    : Hi,
    : using C#;
    : using VS2005;
    :
    : I have a dataset that contains several datatables. What I wish to do is search each row in one of the tables and read/copy the value of a column into a variable. I will then manipulate the variable as needed. Specifically, the dataTable is "MembershipDetails" and the column is "MembershipNumber". The dataset is called "myclubDataSet".
    :
    : The purpose of all this is to determine the next sequential membership number.
    :
    : If someone could be so good enough to write the code for me I would very much appreciate it because I can't find a simple straightforward example anywhere on the net to learn from.
    :
    : I know how to do a sql command to do this but I don't want to do it on the database. I wish to achieve it using the dataset.
    :
    : Thanks in advance.
    : Glen Harvy

    If your goal is to increment the MembershipNumber column for a new row, you can set the DataColumn.AutoIncrement property to true.

    If not, you can get the value using:
    [code]
    DataTable dataTable = DataSet["MembershipDetails"];
    DataRow dataRow = table[rowNumber];

    int num = System.Convert.ToInt32(dataRow["MembershipNumber"]);
    [/code]


    Greets,
    Eric Goldstein
    http://www.gvh-maatwerk.nl


  • glenharvyglenharvy Posts: 5Member
    Thanks very much for your time and interest in helping me ...

    : If your goal is to increment the MembershipNumber column for a new row, you can set the DataColumn.AutoIncrement property to true.
    :

    My goal is to learn how to extract a value from a column in a datarow and this just happens to be the project I'm using to learn with :-) I am aware of the autoincrement and use it elsewhere in my project.

    : If not, you can get the value using:
    : [code]
    : DataTable dataTable = DataSet["MembershipDetails"];
    : DataRow dataRow = table[rowNumber];
    :
    : int num = System.Convert.ToInt32(dataRow["MembershipNumber"]);
    : [/code]
    :

    I've created the following code so far with your help:
    [code]
    DataTable table = myclubDataSet.Tables["MemberDetails"];
    DataRow[] foundRows;
    foundRows = table.Select("MembershipNumber > 1 ");

    for (int i = 0; i < foundRows.Length; i++)
    {
    int num = System.Convert.ToInt32(foundRows["MembershipNumber"]);
    MessageBox.Show("Membership number is: " + num);
    }
    [/code]
    When I try to build this I get an error
    "Error Cannot implicitly convert type 'string' to 'int' "
    with the culprit being MembershipNumber.

    In the datatable, MembershipNumber is a Int32.

    I can't figure it out - looks fine to me.

    I'm getting used to solving one issue and then creating another .

    Can you help me again please. Any other tips will be much appreciated.
    Glen Harvy

  • glenharvyglenharvy Posts: 5Member
    : When I try to build this I get an error
    : "Error Cannot implicitly convert type 'string' to 'int' "
    : with the culprit being MembershipNumber.
    :

    This problem was found to be me changing the column types in the .xsd file and not then updating the sql commands to update the database.

    : I'm getting used to solving one issue and then creating another .

    Here's what I ended up with and it works :-)

    [code]
    private void defaults()
    {
    this.memberDetailsTableAdapter.Fill(this.myclubDataSet.MemberDetails);

    int totalRows = myclubDataSet.MemberDetails.Rows.Count;
    short maxMemNum = 0;
    short nextMemNum = 0;
    for (int i = 0; i < totalRows; i++)
    {
    short MemNum = (short)myclubDataSet.Tables["MemberDetails"].Rows[i]["MembershipNumber"];
    if (MemNum >= maxMemNum)
    {
    maxMemNum = MemNum;
    nextMemNum = ++maxMemNum;
    MemNum = nextMemNum;
    }
    }
    this.editMembershipNumber.Text = nextMemNum.ToString();
    }
    [/code]

    Thanks again.
    Glen Harvy

Sign In or Register to comment.