none
Reading specific data>> Row[i].["column_name"] using C# (mdb) RRS feed

  • Question

  • Friends,

    I'm trying to read mdb data from a specific column, I tried something like this

    textBox1.Text = DS.Tables[0].Rows[i][

    "gender"].ToString();

    But this is not working.... kindly help


    I'm not perfect, neither are u. But who's here to compare anyway, let's cherish the togetherness.
    Friday, July 8, 2011 9:19 AM

All replies

  • Hi,

    you'd have to fill the dataset first.

    btw,

    thank you for calling me not perfect, it immediately created a feeling of rapport towards you.


    Regards, Nico
    Friday, July 8, 2011 9:29 AM
  • Hi,
    
    

    you'd have to fill the dataset first.

    btw,

    thank you for calling me not perfect, it immediately created a feeling of rapport towards you.


    Regards, Nico


    LOL, thx for that perfect reply. Will this code be enough to let u know that I'm having confusion in fetching the data from aparticular column using the columnname? :-)

       OleDbConnection con = new OleDbConnection(conString);
    
        var empcon = new OleDbCommand("SELECT name FROM [emp]", con);
    
        var adapter = new OleDbDataAdapter(empcon);
    
        DataSet DS = new DataSet();
    
        adapter.Fill(DS, "emp");
    
       int i = comboBox1.SelectedIndex;
    
       int y = ++i;
    
       if (i > -1 )
    
       {
    
        textBox1.Text = DS.Tables[0].Rows[i][0].ToString();
    
       }
    
        con.Close();
    
    
    
    

    I'm not perfect, neither are u. But who's here to compare anyway, let's cherish the togetherness.
    • Edited by gsvirdi Friday, July 8, 2011 9:42 AM spellings
    Friday, July 8, 2011 9:41 AM
  • The code seems ok,

    there is either no data in the table, or i gets a value that exceeds the number of rows, or there's a null value in the field. It would be useful to mention what the error is. Anyhow, if you learn how to debug, you would easily find the cause. Put a breakpoint after the 'adapter.fill ' statement, and inspect your dataset; go step by step (F10) from then on. Select variables and inpsect them as you go.


    Regards, Nico
    Friday, July 8, 2011 10:00 AM
  • Hi,

     

    change this line like that 

     textBox1.Text = DS.Tables(0).Rows(i)(0).ToString();

    Regards







    Friday, July 8, 2011 10:29 AM
  • The code seems ok,

    there is either no data in the table, or i gets a value that exceeds the number of rows, or there's a null value in the field. It would be useful to mention what the error is. Anyhow, if you learn how to debug, you would easily find the cause. Put a breakpoint after the 'adapter.fill ' statement, and inspect your dataset; go step by step (F10) from then on. Select variables and inpsect them as you go.


    Regards, Nico

    The error was that it was not able to find the column name "gender", column "gender" does not exists. I had found my error and answered it separatly. Thx so much for helping. :)
    I'm not perfect, neither are u. But who's here to compare anyway, let's cherish the togetherness.
    Friday, July 8, 2011 10:58 AM
  • Hi,

     change this line like that 

     textBox1.Text = DS.Tables(0).Rows(i)(0).ToString();
    Regards

    Changing [] into () will make Tables to be used as a method, which itself will be wrong practise. Thx anyways for replying. I've found the answer (my mistake) and shared it separately

    I'm not perfect, neither are u. But who's here to compare anyway, let's cherish the togetherness.
    Friday, July 8, 2011 10:59 AM
  • The error was from my side. I had a table (emp) with columns (name, id, gender). In "Command" statement I was making error

    var empcon = new OleDbCommand("SELECT name FROM [emp]", con); // Selecting only "name" from the table.

    Correction: var empcon = new OleDbCommand("SELECT * FROM [emp]", con);

    My mistake, appologies to all of u. As I know my talent (of making mistakes), I will be posting another doubt soon. LOL


    I'm not perfect, neither are u. But who's here to compare anyway, let's cherish the togetherness.
    • Edited by gsvirdi Friday, July 8, 2011 11:03 AM Typo mistakes
    Friday, July 8, 2011 11:02 AM
  • the mistake that you claim shouldn't make any difference in the code that you sent
    Regards, Nico
    Friday, July 8, 2011 11:53 AM
  • the mistake that you claim shouldn't make any difference in the code that you sent
    Regards, Nico


    in previous statement var empcon = new OleDbCommand("SELECT name FROM [emp]", con);  I was restricting it to the column "name" and I was trying to fetch data from column "gender" which was giving error "Column gender does not exists in the table"

    So the statment was corrected to var empcon = new OleDbCommand("SELECT * FROM [emp]", con); only then it was able to fetch all other columns also. Plz correct me if I'm understanding it wrongly. I have tried to do this change again and again... then I came to this conclusion.


    I'm not perfect, neither are u. But who's here to compare anyway, let's cherish the togetherness.
    Friday, July 8, 2011 11:59 AM
  • I the code you sent, you try to fetch the first column of the row, no name stated, so it should at least have given you the 'name'.
    Regards, Nico
    Friday, July 8, 2011 12:22 PM
  • Hi All,

     

    We don't refuse the error, it's the way to the success and better to know ourself.

     

    Thanks to all the participators.



    David Peng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Monday, July 11, 2011 3:12 AM
    Moderator