By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,619 Members | 2,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,619 IT Pros & Developers. It's quick & easy.

Binding object method to Dataview.. Not understanding List<GENERIC> ..

P: n/a
Pardon my being a total C# noob.

I'm trying to take apart the dotNet Time Tracker dotNet C# starterkit
sample application and replicate a part of the code.

At a high level, I have a very simple SQL server table and stored
procedure which I have no problems binding from directly from an
ASP.NET control...

Table BOOK
name varchar(50)
phone varchar(10)
ALTER procedure "jcp1"
@name varchar(50) AS
SELECT phone FROM book where name = @name

What I'm trying to do is return a dataview given a textbox value passed
to to the stored procedure, but using C# method and List<like they do
in the sample Ap. Eventough I might not need to, I'm trying to use a
generic List but I'm just not getting it.

I reverted my code to the closest working revision, the below code is
apparently fetching the correct row, however I am unable to see or list
no other other items except LENGTH of the returning field when
configuring the control in the UI . I suspect this is because I'm using
List<stringand not List<whaterver>.

The Dataview source is the method getphone() with a where clause of a
textbox = name
The idea being, if I enter "Jason" in the texbox, the dataview would
list phone with name "Jason"..
I'm sure there is more right than wrong with the below - I just taking
shots in the dark at this point.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Collections;
using System.Text;
/// <summary>
/// Summary description for jcp
/// </summary>
///

public class book
{
private string name;
private string phone;

}

public class jcp
{

private delegate void TGenerateListFromReader<T>(SqlDataReader
returnData, ref List<TtempList);
/**** connection string */
public static string ConnectionString =
ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
public static List<stringGetPhone(string name) {
SqlCommand sqlCmd = new SqlCommand();
AddParamToSQLCmd(sqlCmd, "@name", SqlDbType.NText, 50,
ParameterDirection.Input, name);
SetCommandType(sqlCmd, CommandType.StoredProcedure, "jcp1");
List<stringuserList = new List<string>();
TExecuteReaderCmd<string>(sqlCmd, TGeneratephone<string>, ref
userList);
return (userList);
}

#region SQL HELPER

protected static void AddParamToSQLCmd(SqlCommand sqlCmd,
string paramId,
SqlDbType sqlType,
int paramSize,
ParameterDirection paramDirection,
object paramvalue)
{

if (sqlCmd == null)
throw (new ArgumentNullException("sqlCmd"));
if (paramId == string.Empty)
throw (new ArgumentOutOfRangeException("paramId"));

SqlParameter newSqlParam = new SqlParameter();
newSqlParam.ParameterName = paramId;
newSqlParam.SqlDbType = sqlType;
newSqlParam.Direction = paramDirection;

if (paramSize 0)
newSqlParam.Size = paramSize;

if (paramvalue != null)
newSqlParam.Value = paramvalue;

sqlCmd.Parameters.Add(newSqlParam);
}

private static void SetCommandType(SqlCommand sqlCmd, CommandType
cmdType, string cmdText)
{
sqlCmd.CommandType = cmdType;
sqlCmd.CommandText = cmdText;
}

private static void ExecuteScalarCmd(SqlCommand sqlCmd)
{
if (ConnectionString == string.Empty)
throw (new
ArgumentOutOfRangeException("ConnectionString"));

if (sqlCmd == null)
throw (new ArgumentNullException("sqlCmd"));

using (SqlConnection cn = new SqlConnection(ConnectionString))
{
sqlCmd.Connection = cn;
cn.Open();
sqlCmd.ExecuteScalar();
}
}

private static void TExecuteReaderCmd<T>(SqlCommand sqlCmd,
TGenerateListFromReader<Tgcfr, ref List<TList)
{
if (ConnectionString == string.Empty)
throw (new
ArgumentOutOfRangeException("ConnectionString"));

if (sqlCmd == null)
throw (new ArgumentNullException("sqlCmd"));

using (SqlConnection cn = new SqlConnection(ConnectionString))
{
sqlCmd.Connection = cn;

cn.Open();

gcfr(sqlCmd.ExecuteReader(), ref List);
}
}

private static void TGeneratephone<T>(SqlDataReader returnData, ref
List<stringphoneList)
{
while (returnData.Read())
{
string phone = (string)returnData["phone"];
phoneList.Add(phone);
}
}

#endregion SQL HELPER
}

Thank you for ANY help or information.

Sep 18 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.