Solved

Creating class function to retrieve single-row database content

Posted on 2006-06-08
4
219 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 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