Click here to Skip to main content
16,019,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get data from database table SQL to Textbox in the form (copmany) . I create class for the form contain a function to get the max value of row in the table
C#
//  class COMPCLS 
 public DataTable MaxNEW()
            {
                var DAL = new SQlcls.DataAccessLayer();

                var DT = new DataTable();
                DT = DAL.SelectData_txt("SELECT ISNULL(MAX (ID)+1,1) FROM [dbo].[company]", default);
                return DT;
            }

. then in the form (company) i create :
C#
//FRM_COMPANY
 public void MAX_NEW()
        {
            COMPCLS cmp = new COMPCLS();
            var DT = new DataTable();
            DT = cmp.MaxNEW();
            if (DT.Rows.Count > 0)
            {
                TXT_ID.Text = DT.Rows(0)(0).ToString();
                
            }

        }

it shows the error "
Non-invocable member 'Data.Table.Rows cannot be used like a method.


What I have tried:

I try search but I cannot get the answer
Posted
Updated 2-Sep-22 3:22am

Indexing in C# is by using square brackets, not parentheses:
C#
                TXT_ID.Text = DT.Rows(0)(0).ToString();

// should be
                TXT_ID.Text = DT.Rows[0][0].ToString();
 
Share this answer
 
Comments
mohamed thrwat 2-Sep-22 9:27am    
thank for help .. i am new with c#
Richard MacCutchan 2-Sep-22 9:43am    
You are welcome. But your code does not appear to be written by someone new. Or maybe you are moving from VB.NET to C#.
mohamed thrwat 2-Sep-22 14:28pm    
that is right i am moving to c# from vb.net but also not a master at vb.net :(
Replace
Quote:
TXT_ID.Text = DT.Rows(0)(0).ToString();
With
C#
TXT_ID.Text = DT.Rows[0][0].ToString();

(square brackets instead of round ones).
 
Share this answer
 
Comments
mohamed thrwat 2-Sep-22 9:27am    
thank for help .. i am new with c#
CPallini 2-Sep-22 9:38am    
You are welcome.
Indexes need square brackets, not round:
C#
TXT_ID.Text = DT.Rows(0)(0).ToString();
Should be
C#
TXT_ID.Text = DT.Rows[0][0].ToString();
 
Share this answer
 
Comments
mohamed thrwat 2-Sep-22 9:27am    
thank for help .. i am new with c#
OriginalGriff 2-Sep-22 9:32am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900