Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6364401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:09:32+00:00 2026-05-25T00:09:32+00:00

I am writing my first C# application, which in this case is just a

  • 0

I am writing my first C# application, which in this case is just a “learning exercise” This example is a simplified block of code that I have used many times in VB.Net so I know that it works correctly. This is what the VB code looks like.

Public Class User
    Private Const CN_LoginId As String = "Login"
    Private Const CN_Password As String = "Password"
    Private _password As String

    Public Property Password() As String
        Get
            Return _password
        End Get
        Set(ByVal value As String)
            _password = value
        End Set
    End Property

    Public Shared Function Create(ByVal Login As String) As User
        Dim usr = New User()

        Using dt As DataTable = DAC.ExecuteDataTable("usp_PasswordSelect", _
            DAC.Parameter(CN_LoginId, Login))
            With dt.Rows(0)
                usr.Password = CStr(.Item(CN_Password))
            End With
        End Using

        Return usr
    End Function
End Class

So in C# I have tried converting it by hand and by using Telerik’s online conversion utility, which is what I am posting below because I am assuming that it is closer to the right answer then what I did myself.

public class User
{
    private const string CN_LoginId = "Login";
    private const string CN_Password = "Password";
    private string _password;
    public string Password
    {
        get { return _password; }
        set { _password = value; }
    }

    public static User Create(string Login)
    {
        object usr = new User();

        using (DataTable dt = DAC.ExecuteDataTable("usp_PasswordSelect", 
            DAC.Parameter(CN_LoginId, Login)))
        {
            {
                usr.Password = Convert.ToString(dt.Rows(0).Item(CN_Password));
            }
        }

        return usr;
    }

}

The first error I get is on this line usr.Password = Convert.ToString(dt.Rows(0).Item(CN_Password));. The error is “Error 1 ‘object’ does not contain a definition for ‘Password’ and no extension method ‘Password’ accepting a first argument of type ‘object’ could be found (are you missing a using directive or an assembly reference?)

At this point I am assuming the second error will go away when I fix the first one. So my question is how do I correctly set the property for this Object using the DataTable in C#?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:09:33+00:00Added an answer on May 25, 2026 at 12:09 am

    Here’s how I’d write it:

    public class User
    {
        private const string CN_LoginId = "Login";
        private const string CN_Password = "Password";
    
        public string Password { get; set; }
    
        public static User Create(string Login)
        {
            User usr = new User();
            using (DataTable dt = DAC.ExecuteDataTable("usp_PasswordSelect", 
                DAC.Parameter(CN_LoginId, Login)))
                {
                    usr.Password = Convert.ToString(dt.Rows[0][CN_Password]);
                }
            return usr;
        }
    }
    

    Some notes:

    1. C# accesses arrays using square brackets, not parens. So Rows(0) isn’t correct – it should be Rows[0].
    2. I agree with Bernard that you should declare the User object as a User, and not as an object. You’ll lose all visibility into it’s properties. You can also use var, but if you’re learning C#, you may be better off explicitly declaring your variable types. It can make for duplicate declarations (e.g. User u = new User()) but at least you’ll see clearly what types your variables are.
    3. You don’t need to access array items using Item…just get it as an element in the row element you’re working with. That’s why I have the double array for dt.Rows[0][CN_Password]. Again, C# uses square brackets, not parens for accessing array elements.
    4. This is just a style thing, but I removed your _password field and just used an automatic property for Password. I didn’t see _password being used in your code and thought it would just clutter things up. Automatic properties have a backing field automatically created by the compiler, so you don’t have to keep track of the variable and the property. If you’re using a lot of properties, this can be a big time saver.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing my first distributed erlang application, and I notice that I have to
I've followed this tutorial: http://anandhansubbiah.com/blog/writing-your-first-android-application/ , but no matter what I do, and what
I'm writing an application using DDD techniques. This is my first attempt at a
I am writing an application which tests email addresses, for one part of this
First let me introduce the application scene: I have a service application which is
I am writing my first Android application and have some problems with handing around
I'm writing my first web application (still!), and have a problem activating user accounts.
I'm writing this application which is a flashcard app, I would like to create
I am writing my first WPF application. I have also installed Entity Framework (EF)
I'm writing my first NHibernate application, but I guess this question applies to any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.