public void gridCustInfoBinds(int pageIndex)
{
try
{
//open the db connection if it is closed...
if (connection.State == ConnectionState.Closed)
connection.Open();
string ColumnName = ddlColums.SelectedValue;
string ddlValue = ddlColumValue.SelectedValue;
string txtValue = txtColumValue.Text;
string status = ddlStatus.SelectedValue;
command = new SqlCommand();
command.CommandText = "sp_Get_CustInfoSerach2";
command.CommandType = CommandType.StoredProcedure;
command.Connection = connection;
command.Parameters.AddWithValue("@ColumnName", ColumnName);
command.Parameters.AddWithValue("@ddlValue", ddlValue);
command.Parameters.AddWithValue("@txtValue", txtValue);
command.Parameters.AddWithValue("@status", status);
command.Parameters.AddWithValue("@PageIndex", pageIndex);
command.Parameters.AddWithValue("@PageSize", int.Parse(ddlPaging.SelectedValue));
command.Parameters.Add("@RecordCount", SqlDbType.Int, 4);
//Note: Here throwing an error :Object cannot be cast from DBNull to other types.
command.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
IDataReader idr = command.ExecuteReader();
gridCustomer.DataSource = idr;
gridCustomer.DataBind();
idr.Close();
connection.Close();
int recordCount = Convert.ToInt32(command.Parameters["@RecordCount"].Value);
this.PopulatePager(recordCount, pageIndex);
}
catch (Exception ex)
{
lblMessageCustSerach.Text = ex.Message;
lblMessageCustSerach.Visible = true;
}
finally //Close db Connection if it is open....
{
if (connection.State == ConnectionState.Open)
connection.Close();
}
}
//My Store procedure is
ALTER PROCEDURE Sp_get_custinfoserach2 (@PageIndex INT = 1,
@PageSize INT = 10,
@RecordCount INT output,
@ColumnName VARCHAR(50)=NULL,
@Value VARCHAR(50)=NULL,
@ddlValue VARCHAR(50)=NULL,
@txtValue VARCHAR(50)=NULL,
@status VARCHAR(30))
AS
BEGIN
SET nocount ON;
DECLARE @cmd AS NVARCHAR(max)
IF @txtValue IS NULL
BEGIN
SET @Value = '''' + @ddlValue + ''''
END
ELSE IF @ddlValue IS NULL
BEGIN
SET @Value = '''' + @txtValue + ''''
END
/*create a temp as same structure of your dynamic query select statement*/
SET @cmd ='CREATE TABLE #result
(
rownum INT,
userid INT,
NAME VARCHAR(100),
username VARCHAR(100),
status VARCHAR(15),
packageperiod VARCHAR(15),
packagename VARCHAR(100),
activationdate DATETIME,
oldexpirydate DATETIME,
balance NUMERIC(22, 4),
pyingamount NUMERIC(22, 4),
lastpaiddate DATETIME,
lastupdatetime DATETIME,
areaname VARCHAR(100),
mobno INT,
empname VARCHAR(100),
address VARCHAR(5000),
createddate DATETIME
)
Insert into #result
SELECT ROW_NUMBER() OVER (ORDER BY C_Register.UserId desc )AS RowNumber,
C_Register.UserId, C_Register.Name, C_Register.UserName,
C_Register.Status, Packages.PackagePeriod, Packages.PackageName,
C_Register.ActivationDate,Receive_Payment.OldExpiryDate,
Receive_Payment.Balance, Receive_Payment.PyingAmount,
Receive_Payment.LastPaidDate, C_Register.LastUpdateTime,
Area.AreaName, C_Register.MobNo, Employee.EmpName,
C_Register.Address,C_Register.CreatedDate
FROM C_Register INNER JOIN Receive_Payment ON C_Register.UserId = Receive_Payment.UserId
INNER JOIN Area ON C_Register.AreaId = Area.AreaId
INNER JOIN Employee ON Receive_Payment.EmpId = Employee.EmpId
INNER JOIN Packages ON Receive_Payment.PackageId = Packages.PackageId
where C_Register.AccountExpiry= Receive_Payment.OldExpiryDate And C_Register.Status = ' + @status + ' And ' + @ColumnName + ' = ' + @Value +
'SELECT @RecordCount = COUNT(*) FROM #results
SELECT * FROM #results
WHERE rownumber BETWEEN'+ cast(( cast(@PageIndex as int) - 1) * cast(@PageSize as int) + 1 as varchar(50))+' AND'+cast(((( @PageIndex - 1 ) * @PageSize+ 1 ) + @PageSize ) - 1 as varchar(50))
+ 'DROP TABLE #results'
EXEC(@cmd)
END

Jignesh TrivediPosted Mar 18, 2015, 4:18 AM
are your sure it string?
if yes so you have to pass in single quote
SET @cmd ='
SELECT ROW_NUMBER() OVER (ORDER BY C_Register.UserId desc )AS RowNumber,
C_Register.UserId, C_Register.Name, C_Register.UserName,
C_Register.Status, Packages.PackagePeriod, Packages.PackageName,
C_Register.ActivationDate,Receive_Payment.OldExpiryDate,
Receive_Payment.Balance, Receive_Payment.PyingAmount,
Receive_Payment.LastPaidDate, C_Register.LastUpdateTime,
Area.AreaName, C_Register.MobNo, Employee.EmpName,
C_Register.Address,C_Register.CreatedDate
FROM C_Register INNER JOIN Receive_Payment ON C_Register.UserId = Receive_Payment.UserId
INNER JOIN Area ON C_Register.AreaId = Area.AreaId
INNER JOIN Employee ON Receive_Payment.EmpId = Employee.EmpId
INNER JOIN Packages ON Receive_Payment.PackageId = Packages.PackageId
where C_Register.AccountExpiry= Receive_Payment.OldExpiryDate And C_Register.Status = ''' + @status + ''' And ' + @ColumnName + ' = ' + @Value
hope this will help you...
Karthik ElumalaiPosted Jun 7, 2016, 7:26 AM
How to Resolve the error Object cannot be cast from DBNull to other types.
Reason for the error:
In an object-oriented programming language, null means the absence of a reference to an object. DBNull represents an uninitialized variant or nonexistent database column. Source:MSDN
Actual Code which I faced error:
Before changed the code:
if( ds.Tables[0].Rows[0][0] == null ) // Which is not working
{
seqno = 1;
}
else
{
seqno = Convert.ToInt16(ds.Tables[0].Rows[0][0]) + 1;
}
After changed the code:
if( ds.Tables[0].Rows[0][0] == DBNull.Value ) //which is working properly
{
seqno = 1;
}
else
{
seqno = Convert.ToInt16(ds.Tables[0].Rows[0][0]) + 1;
}
Conclusion: when the database value return the null value, we recommend to use the DBNull class instead of just specifying as a null like in C# language.
Please let me know your feedback
Thanks
Gowtham RajamanickamPosted Apr 5, 2015, 1:30 AM
Jignesh TrivediPosted Mar 18, 2015, 4:55 AM
YES
but you override the value of parameter @Value
SET @Value = '''' + @txtValue + ''''
hope this will help you
(If this post is useful then mark it as "Accepted Answer")
Dawood AbbasPosted Mar 18, 2015, 4:51 AM
but couldn't understood why not taking C_Register.Status = Suspend as string where as And [PackagePeriod] = 'Monthly' tasking as string
both are same datatype and passing in same way na
Dawood AbbasPosted Mar 18, 2015, 4:11 AM
where as 'suspend' is column value equating with status column...
Jignesh TrivediPosted Mar 18, 2015, 3:49 AM
I think parameter @ColumnName (Package Period ) is wrong. it contains space between word...
if it is correct column name then you place it with [Package Period]
like
= ' + @status + ' And [' + @ColumnName + '] = ' + @Value
try this and let us know if you face any issue...
Dawood AbbasPosted Mar 18, 2015, 3:44 AM
Jignesh TrivediPosted Mar 18, 2015, 2:56 AM
Please provide query within variable @cmd
which can be get in Message tab in query window.
Dawood AbbasPosted Mar 18, 2015, 2:53 AM
An expression of non-boolean type specified in a context where a condition is expected, near 'Period'.
Jignesh TrivediPosted Mar 18, 2015, 2:28 AM
Check the value of @status, @ColumnName and @Value... any one of them is null or blank
I advice you first print @cmd query before execute it and verify the query....
print @cmd
insert into #result
EXEC(@cmd)
hope this will help you.
Dawood AbbasPosted Mar 18, 2015, 2:23 AM
An expression of non-boolean type specified in a context where a condition is expected, near 'Period'.
Jignesh TrivediPosted Mar 18, 2015, 1:49 AM
Hi,
Here @RecordCount is out of scope from dynamic query.
why are you use use here dynamic query?
Intead of do create temp table out of dynamic query...
try following stored procedure
ALTER PROCEDURE Sp_get_custinfoserach2 (@PageIndex INT = 1,
@PageSize INT = 10,
@RecordCount INT output,
@ColumnName VARCHAR(50)=NULL,
@Value VARCHAR(50)=NULL,
@ddlValue VARCHAR(50)=NULL,
@txtValue VARCHAR(50)=NULL,
@status VARCHAR(30))
AS
BEGIN
SET nocount ON;
DECLARE @cmd AS NVARCHAR(max)
IF @txtValue IS NULL
BEGIN
SET @Value = '''' + @ddlValue + ''''
END
ELSE IF @ddlValue IS NULL
BEGIN
SET @Value = '''' + @txtValue + ''''
END
/*create a temp as same structure of your dynamic query select statement*/
CREATE TABLE #result
(
rownum INT,
userid INT,
NAME VARCHAR(100),
username VARCHAR(100),
status VARCHAR(15),
packageperiod VARCHAR(15),
packagename VARCHAR(100),
activationdate DATETIME,
oldexpirydate DATETIME,
balance NUMERIC(22, 4),
pyingamount NUMERIC(22, 4),
lastpaiddate DATETIME,
lastupdatetime DATETIME,
areaname VARCHAR(100),
mobno INT,
empname VARCHAR(100),
address VARCHAR(5000),
createddate DATETIME
)
SET @cmd ='
SELECT ROW_NUMBER() OVER (ORDER BY C_Register.UserId desc )AS RowNumber,
C_Register.UserId, C_Register.Name, C_Register.UserName,
C_Register.Status, Packages.PackagePeriod, Packages.PackageName,
C_Register.ActivationDate,Receive_Payment.OldExpiryDate,
Receive_Payment.Balance, Receive_Payment.PyingAmount,
Receive_Payment.LastPaidDate, C_Register.LastUpdateTime,
Area.AreaName, C_Register.MobNo, Employee.EmpName,
C_Register.Address,C_Register.CreatedDate
FROM C_Register INNER JOIN Receive_Payment ON C_Register.UserId = Receive_Payment.UserId
INNER JOIN Area ON C_Register.AreaId = Area.AreaId
INNER JOIN Employee ON Receive_Payment.EmpId = Employee.EmpId
INNER JOIN Packages ON Receive_Payment.PackageId = Packages.PackageId
where C_Register.AccountExpiry= Receive_Payment.OldExpiryDate And C_Register.Status = ' + @status + ' And ' + @ColumnName + ' = ' + @Value
insert into #result
EXEC(@cmd)
SELECT @RecordCount = COUNT(*) FROM #result
SELECT * FROM #result
WHERE rownum BETWEEN ((@PageIndex -1) * @PageSize) + 1 and ((( @PageIndex - 1 ) * @PageSize+ 1 ) + @PageSize ) - 1
END
hope this will help you.
Dawood AbbasPosted Mar 18, 2015, 1:24 AM
"int recordCount = Convert.ToInt32(command.Parameters["@RecordCount"].Value);"
VulpesPosted Mar 16, 2015, 7:29 AM
int recordCount = Convert.ToInt32(command.Parameters["@RecordCount"].Value);
to these:
int recordCount = 0;
object val = command.Parameters["@RecordCount"].Value;
if (!Convert.IsDBNull(val)) recordCount = (int)val;
Rahul BansalPosted Mar 16, 2015, 4:45 AM
Dawood AbbasPosted Mar 16, 2015, 2:58 AM
Manish Kumar ChoudharyPosted Mar 16, 2015, 2:54 AM
Replace following line of your stored procedure
'SELECT @RecordCount = COUNT(*) FROM #results
To
set @RecordCount = Select COUNT(*) FROM #results
then it may work.