views:

666

answers:

4

I'm trying to Bind 3 textboxes to a Class that retrieves any previously stored records for each of the 3 textboxes. I don't know how to retrieve 3 different values from a class in a Object Orientated perspective. I know how to return single strings, bool, etc vars but not more than 1 at a time.

example of a simple bool returning method I use, how do I adjust it to return 3 separate string variables - Code Snippet:

public static Boolean isQuestionnaireComplete(string strHash)
        {
            SqlConnection con = Sql.getConnection();

            try
            {
                SqlCommand cmd = new SqlCommand("SELECT IsComplete FROM UserDetails WHERE Hash='" + strHash + "' AND IsComplete=1");
                cmd.Connection = con;
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                con.Open();
                da.Fill(dt);
                if (dt.Rows.Count == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch
            {
                //TODO:log error
                return false;
            }
            finally
            {
                con.Close();
            }
        }

ASPX Snippet:

<asp:TextBox runat="server" ID="txt1" Height="200px" Width="600px"></asp:TextBox>
<asp:TextBox runat="server" ID="txt2" Height="200px" Width="600px"></asp:TextBox>
<asp:TextBox runat="server" ID="txt3" Height="200px" Width="600px"></asp:TextBox>
+1  A: 

Store the strings in a dataset or a datareader then pass them back to your appropriate tier.

JonH
+1  A: 

What do you want to return?

public static string[] ReturnStringArrayMethod()
{
     string[] arrStr = new string[3]();
     arrStr[0] = "first string";
     arrStr[1] = "second string";
     arrStr[2] = "third string";
     return arrStr;
}

You can do the same with and Type eg bool[], int[] etc you then access items from the return value using

string val = arrStr[0];
Damian
He's dealing with a database not static data in an array.
JonH
I was just showing how you would build the return values. His code snippet does not show us what he wants to return. He just says "how do I adjust it to return 3 separate string variables". What string variables?
Damian
is it safe to use an Array to store the strings if the strings themselves may be 1000's of characters long? They would be stored in the database as varchars(max) so are fairly long.
Alex
Yes, but a better way to do it would be to map your dt to a class and have the string as properties of the class just like in David Basarab's answer.
Damian
+1  A: 

You would need to return your own structure with the 3 values. You can do an array, but you have to make sure you know which item in the array matches the textbox with a data structure you don't have to remember [0] == the first textbox, and you can use friendly names for more readability and easier to maintain.

public static MyDataStructure isQuestionnaireComplete(string strHash)
{
    SqlConnection con = Sql.getConnection();

    try
    {
        SqlCommand cmd = new SqlCommand("SELECT IsComplete, FirstString, SecondString, ThridString FROM UserDetails WHERE Hash='" + strHash + "' AND IsComplete=1");
        cmd.Connection = con;
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        con.Open();
        da.Fill(dt);

        if (dt.Rows.Count == 0)
        {
            return null;
        }
        else
        {
            // Populate object from data table
            DataRow row = dt.Rows[0];

            retun new MyDataStructure
              {
                MyFristString = row["FirstString"],
                MySecondString = row["SecondString"],
                MyThirdString = row["ThridString"]
              };
        }
    }
    catch
    {
        //TODO:log error
        return false;
    }
    finally
    {
        con.Close();
    }
}

public class MyDataStructure
{
  pubic string MyFirstString { get; set; }
  pubic string MySecondString { get; set; }
  pubic string MyThirdString { get; set; }
}
David Basarab
Thanks, I like the way this is done, going to give it a whirl now. Also liked the misspellings of public :)
Alex
Just tried it and got a little issue with each of the Gets and Sets:Error 1 'Package.Sql.Sql.Comments.MyFirstComment.get' must declare a body because it is not marked abstract or extern
Alex
The public string MyFirstString { get; set; } is for .NET 3.5, if you are using .NET 2.0 or .NET 1.1 you have do declare a private member, and then doing the get and set.
David Basarab
Yeah its 2.0 I am using. Do I need to code the gets and sets within the public class MyDataStructure {} or externally? Thanks again
Alex
+1  A: 

I want to preface this with I don't quite know what you are trying to do. Is the questionaire always 3 questions? or is the size dynamic?

I would use a Repeater if the size is dynamic (this is the example I provide) or I would use a FormView if the size is fixed at 3.

Use the following code to access the database:

namespace BLL
{
    using System;
    using System.Data;
    using System.Data.SqlClient;
    [System.ComponentModel.DataObject]
    public class QuestionnaireDataObject
    {
        public static DataTable isQuestionnaireComplete(string strHash1, string strHash2, string strHash3)
        {
            DataTable dt = new DataTable();
            using (SqlConnection con = Sql.getConnection())
            {
                SqlCommand cmd =
                    new SqlCommand(
                        String.Format("SELECT Hash, IsComplete FROM UserDetails WHERE Hash IN ('{0}', '{1}', '{2}')",
                                      strHash1, strHash2, strHash3));
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                da.Dispose();
            }

            return dt;
        }
    }
}

In your code behind you will need something like the following:

<asp:Repeater id="Repeater1" runat="server" datasourceid="ObjectDataSource1">
    <ItemTemplate>
        <asp:CheckBox id="CheckBox1" runat="server" checked='<%# Bind("IsComplete") %>' /><asp:TextBox
            id="TextBox1" runat="server" text='<%# Bind("Hash") %>'></asp:TextBox>
    </ItemTemplate>
</asp:Repeater>
<asp:ObjectDataSource id="ObjectDataSource1" runat="server" selectmethod="isQuestionnaireComplete"
    typename="Bll.QuestionnaireDataObject" onselecting="ObjectDataSource1_Selecting"></asp:ObjectDataSource>

And then in your code behind you will need the selecting method to set the parameters for the query:

protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
    e.InputParameters["strHash1"] = strHash1Value;
    e.InputParameters["strHash2"] = strHash2Value;
    e.InputParameters["strHash3"] = strHash3Value;
}

If you explain the situation a little more (What all is needed to display, and the database logic involved) I will refine my answer to closer match your needs. I hope this is a good starting point.

Chris