?
Solved

Creating class function to retrieve single-row database content

Posted on 2006-06-08
4
Medium Priority
?
222 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 2000 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

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

743 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