?
Solved

How to pull one record and store value to a variable instead of a grid?

Posted on 2011-05-03
4
Medium Priority
?
247 Views
Last Modified: 2012-05-11
Hi,

I'm using SQL 2005, with Visual Studio 2005/ASP 3.5. I'm trying to make my first Sql server connection, to store the values into a string variable. But unsuccessful. I'd appreciated if some one could tell me what I'm doing wrong or is there a better way. I'm use to just dumping the table into grid, but now I need it in a variable instead.

string sqlCheck = "SELECT lastrundate FROM CheckStatus WHERE sitename = 'Pack'";

string = "Data Source=169.00.00.1;Initial Catalog=DatabaseName;User Id=useridtest;Password=passwordtest";

            // Establish SqlServer Connection
            using (SqlConnection conn = new SqlConnection(connectionStr))
            {
                conn.Open();

                using (SqlDataAdapter a = new SqlDataAdapter(sqlCheck,conn))
                {
                    DataTable t = new DataTable();
                    a.Fill(t);

                    if (t.DataSet.Tables.Count > 0)
                    {
                        string datavalue;
                        // How do I store the value from this one record into a variable?

                    }

                }

                conn.Close();
            }
0
Comment
Question by:mlong219
4 Comments
 
LVL 2

Expert Comment

by:Matt_Lebrao
ID: 35639849
I am not sure if I unsderstood what you want to do, but it looks like you want to sore the informations of a recorde, that means, a row of a datatable.... If that's it, then you can create a struct with the same type of datas you have int the row, for example...

struct dataRecord
{
    int ID;
    string Name;
    DateTime SubscriptionDate;
}

then you use it to store what you in one variable of the type of your struct, to store every field of the record.

I hope this helps.
0
 
LVL 20

Accepted Solution

by:
informaniac earned 1400 total points
ID: 35650147
string strFirstRowColumn  = Ds.Tables[0].Rows[0][0].ToString();
0
 
LVL 9

Assisted Solution

by:sarabhai
sarabhai earned 600 total points
ID: 35665962
all columns or particular column value need to be store?

string datavalue;
datavalue = DataSet.Tables[0].Rows[0][0].ToString();
0
 

Author Comment

by:mlong219
ID: 35667765
@informaniac:

I tried your solution, and I get the following error message...
Object reference not set to an instance of an object. Error message.

@Matt_Lebrao:
I need to store the value from one field into a variable.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question