[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

Difficulty in Accessing ColumnName in a TypedDataset

I am having a severe problem accessing TypedDataset ColumnName to be passed as a parameter to my stored procedure

Below code I want to eliminate "Name" and put  TypedDataset ColumnName
    _Database.AddInParameter(InsertCommand, "@Name", DbType.string, "Name", DataRowVersion.Current);

VisualStudio.NET 2005 IDE --- TypedDataSet Name ----- CustomerDataSet   TableName:Customer
I wanted to pass as :  CustomerDataSet.Customer.Name but I dont get in my code. Any one had same issues.



0
dotnet0824
Asked:
dotnet0824
  • 10
  • 10
1 Solution
 
VBRocksCommented:
You should be able to do it like this:

_Database.AddInParameter(InsertCommand, "@" & CustomerDataSet.Customer.Name.ColumnName", DbType.string, "Name", DataRowVersion.Current);


0
 
VBRocksCommented:
Oops, remove the quote (") after CustomerDataSet.Customer.Name.ColumnName,



0
 
dotnet0824Author Commented:
CustomerDataSet  _dsCustomer= new CustomerDataSet();
_dsCustomer.Customer.NameColumn               /// I Only Get this
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
VBRocksCommented:
You should be able to do it this way:
    _dsCustomer.Customer.NameColumn.ColumnName


0
 
dotnet0824Author Commented:
Nope I dont get it......U can try adding a TypedDataset to your Visual studio.NET 2005 Designer and check it out
0
 
VBRocksCommented:
Yes, I did, both in VB and C#, and it works fine.

This is straight out of my C# project, and it worked fine:

            CustomerDataSet _dsCustomer = new CustomerDataSet();
            System.Diagnostics.Debug.WriteLine  (_dsCustomer.Customer.NameColumn.ColumnName);

So, I'm not sure why it's not working for you.

Try using the ToString() method, that should return the column name as well:
            _dsCustomer.Customer.NameColumn.ToString()

0
 
dotnet0824Author Commented:
Though this is not related to the question.. (I found something weird when u add a typed Dataset and name the Dataset Same as TableName which u drag from the Server explorer in IDE )
lets say Customer Table is dragged and u name the TypedDataset Customer and when u try to refer it in ur code it take _Customer  (thats the reason I named the TypedDataset as CustomerDataset /Table name being Customer)
0
 
dotnet0824Author Commented:
Well I created a Project Named  (Class)

Biz.US
 Now I add New Item ---- Select Dataset  and Drag a Table - Customer from Server Explorer
I remove the DataAdapater in the Diagram as We dont need it as I am working on Code to populate it .
Every thing works fine but I cant access the ColumnNames directly

Now i Refer in my code  
Biz.US.CustomerDataSet.Customer.  like that
0
 
VBRocksCommented:
I was able to reproduce the situation, so I see what you are saying.

So can you get the column name now, or do you have to create an instance of it?
    Biz.US.CustomerDataSet.Customer.ColumnName

or
     Biz.US.CustomerDataSet _dsCustomer = new Biz.US.CustomerDataSet();
      _dsCustomer.Customer.NameColumn.ColumnName;
0
 
dotnet0824Author Commented:
ok now i use this   Biz.US.CustomerDataset in another Class like this
////////////////////////////////////////////////////////
using BIZ.US  (Imports in VB.NET) added a refernce of BIZ.US in this Class
  public int Add(Biz.US.CustomerDataSet dsCustomer)
        {
            try
            {
       BIZ.US.CustomerDataSet  dsCustomer = new BIZ.US.CustomerDataSet();
         dsCustomer.Customer.NameColumn.     (After this NameColum Dot I dont get Name)
         

             }
0
 
VBRocksCommented:
What options do you have in the drop down list when you type a period "." after
    dsCustomer.Customer.NameColumn?

0
 
dotnet0824Author Commented:
dsCustomer.Customer.NameColumn.AllowDBNull,
                                                           AutoIncrement,Caption,ColumnMapping,DataType,ColumnName

                     and other string functions Lenglth,Padleft,PadRight etc
 
0
 
VBRocksCommented:
So select the "ColumnName" property, like this:
    dsCustomer.Customer.NameColumn.ColumnName

0
 
dotnet0824Author Commented:
Is  dsCustomer.Customer.NameColumn.ColumnName   = dsCustomer.Customer.NameColumn.Name??
0
 
dotnet0824Author Commented:
No VBRocks...  its wrong

dsCustomer.Customer.NameColumn.                    (I have Name,Address,Active Fields)  I can see ColumnName common.......... I cant Refer  Name or address or Active
0
 
VBRocksCommented:
In response to your first post:

"NameColumn" is the the actual column that is named "Name", and it is of type DataColumn.  

"ColumnName" is the name of the column, and it is of type "String".  It will return the name of the
    DataColumn.

In your last post, you still list "ColumnName" as one of the properties?

0
 
dotnet0824Author Commented:
Hi VbRocks,
We have to refer as

dsCustomer.Customer.NameColumn.ColumnName
dsCustomer.Customer.AddressColumn.ColumnName
dsCustomer.Customer.CityColumn.ColumnName  

I added a Quick watch while debugging on these and got them to be Name,Address and City
It works. Thanks  a lot.   My last question is   why doesnt it just take  Name, Address, City   any idea>
0
 
VBRocksCommented:
You can type it in, like your question demonstrated, this works fine:

    _Database.AddInParameter(InsertCommand, "@Name", DbType.string, "Name", DataRowVersion.Current);

    _Database.AddInParameter(InsertCommand, "@Address ", DbType.string, "Address ", DataRowVersion.Current);

    _Database.AddInParameter(InsertCommand, "@City", DbType.string, "City", DataRowVersion.Current);


0
 
dotnet0824Author Commented:
Hi Vbrocks yep i can do that

dsCustomer.Customer.NameColumn.ColumnName
dsCustomer.Customer.AddressColumn.ColumnName
dsCustomer.Customer.CityColumn.ColumnName  

Can I take these into variables and assign them in my parameters... if so could u please show me
0
 
VBRocksCommented:
Sure, here's an example:

'Create a variable to store the column names
Dim colName as String = ""

colName = dsCustomer.Customer.NameColumn.ColumnName
_Database.AddInParameter(InsertCommand, "@" & colName, DbType.string, colName,  
    DataRowVersion.Current);

colName = dsCustomer.Customer.AddressColumn.ColumnName
_Database.AddInParameter(InsertCommand, "@" & colName, DbType.string, colName,  
    DataRowVersion.Current);

colName = dsCustomer.Customer.CityColumn.ColumnName
_Database.AddInParameter(InsertCommand, "@" & colName, DbType.string, colName,  
    DataRowVersion.Current);


Good luck, and happy programming!



0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 10
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now