Solved

Creating class function to retrieve single-row database content

Posted on 2006-06-08
4
214 Views
Last Modified: 2012-05-05
I have this function:
public static DataSet getPageContentByID(int tableContentID)
    {
        SqlConnection SQLConn = new SqlConnection(MarComConnectionString());

        SqlCommand sqlCmd = new SqlCommand("spx_FetchOrrtaxWebsiteContentText", SQLConn);
        sqlCmd.CommandType = CommandType.StoredProcedure;

        SqlDataAdapter sqlAdp = new SqlDataAdapter(sqlCmd);

        sqlCmd.Parameters.Add("@output_id", SqlDbType.Int);
        sqlCmd.Parameters["@output_id"].Value = tableContentID;

        DataSet dtsData = new DataSet();
       
        try
        {
            SQLConn.Open();
            sqlAdp.Fill(dtsData);
        }
        finally
        {
            SQLConn.Close();
        }

        return dtsData;
    }

As you can see, it returns a dataset. What I would like to do is instead just have it return 1 piece of data, as the table in question only has 1 column I'm interested in, 1 row at a time (the sproc called, in fact, only outputs 1 row)

this would be easy in VB, as I could set the function as a string, but since I'm required to do this in C#, I'm not sure what to do.
0
Comment
Question by:Paul Kahl
  • 2
4 Comments
 
LVL 4

Expert Comment

by:CaldNT
ID: 16865336
Use:

dtsData.Tables[0].Rows[0]


This will return a System.Data.DataRow with the first row from the first table in your DataSet. If the DataSet has only one table with one row this will be all the data in it.

Good luck!
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 16865460
Or if you just want one column:

public static getPageContentByID(int tableContentID)
{
   ...

  return dtsData.Tables[0].Rosw[0][0].ToString();

}
0
 
LVL 25

Accepted Solution

by:
dstanley9 earned 500 total points
ID: 16865463
Oops.. typo:

public static getPageContentByID(int tableContentID)
{
   ...

  return dtsData.Tables[0].Rows[0][0].ToString();

}
0
 
LVL 3

Author Comment

by:Paul Kahl
ID: 16865569
Final function:

public static string getPageContentByID(int tableContentID)
    {
        SqlConnection SQLConn = new SqlConnection(MarComConnectionString());

        SqlCommand sqlCmd = new SqlCommand("spx_FetchOrrtaxWebsiteContentText", SQLConn);
        sqlCmd.CommandType = CommandType.StoredProcedure;

        SqlDataAdapter sqlAdp = new SqlDataAdapter(sqlCmd);

        sqlCmd.Parameters.Add("@output_id", SqlDbType.Int);
        sqlCmd.Parameters["@output_id"].Value = tableContentID;

        DataSet dtsData = new DataSet();

        try
        {
            SQLConn.Open();
            sqlAdp.Fill(dtsData);
        }
        finally
        {
            SQLConn.Close();
        }

        return dtsData.Tables[0].Rows[0][0].ToString();
    }

I changed the function to a String base instead of a DataSet base, since that's the true goal of it, but otherwise used the code you suggested. It worked perfectly! Thank you!
0

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

8 Experts available now in Live!

Get 1:1 Help Now