Sunday, March 11, 2012

Yes/No field in Stored Procedure (Query)

hi
look at the code below,In command.parameter.add parametername and datatype has to be specified,which is missing in your code.Also mention the direction.in store procedure side give properly the two input parameter.
OleDbCommand cmdGetPointer = new OleDbCommand("Storeprocedurename", cn);
OleDbParameter PointerOutParam = cmdGetPointer.Parameters.Add("@.Pointer", OleDbType.VarBinary, 100);
PointerOutParam.Direction = ParameterDirection.Output;

Thank you. I also had to make one other change. The Stored Procedure returned the values with the names Photos.AlbumID (instead of just AlbumID) and Photos.Caption (instead of just Caption). Now I am getting the data back. But I get another error.
Specified cast is not valid.
Line 126: Photo temp = new Photo(
Line 127: (int)reader["PhotoID"],
Line 128: (int)reader["Photos.AlbumID"],

--BEGIN CODE--
public static List<Photo> GetPhotos(int AlbumID) {
using (OleDbConnection connection = new OleDbConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString))
{
using (OleDbCommand command = new OleDbCommand("GetPhotos", connection))
{
command.CommandType = CommandType.StoredProcedure;

OleDbParameter paramAlbumID = command.Parameters.Add("@.AlbumID", OleDbType.SmallInt);
paramAlbumID.Direction = ParameterDirection.Input;
command.Parameters["@.AlbumID"].Value = AlbumID;

bool filter = !(HttpContext.Current.User.IsInRole("Friends") || HttpContext.Current.User.IsInRole("Administrators"));

OleDbParameter paramIsPublic = command.Parameters.Add("@.IsPublic", OleDbType.Boolean);
paramIsPublic.Direction = ParameterDirection.Input;
command.Parameters["@.IsPublic"].Value = filter;

connection.Open();
List<Photo> list = new List<Photo>();
object obj = command.ExecuteNonQuery();
using (OleDbDataReader reader = command.ExecuteReader()) {
while (reader.Read()) {
Photo temp = new Photo(
(int)reader["PhotoID"],
(int)reader["Photos.AlbumID"],
(string)reader["Photos.Caption"]);
list.Add(temp);
}
}
return list;
}
}
}
--END CODE--
--BEGIN CLASS--
public class Photo {

private int _id;
private int _albumid;
private string _caption;

public int PhotoID { get { return _id; } }
public int AlbumID { get { return _albumid; } }
public string Caption { get { return _caption; } }

public Photo(int id, int albumid, string caption) {
_id = id;
_albumid = albumid;
_caption = caption;
}

}
--END CLASS--


Figured it out. The datatype coming back with Photos.AlbumID was not an int.

Try this linkhttp://forums.asp.net/1043012/ShowPost.aspx

No comments:

Post a Comment