954,593 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help passing database column value to a string variable.

I have a data table adapter that returns a single column and I would like to assign the column to a string variable. Can anyone help me on this. it seems very simple but when I use this syntax

GpsitDataTableAdpater provider = new GpsitDataTableAdapter();
string CarrierString = Convert.ToString(provider.getProviderByDevUID(1789));

it returns the table title "DEVICE". when I test the select statement, I get the right column but, when I used in the context above, I get the table's title.

justapimp
Junior Poster in Training
87 posts since Sep 2007
Reputation Points: 11
Solved Threads: 1
 

Hi,
This is how i do it for SQL databases

Dim SQLda as New SQLDataAdapter
Dim SQLTable as new Data.DataTable
Dim SQLRow as Data.DataRow
Dim Value as String

SQLda = New SQLDataAdapter(<em>SQL_SELECT_STATEMENT</em>, <em>CONNECTION_STRING</em>)
SQLDA.Fill(SQLTable)

For Each SQLRow in SQLTable.Rows
    Value = SQLRow(<em>Column_Name</em>).toString
Next
ptaylor965
Junior Poster
170 posts since Oct 2006
Reputation Points: 16
Solved Threads: 19
 

Is there another implementation that works better with C#'s table data adapters. I tried your recommendation and it did not work for me.

justapimp
Junior Poster in Training
87 posts since Sep 2007
Reputation Points: 11
Solved Threads: 1
 

Are you needing to keep the values in a data table or are you pulling them to use as an array? What exactly are you needing, if you can post it!

SheSaidImaPregy
Veteran Poster
1,080 posts since Sep 2007
Reputation Points: 43
Solved Threads: 68
 

I want to use them as an array that I can iterate through and Possibly produce different output. For example let say my my database content looks something like this.

Column StartDateTime = 2007-12-12 11:00:00:000
Column EndDateTime = 2007-12-17 04:00:00:000
Column TimeZone = 3


I would like to evaluate the value for the TimeZone and subtract or add the value to the StartDateTime and EndDateTime.

If I can load the values into an array, I can use a foreach statement to do the mathematical operation and return and return the datatable to be binded to a formview later.


Thank you for responding.

justapimp
Junior Poster in Training
87 posts since Sep 2007
Reputation Points: 11
Solved Threads: 1
 

well I am not a C# man, infact I have never used it besides for helping those with problems on this and other forums. Sorry for the late response. I can do it in VB and use www.codechanger.com to change it to C#. Below is the C# code I would use, and use a reader, not a datatable unelss you need updating. It's quicker and more efficient:

void Page_Load(Object s, EventArgs e) {
    SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["con"]);
    SqlCommand cmd = new SqlCommand("SELECT COMMAND OR STORED PROCEDURE NAME",con);
    //Use below line if stored procedure
    //cmd.CommandType = CommandType.StoredProcedure;

    con.Open();
    
    ArrayList arr = new ArrayList();
    SqlDataReader dr = cmd.ExecuteReader();
        
    while(dr.Read()) {
        object[] values = new object[dr.FieldCount];
        dr.GetValues(values);
        arr.Add(values);
    }

    dr.Close();
    con.Close();
}
SheSaidImaPregy
Veteran Poster
1,080 posts since Sep 2007
Reputation Points: 43
Solved Threads: 68
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You