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

Posted on 2011-05-03
Last Modified: 2012-05-11

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=;Initial Catalog=DatabaseName;User Id=useridtest;Password=passwordtest";

            // Establish SqlServer Connection
            using (SqlConnection conn = new SqlConnection(connectionStr))

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

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



Question by:mlong219
    LVL 2

    Expert Comment

    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.
    LVL 20

    Accepted Solution

    string strFirstRowColumn  = Ds.Tables[0].Rows[0][0].ToString();
    LVL 9

    Assisted Solution

    all columns or particular column value need to be store?

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

    Author Comment


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

    I need to store the value from one field into a variable.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
    Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
    Viewers will learn how the fundamental information of how to create a table.

    732 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now