Reputation: 1609
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>
Upvotes: 1
Views: 4955
Reputation: 73311
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
{
MyFirstString = row["FirstString"],
MySecondString = row["SecondString"],
MyThirdString = row["ThridString"]
};
}
}
catch
{
//TODO:log error
return false;
}
finally
{
con.Close();
}
}
public class MyDataStructure
{
public string MyFirstString { get; set; }
public string MySecondString { get; set; }
public string MyThirdString { get; set; }
}
Upvotes: 1
Reputation: 2045
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.
Upvotes: 1
Reputation: 1553
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];
Upvotes: 1
Reputation: 33153
Store the strings in a dataset or a datareader then pass them back to your appropriate tier.
Upvotes: 1