S
Singularity.co.uk
Hi
I want to call a stored procedure from my page and the code I am using is:
SqlConnection sqlConn = new SqlConnection();
sqlConn.ConnectionString = DBConnection;
SqlCommand cmd = new SqlCommand("spGetProjects", sqlConn);
//Set up the command
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@UserID", SqlDbType.BigInt, 5);
// Test Stored Procedure with hard coded value
cmd.Parameters["@UserID"].Value = 12;
try
{
sqlConn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
this.cboProjects.DataValueField = dr["ProjectId"].ToString();
this.cboProjects.DataTextField = dr["ProjectCode"].ToString();
}
//Close the DataReader
dr.Close();
}
catch(SqlException ex)
{
}
finally
{
sqlConn.Close();
}
However nothing is returned, even though when I run the stored procedure
through the SQL Query Analyzer 4 records are returned. If I use the code
below, then the drop down is populated, but I want to use a stored procedure
instead of coding the SQL in the page:
SqlConnection sqlConn = new SqlConnection();
sqlConn.ConnectionString = DBConnection;
SqlDataAdapter dsc = new SqlDataAdapter("SELECT Projects.ProjectId,
Projects.ProjectCode FROM Projects ORDER BY ProjectCode", sqlConn);
DataSet ds = new DataSet();
dsc.Fill(ds, "Projects");
cboProjects.DataSource = ds.Tables["Projects"].DefaultView;
cboProjects.DataValueField = "ProjectId";
cboProjects.DataTextField = "ProjectCode";
cboProjects.DataBind();
Any and all help is greatly appreciated.
Thanks
Brendan
I want to call a stored procedure from my page and the code I am using is:
SqlConnection sqlConn = new SqlConnection();
sqlConn.ConnectionString = DBConnection;
SqlCommand cmd = new SqlCommand("spGetProjects", sqlConn);
//Set up the command
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@UserID", SqlDbType.BigInt, 5);
// Test Stored Procedure with hard coded value
cmd.Parameters["@UserID"].Value = 12;
try
{
sqlConn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
this.cboProjects.DataValueField = dr["ProjectId"].ToString();
this.cboProjects.DataTextField = dr["ProjectCode"].ToString();
}
//Close the DataReader
dr.Close();
}
catch(SqlException ex)
{
}
finally
{
sqlConn.Close();
}
However nothing is returned, even though when I run the stored procedure
through the SQL Query Analyzer 4 records are returned. If I use the code
below, then the drop down is populated, but I want to use a stored procedure
instead of coding the SQL in the page:
SqlConnection sqlConn = new SqlConnection();
sqlConn.ConnectionString = DBConnection;
SqlDataAdapter dsc = new SqlDataAdapter("SELECT Projects.ProjectId,
Projects.ProjectCode FROM Projects ORDER BY ProjectCode", sqlConn);
DataSet ds = new DataSet();
dsc.Fill(ds, "Projects");
cboProjects.DataSource = ds.Tables["Projects"].DefaultView;
cboProjects.DataValueField = "ProjectId";
cboProjects.DataTextField = "ProjectCode";
cboProjects.DataBind();
Any and all help is greatly appreciated.
Thanks
Brendan