Solved

Creating class function to retrieve single-row database content

Posted on 2006-06-08
4
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

695 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