Output Parameters null when using SqlCommand.ExecuteReader()...?

M

mo

should I be able to use output parameters when calling
SqlCommand.ExecuteReader()???

SqlConnection cn = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand("StoredProcedureName", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@param1", SqlDbType.Int).Value = _param1Value;
cmd.Parameters.Add("@param2", SqlDbType.VarChar, 50).Value = _param2Value
cmd.Parameters.Add("@param3", SqlDbType.VarChar, 50).Value = _param3Value
cmd.Parameters.Add("@out_param", SqlDbType.Int);
cmd.Parameters["@out_param"].Direction = ParameterDirection.Output;

try
{
SqlDataReader dr = cmd.ExecuteReader();
_out_value = (int)cmd.Parameters["@out_value"].Value;
}
catch ...etc...

when doing this, the value of cmd.Parameters["@out_param"] is always null...


thx,
mo
 
W

William Ryan eMVP

Looks like a pspelling issue, one references @out_param and the outher
@out_value. Also, make sure the datareader has had it's .read method called
and is closed.

HTH,

Bill
 
M

mo

oops, sorry about the typo... thanks.

I'm using the SqlDataReader for a paging datagrid. All the examples I've
seen say that you must query the database to get a value for
VirtualItemCount and then use the ExecuteReader() to get your result set. I
figured I could use an output parameter and only query the db once this
way.... I can't get output parameters to return any values with
ExecuteReader() no matter what I do, so I wonder if it is possible? The
stored procedure works great if I us a select @@rowcount in and return the
results in a second result set, then get the virtual item count from the
first result set then call NextResult() to populate the datagrid

Anyway... here's the code, if you see why it won't work please let me know
Thanks!

===============================

// this doesn't work...
private void dgCustomers_DataBind()
{
SqlConnection cn = new
SqlConnection(ConfigurationSettings.AppSettings["NorthwindDB"]);
SqlCommand cmd = new SqlCommand("usp_CustomersSelect", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@pageSize", SqlDbType.Int).Value =
dgCustomers.PageSize;
cmd.Parameters.Add("@rowStartIndex", SqlDbType.Int).Value =
(dgCustomers.CurrentPageIndex * dgCustomers.PageSize);
cmd.Parameters.Add("@virtualItemCount", SqlDbType.Int).Direction =
ParameterDirection.Output;

try
{
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
dgCustomers.VirtualItemCount =
(int)cmd.Parameters["@virtualItemCount"].Value;
dgCustomers.DataSource = dr;
dgCustomers.DataBind();
}
dr.Close();
lblVirtualItemCount.Text = String.Format("Total results: {0}",
dgCustomers.VirtualItemCount);
}
catch (Exception ex)
{
errMessage.InnerText = ex.ToString();
errMessage.Visible = true;
}
finally
{
cn.Dispose();
}
}

===================================

CREATE procedure usp_CustomersSelect
@pageSize int,
@rowStartIndex int,
@virtualItemCount int output
as

declare @rowEndIndex int
set @rowEndIndex = @rowStartIndex + @pageSize - 1

select identity(int,0,1) as rowIndex,
CustomerID, CompanyName, ContactName,
ContactTitle, Address, City, Region,
PostalCode, Country, Phone, Fax
into #TEMP_NWindCustomers
from Customers order by CompanyName

set @virtualItemCount = @@rowcount

select rowIndex, CustomerID, CompanyName, ContactName,
ContactTitle, Address, City, Region, PostalCode,
Country, Phone, Fax
from #TEMP_NWindCustomers
where rowIndex between @rowStartIndex and @rowEndIndex order by 1

====================================================
This works fine:

private void dgCustomers_DataBind()
{
SqlConnection cn = new
SqlConnection(ConfigurationSettings.AppSettings["NorthwindDB"]);
SqlCommand cmd = new SqlCommand("usp_CustomersSelect", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@pageSize", SqlDbType.Int).Value =
dgCustomers.PageSize;
cmd.Parameters.Add("@rowStartIndex", SqlDbType.Int).Value =
(dgCustomers.CurrentPageIndex * dgCustomers.PageSize);

try
{
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
dgCustomers.VirtualItemCount = (int)dr["virtualItemCount"];
}

while (dr.NextResult())
{
dgCustomers.DataSource = dr;
dgCustomers.DataBind();
}

dr.Close();
lblVirtualItemCount.Text = String.Format("Total results: {0}",
dgCustomers.VirtualItemCount);
}
catch (Exception ex)
{
errMessage.InnerText = ex.ToString();
errMessage.Visible = true;
}
finally
{
cn.Dispose();
}
}

private void dgCustomers_PageIndexChanged(object source,
DataGridPageChangedEventArgs e)
{
dgCustomers.CurrentPageIndex = e.NewPageIndex;
dgCustomers_DataBind();
}

CREATE procedure usp_CustomersSelect
@pageSize int,
@rowStartIndex int
as

declare @rowEndIndex int
set @rowEndIndex = @rowStartIndex + @pageSize - 1

select identity(int,0,1) as rowIndex,
CustomerID, CompanyName, ContactName,
ContactTitle, Address, City, Region,
PostalCode, Country, Phone, Fax
into #TEMP_NWindCustomers
from Customers order by CompanyName

select virtualItemCount = @@rowcount

select rowIndex, CustomerID, CompanyName, ContactName,
ContactTitle, Address, City, Region, PostalCode,
Country, Phone, Fax
from #TEMP_NWindCustomers
where rowIndex between @rowStartIndex and @rowEndIndex order by 1






William Ryan eMVP said:
Looks like a pspelling issue, one references @out_param and the outher
@out_value. Also, make sure the datareader has had it's .read method called
and is closed.

HTH,

Bill
mo said:
should I be able to use output parameters when calling
SqlCommand.ExecuteReader()???

SqlConnection cn = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand("StoredProcedureName", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@param1", SqlDbType.Int).Value = _param1Value;
cmd.Parameters.Add("@param2", SqlDbType.VarChar, 50).Value = _param2Value
cmd.Parameters.Add("@param3", SqlDbType.VarChar, 50).Value = _param3Value.
cmd.Parameters.Add("@out_param", SqlDbType.Int);
cmd.Parameters["@out_param"].Direction = ParameterDirection.Output;

try
{
SqlDataReader dr = cmd.ExecuteReader();
_out_value = (int)cmd.Parameters["@out_value"].Value;
}
catch ...etc...

when doing this, the value of cmd.Parameters["@out_param"] is always null...


thx,
mo



-----------------------------------------------
"Too much football without a helmet?"

Al Pacino
Scent of a Woman
 
D

DalePres

Output and return parameters aren't available until you close the reader.

Dale

mo said:
oops, sorry about the typo... thanks.

I'm using the SqlDataReader for a paging datagrid. All the examples I've
seen say that you must query the database to get a value for
VirtualItemCount and then use the ExecuteReader() to get your result set. I
figured I could use an output parameter and only query the db once this
way.... I can't get output parameters to return any values with
ExecuteReader() no matter what I do, so I wonder if it is possible? The
stored procedure works great if I us a select @@rowcount in and return the
results in a second result set, then get the virtual item count from the
first result set then call NextResult() to populate the datagrid

Anyway... here's the code, if you see why it won't work please let me know
Thanks!

===============================

// this doesn't work...
private void dgCustomers_DataBind()
{
SqlConnection cn = new
SqlConnection(ConfigurationSettings.AppSettings["NorthwindDB"]);
SqlCommand cmd = new SqlCommand("usp_CustomersSelect", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@pageSize", SqlDbType.Int).Value =
dgCustomers.PageSize;
cmd.Parameters.Add("@rowStartIndex", SqlDbType.Int).Value =
(dgCustomers.CurrentPageIndex * dgCustomers.PageSize);
cmd.Parameters.Add("@virtualItemCount", SqlDbType.Int).Direction =
ParameterDirection.Output;

try
{
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
dgCustomers.VirtualItemCount =
(int)cmd.Parameters["@virtualItemCount"].Value;
dgCustomers.DataSource = dr;
dgCustomers.DataBind();
}
dr.Close();
lblVirtualItemCount.Text = String.Format("Total results: {0}",
dgCustomers.VirtualItemCount);
}
catch (Exception ex)
{
errMessage.InnerText = ex.ToString();
errMessage.Visible = true;
}
finally
{
cn.Dispose();
}
}

===================================

CREATE procedure usp_CustomersSelect
@pageSize int,
@rowStartIndex int,
@virtualItemCount int output
as

declare @rowEndIndex int
set @rowEndIndex = @rowStartIndex + @pageSize - 1

select identity(int,0,1) as rowIndex,
CustomerID, CompanyName, ContactName,
ContactTitle, Address, City, Region,
PostalCode, Country, Phone, Fax
into #TEMP_NWindCustomers
from Customers order by CompanyName

set @virtualItemCount = @@rowcount

select rowIndex, CustomerID, CompanyName, ContactName,
ContactTitle, Address, City, Region, PostalCode,
Country, Phone, Fax
from #TEMP_NWindCustomers
where rowIndex between @rowStartIndex and @rowEndIndex order by 1

====================================================
This works fine:

private void dgCustomers_DataBind()
{
SqlConnection cn = new
SqlConnection(ConfigurationSettings.AppSettings["NorthwindDB"]);
SqlCommand cmd = new SqlCommand("usp_CustomersSelect", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@pageSize", SqlDbType.Int).Value =
dgCustomers.PageSize;
cmd.Parameters.Add("@rowStartIndex", SqlDbType.Int).Value =
(dgCustomers.CurrentPageIndex * dgCustomers.PageSize);

try
{
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
dgCustomers.VirtualItemCount = (int)dr["virtualItemCount"];
}

while (dr.NextResult())
{
dgCustomers.DataSource = dr;
dgCustomers.DataBind();
}

dr.Close();
lblVirtualItemCount.Text = String.Format("Total results: {0}",
dgCustomers.VirtualItemCount);
}
catch (Exception ex)
{
errMessage.InnerText = ex.ToString();
errMessage.Visible = true;
}
finally
{
cn.Dispose();
}
}

private void dgCustomers_PageIndexChanged(object source,
DataGridPageChangedEventArgs e)
{
dgCustomers.CurrentPageIndex = e.NewPageIndex;
dgCustomers_DataBind();
}

CREATE procedure usp_CustomersSelect
@pageSize int,
@rowStartIndex int
as

declare @rowEndIndex int
set @rowEndIndex = @rowStartIndex + @pageSize - 1

select identity(int,0,1) as rowIndex,
CustomerID, CompanyName, ContactName,
ContactTitle, Address, City, Region,
PostalCode, Country, Phone, Fax
into #TEMP_NWindCustomers
from Customers order by CompanyName

select virtualItemCount = @@rowcount

select rowIndex, CustomerID, CompanyName, ContactName,
ContactTitle, Address, City, Region, PostalCode,
Country, Phone, Fax
from #TEMP_NWindCustomers
where rowIndex between @rowStartIndex and @rowEndIndex order by 1






William Ryan eMVP said:
Looks like a pspelling issue, one references @out_param and the outher
@out_value. Also, make sure the datareader has had it's .read method called
and is closed.

HTH,

Bill
mo said:
should I be able to use output parameters when calling
SqlCommand.ExecuteReader()???

SqlConnection cn = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand("StoredProcedureName", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@param1", SqlDbType.Int).Value = _param1Value;
cmd.Parameters.Add("@param2", SqlDbType.VarChar, 50).Value = _param2Value
cmd.Parameters.Add("@param3", SqlDbType.VarChar, 50).Value = _param3Value.
cmd.Parameters.Add("@out_param", SqlDbType.Int);
cmd.Parameters["@out_param"].Direction = ParameterDirection.Output;

try
{
SqlDataReader dr = cmd.ExecuteReader();
_out_value = (int)cmd.Parameters["@out_value"].Value;
}
catch ...etc...

when doing this, the value of cmd.Parameters["@out_param"] is always null...


thx,
mo



-----------------------------------------------
"Too much football without a helmet?"

Al Pacino
Scent of a Woman
 
W

William Ryan eMVP

Mo:

Are you using this for paging in an ASP.NET web app by any chance? If so,
let me send you some code to handle your paging without having to reopen
connections...


mo said:
oops, sorry about the typo... thanks.

I'm using the SqlDataReader for a paging datagrid. All the examples I've
seen say that you must query the database to get a value for
VirtualItemCount and then use the ExecuteReader() to get your result set. I
figured I could use an output parameter and only query the db once this
way.... I can't get output parameters to return any values with
ExecuteReader() no matter what I do, so I wonder if it is possible? The
stored procedure works great if I us a select @@rowcount in and return the
results in a second result set, then get the virtual item count from the
first result set then call NextResult() to populate the datagrid

Anyway... here's the code, if you see why it won't work please let me know
Thanks!

===============================

// this doesn't work...
private void dgCustomers_DataBind()
{
SqlConnection cn = new
SqlConnection(ConfigurationSettings.AppSettings["NorthwindDB"]);
SqlCommand cmd = new SqlCommand("usp_CustomersSelect", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@pageSize", SqlDbType.Int).Value =
dgCustomers.PageSize;
cmd.Parameters.Add("@rowStartIndex", SqlDbType.Int).Value =
(dgCustomers.CurrentPageIndex * dgCustomers.PageSize);
cmd.Parameters.Add("@virtualItemCount", SqlDbType.Int).Direction =
ParameterDirection.Output;

try
{
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
dgCustomers.VirtualItemCount =
(int)cmd.Parameters["@virtualItemCount"].Value;
dgCustomers.DataSource = dr;
dgCustomers.DataBind();
}
dr.Close();
lblVirtualItemCount.Text = String.Format("Total results: {0}",
dgCustomers.VirtualItemCount);
}
catch (Exception ex)
{
errMessage.InnerText = ex.ToString();
errMessage.Visible = true;
}
finally
{
cn.Dispose();
}
}

===================================

CREATE procedure usp_CustomersSelect
@pageSize int,
@rowStartIndex int,
@virtualItemCount int output
as

declare @rowEndIndex int
set @rowEndIndex = @rowStartIndex + @pageSize - 1

select identity(int,0,1) as rowIndex,
CustomerID, CompanyName, ContactName,
ContactTitle, Address, City, Region,
PostalCode, Country, Phone, Fax
into #TEMP_NWindCustomers
from Customers order by CompanyName

set @virtualItemCount = @@rowcount

select rowIndex, CustomerID, CompanyName, ContactName,
ContactTitle, Address, City, Region, PostalCode,
Country, Phone, Fax
from #TEMP_NWindCustomers
where rowIndex between @rowStartIndex and @rowEndIndex order by 1

====================================================
This works fine:

private void dgCustomers_DataBind()
{
SqlConnection cn = new
SqlConnection(ConfigurationSettings.AppSettings["NorthwindDB"]);
SqlCommand cmd = new SqlCommand("usp_CustomersSelect", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@pageSize", SqlDbType.Int).Value =
dgCustomers.PageSize;
cmd.Parameters.Add("@rowStartIndex", SqlDbType.Int).Value =
(dgCustomers.CurrentPageIndex * dgCustomers.PageSize);

try
{
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
dgCustomers.VirtualItemCount = (int)dr["virtualItemCount"];
}

while (dr.NextResult())
{
dgCustomers.DataSource = dr;
dgCustomers.DataBind();
}

dr.Close();
lblVirtualItemCount.Text = String.Format("Total results: {0}",
dgCustomers.VirtualItemCount);
}
catch (Exception ex)
{
errMessage.InnerText = ex.ToString();
errMessage.Visible = true;
}
finally
{
cn.Dispose();
}
}

private void dgCustomers_PageIndexChanged(object source,
DataGridPageChangedEventArgs e)
{
dgCustomers.CurrentPageIndex = e.NewPageIndex;
dgCustomers_DataBind();
}

CREATE procedure usp_CustomersSelect
@pageSize int,
@rowStartIndex int
as

declare @rowEndIndex int
set @rowEndIndex = @rowStartIndex + @pageSize - 1

select identity(int,0,1) as rowIndex,
CustomerID, CompanyName, ContactName,
ContactTitle, Address, City, Region,
PostalCode, Country, Phone, Fax
into #TEMP_NWindCustomers
from Customers order by CompanyName

select virtualItemCount = @@rowcount

select rowIndex, CustomerID, CompanyName, ContactName,
ContactTitle, Address, City, Region, PostalCode,
Country, Phone, Fax
from #TEMP_NWindCustomers
where rowIndex between @rowStartIndex and @rowEndIndex order by 1






William Ryan eMVP said:
Looks like a pspelling issue, one references @out_param and the outher
@out_value. Also, make sure the datareader has had it's .read method called
and is closed.

HTH,

Bill
mo said:
should I be able to use output parameters when calling
SqlCommand.ExecuteReader()???

SqlConnection cn = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand("StoredProcedureName", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@param1", SqlDbType.Int).Value = _param1Value;
cmd.Parameters.Add("@param2", SqlDbType.VarChar, 50).Value = _param2Value
cmd.Parameters.Add("@param3", SqlDbType.VarChar, 50).Value = _param3Value.
cmd.Parameters.Add("@out_param", SqlDbType.Int);
cmd.Parameters["@out_param"].Direction = ParameterDirection.Output;

try
{
SqlDataReader dr = cmd.ExecuteReader();
_out_value = (int)cmd.Parameters["@out_value"].Value;
}
catch ...etc...

when doing this, the value of cmd.Parameters["@out_param"] is always null...


thx,
mo



-----------------------------------------------
"Too much football without a helmet?"

Al Pacino
Scent of a Woman
 
M

mo

Here you go: http://www.moweb.com/examples/datagrid.aspx
There's a link to download the .aspx and codebehind.

thx,
mo

William Ryan eMVP said:
Mo:

Are you using this for paging in an ASP.NET web app by any chance? If so,
let me send you some code to handle your paging without having to reopen
connections...


mo said:
oops, sorry about the typo... thanks.

I'm using the SqlDataReader for a paging datagrid. All the examples I've
seen say that you must query the database to get a value for
VirtualItemCount and then use the ExecuteReader() to get your result
set.
I
figured I could use an output parameter and only query the db once this
way.... I can't get output parameters to return any values with
ExecuteReader() no matter what I do, so I wonder if it is possible? The
stored procedure works great if I us a select @@rowcount in and return the
results in a second result set, then get the virtual item count from the
first result set then call NextResult() to populate the datagrid

Anyway... here's the code, if you see why it won't work please let me know
Thanks!

===============================

// this doesn't work...
private void dgCustomers_DataBind()
{
SqlConnection cn = new
SqlConnection(ConfigurationSettings.AppSettings["NorthwindDB"]);
SqlCommand cmd = new SqlCommand("usp_CustomersSelect", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@pageSize", SqlDbType.Int).Value =
dgCustomers.PageSize;
cmd.Parameters.Add("@rowStartIndex", SqlDbType.Int).Value =
(dgCustomers.CurrentPageIndex * dgCustomers.PageSize);
cmd.Parameters.Add("@virtualItemCount", SqlDbType.Int).Direction =
ParameterDirection.Output;

try
{
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
dgCustomers.VirtualItemCount =
(int)cmd.Parameters["@virtualItemCount"].Value;
dgCustomers.DataSource = dr;
dgCustomers.DataBind();
}
dr.Close();
lblVirtualItemCount.Text = String.Format("Total results: {0}",
dgCustomers.VirtualItemCount);
}
catch (Exception ex)
{
errMessage.InnerText = ex.ToString();
errMessage.Visible = true;
}
finally
{
cn.Dispose();
}
}

===================================

CREATE procedure usp_CustomersSelect
@pageSize int,
@rowStartIndex int,
@virtualItemCount int output
as

declare @rowEndIndex int
set @rowEndIndex = @rowStartIndex + @pageSize - 1

select identity(int,0,1) as rowIndex,
CustomerID, CompanyName, ContactName,
ContactTitle, Address, City, Region,
PostalCode, Country, Phone, Fax
into #TEMP_NWindCustomers
from Customers order by CompanyName

set @virtualItemCount = @@rowcount

select rowIndex, CustomerID, CompanyName, ContactName,
ContactTitle, Address, City, Region, PostalCode,
Country, Phone, Fax
from #TEMP_NWindCustomers
where rowIndex between @rowStartIndex and @rowEndIndex order by 1

====================================================
This works fine:

private void dgCustomers_DataBind()
{
SqlConnection cn = new
SqlConnection(ConfigurationSettings.AppSettings["NorthwindDB"]);
SqlCommand cmd = new SqlCommand("usp_CustomersSelect", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@pageSize", SqlDbType.Int).Value =
dgCustomers.PageSize;
cmd.Parameters.Add("@rowStartIndex", SqlDbType.Int).Value =
(dgCustomers.CurrentPageIndex * dgCustomers.PageSize);

try
{
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
dgCustomers.VirtualItemCount = (int)dr["virtualItemCount"];
}

while (dr.NextResult())
{
dgCustomers.DataSource = dr;
dgCustomers.DataBind();
}

dr.Close();
lblVirtualItemCount.Text = String.Format("Total results: {0}",
dgCustomers.VirtualItemCount);
}
catch (Exception ex)
{
errMessage.InnerText = ex.ToString();
errMessage.Visible = true;
}
finally
{
cn.Dispose();
}
}

private void dgCustomers_PageIndexChanged(object source,
DataGridPageChangedEventArgs e)
{
dgCustomers.CurrentPageIndex = e.NewPageIndex;
dgCustomers_DataBind();
}

CREATE procedure usp_CustomersSelect
@pageSize int,
@rowStartIndex int
as

declare @rowEndIndex int
set @rowEndIndex = @rowStartIndex + @pageSize - 1

select identity(int,0,1) as rowIndex,
CustomerID, CompanyName, ContactName,
ContactTitle, Address, City, Region,
PostalCode, Country, Phone, Fax
into #TEMP_NWindCustomers
from Customers order by CompanyName

select virtualItemCount = @@rowcount

select rowIndex, CustomerID, CompanyName, ContactName,
ContactTitle, Address, City, Region, PostalCode,
Country, Phone, Fax
from #TEMP_NWindCustomers
where rowIndex between @rowStartIndex and @rowEndIndex order by 1






William Ryan eMVP said:
Looks like a pspelling issue, one references @out_param and the outher
@out_value. Also, make sure the datareader has had it's .read method called
and is closed.

HTH,

Bill
should I be able to use output parameters when calling
SqlCommand.ExecuteReader()???

SqlConnection cn = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand("StoredProcedureName", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@param1", SqlDbType.Int).Value = _param1Value;
cmd.Parameters.Add("@param2", SqlDbType.VarChar, 50).Value = _param2Value
cmd.Parameters.Add("@param3", SqlDbType.VarChar, 50).Value = _param3Value.
cmd.Parameters.Add("@out_param", SqlDbType.Int);
cmd.Parameters["@out_param"].Direction = ParameterDirection.Output;

try
{
SqlDataReader dr = cmd.ExecuteReader();
_out_value = (int)cmd.Parameters["@out_value"].Value;
}
catch ...etc...

when doing this, the value of cmd.Parameters["@out_param"] is always
null...


thx,
mo



-----------------------------------------------
"Too much football without a helmet?"

Al Pacino
Scent of a Woman
 
M

mo

Darn!!! You're right!
Unfortunately the DataGrid.VirtualItemCount has to be set before
DataGrid.DataBind()...

Thanks for your help...
Here's a quick sample: http://www.moweb.com/examples/datagrid.aspx

This does work making one trip to the database. The virtualitemcount is
returned int the first result set, then the datagrid is filled after calling
DataReader.NextResult(). If anyone sees a better way, please let me know...

thx,
mo


DalePres said:
Output and return parameters aren't available until you close the reader.

Dale

mo said:
oops, sorry about the typo... thanks.

I'm using the SqlDataReader for a paging datagrid. All the examples I've
seen say that you must query the database to get a value for
VirtualItemCount and then use the ExecuteReader() to get your result
set.
I
figured I could use an output parameter and only query the db once this
way.... I can't get output parameters to return any values with
ExecuteReader() no matter what I do, so I wonder if it is possible? The
stored procedure works great if I us a select @@rowcount in and return the
results in a second result set, then get the virtual item count from the
first result set then call NextResult() to populate the datagrid

Anyway... here's the code, if you see why it won't work please let me know
Thanks!

===============================

// this doesn't work...
private void dgCustomers_DataBind()
{
SqlConnection cn = new
SqlConnection(ConfigurationSettings.AppSettings["NorthwindDB"]);
SqlCommand cmd = new SqlCommand("usp_CustomersSelect", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@pageSize", SqlDbType.Int).Value =
dgCustomers.PageSize;
cmd.Parameters.Add("@rowStartIndex", SqlDbType.Int).Value =
(dgCustomers.CurrentPageIndex * dgCustomers.PageSize);
cmd.Parameters.Add("@virtualItemCount", SqlDbType.Int).Direction =
ParameterDirection.Output;

try
{
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
dgCustomers.VirtualItemCount =
(int)cmd.Parameters["@virtualItemCount"].Value;
dgCustomers.DataSource = dr;
dgCustomers.DataBind();
}
dr.Close();
lblVirtualItemCount.Text = String.Format("Total results: {0}",
dgCustomers.VirtualItemCount);
}
catch (Exception ex)
{
errMessage.InnerText = ex.ToString();
errMessage.Visible = true;
}
finally
{
cn.Dispose();
}
}

===================================

CREATE procedure usp_CustomersSelect
@pageSize int,
@rowStartIndex int,
@virtualItemCount int output
as

declare @rowEndIndex int
set @rowEndIndex = @rowStartIndex + @pageSize - 1

select identity(int,0,1) as rowIndex,
CustomerID, CompanyName, ContactName,
ContactTitle, Address, City, Region,
PostalCode, Country, Phone, Fax
into #TEMP_NWindCustomers
from Customers order by CompanyName

set @virtualItemCount = @@rowcount

select rowIndex, CustomerID, CompanyName, ContactName,
ContactTitle, Address, City, Region, PostalCode,
Country, Phone, Fax
from #TEMP_NWindCustomers
where rowIndex between @rowStartIndex and @rowEndIndex order by 1

====================================================
This works fine:

private void dgCustomers_DataBind()
{
SqlConnection cn = new
SqlConnection(ConfigurationSettings.AppSettings["NorthwindDB"]);
SqlCommand cmd = new SqlCommand("usp_CustomersSelect", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@pageSize", SqlDbType.Int).Value =
dgCustomers.PageSize;
cmd.Parameters.Add("@rowStartIndex", SqlDbType.Int).Value =
(dgCustomers.CurrentPageIndex * dgCustomers.PageSize);

try
{
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
dgCustomers.VirtualItemCount = (int)dr["virtualItemCount"];
}

while (dr.NextResult())
{
dgCustomers.DataSource = dr;
dgCustomers.DataBind();
}

dr.Close();
lblVirtualItemCount.Text = String.Format("Total results: {0}",
dgCustomers.VirtualItemCount);
}
catch (Exception ex)
{
errMessage.InnerText = ex.ToString();
errMessage.Visible = true;
}
finally
{
cn.Dispose();
}
}

private void dgCustomers_PageIndexChanged(object source,
DataGridPageChangedEventArgs e)
{
dgCustomers.CurrentPageIndex = e.NewPageIndex;
dgCustomers_DataBind();
}

CREATE procedure usp_CustomersSelect
@pageSize int,
@rowStartIndex int
as

declare @rowEndIndex int
set @rowEndIndex = @rowStartIndex + @pageSize - 1

select identity(int,0,1) as rowIndex,
CustomerID, CompanyName, ContactName,
ContactTitle, Address, City, Region,
PostalCode, Country, Phone, Fax
into #TEMP_NWindCustomers
from Customers order by CompanyName

select virtualItemCount = @@rowcount

select rowIndex, CustomerID, CompanyName, ContactName,
ContactTitle, Address, City, Region, PostalCode,
Country, Phone, Fax
from #TEMP_NWindCustomers
where rowIndex between @rowStartIndex and @rowEndIndex order by 1






William Ryan eMVP said:
Looks like a pspelling issue, one references @out_param and the outher
@out_value. Also, make sure the datareader has had it's .read method called
and is closed.

HTH,

Bill
should I be able to use output parameters when calling
SqlCommand.ExecuteReader()???

SqlConnection cn = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand("StoredProcedureName", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@param1", SqlDbType.Int).Value = _param1Value;
cmd.Parameters.Add("@param2", SqlDbType.VarChar, 50).Value = _param2Value
cmd.Parameters.Add("@param3", SqlDbType.VarChar, 50).Value = _param3Value.
cmd.Parameters.Add("@out_param", SqlDbType.Int);
cmd.Parameters["@out_param"].Direction = ParameterDirection.Output;

try
{
SqlDataReader dr = cmd.ExecuteReader();
_out_value = (int)cmd.Parameters["@out_value"].Value;
}
catch ...etc...

when doing this, the value of cmd.Parameters["@out_param"] is always
null...


thx,
mo



-----------------------------------------------
"Too much football without a helmet?"

Al Pacino
Scent of a Woman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top