Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello All,

I have store procedure that concatenation two column in one column and get the result.
i have 2 field in Table 1) FirstName 2) LastName.
in store procedure in want to result in one column and bind in gridview.
here is Store Procedure.

SQL
ALTER PROCEDURE [dbo].[GetOrderList]


	-- Add the parameters for the stored procedure here
	@OrderID INT,
	@OrderStatus VARCHAR(100),
        @CustomerName VARCHAR(100)
AS
BEGIN
	
	SELECT
		OM.ID,
		OM.OrderID,
		OM.OrderDate,
		OM.OrderStatus,
                OM.FirstName + ' ' + OM.LastName As CustomerName,
		ZM.ZoneName,
		SM.StateName,
		CM.CityName,
		S.StoreName
	FROM
		dbo.OrderMaster OM
	INNER JOIN dbo.OrderProduct OP ON OM.OrderID=OP.OrderID
	INNER JOIN dbo.Zone ZM ON ZM.ID=OM.ZoneID
	INNER JOIN dbo.StateMaster SM ON SM.ID=OM.StateID
	INNER JOIN dbo.City CM ON CM.ID=OM.CityID
	INNER JOIN dbo.StoreMaster S ON S.ID=OM.StoreID
	
	WHERE
		OM.IsDeleted=0
		AND OM.OrderID = ISNULL(NULLIF(@OrderID,0),OM.OrderID)
		AND OM.FirstName + ' ' + OM.LastName like @CustomerName
		AND OM.OrderStatus like '%' + ISNULL(@OrderStatus,'') + '%'
END



but there is no bind in gridview :
here is GirdView BindDate :-


C#
private void BindData(string SortExpression, string SortDirection, int OrderID,string OrderStatus,string CustomerName)
    {
        DatabaseHelper db = new DatabaseHelper();
        DataSet ds = db.GetOrderList(OrderID, OrderStatus,CustomerName);

        if (SortDirection != null && SortExpression != null)
        {
            DataView DView = new DataView(ds.Tables[0]);
            DView.Sort = SortExpression + " " + SortDirection;
            GrdOrder.DataSource = DView;
        }
        else
        {
            GrdOrder.DataSource = ds.Tables[0];
            GrdOrder.DataBind();
        }
    }



SQL
public DataSet GetOrderList(int OrderID,string OrderStatus,string CustomerName)
    {
        Database db = new SqlDatabase(this.ConnectionString);
        using (DbCommand objcmd = db.GetStoredProcCommand("dbo.GetOrderList"))
        {
            if (OrderID == null)
                db.AddInParameter(objcmd, "@OrderID", DbType.Int32, DBNull.Value);
            else
                db.AddInParameter(objcmd, "@OrderID", DbType.Int32, OrderID);

            if (CustomerName== null)
                db.AddInParameter(objcmd, "@CustomerName", DbType.String, DBNull.Value);
            else
                db.AddInParameter(objcmd, "@CustomerName", DbType.String, CustomerName);

            if (OrderStatus == null)
                db.AddInParameter(objcmd, "@OrderStatus", DbType.String, DBNull.Value);
            else
                db.AddInParameter(objcmd, "@OrderStatus", DbType.String, OrderStatus);

            DataSet ds = db.ExecuteDataSet(objcmd);
            return ds;
        }
    }



Where I am Wrong in this..!!

Thank You All
Posted
Updated 22-Aug-12 21:16pm
v2
Comments
Kuthuparakkal 23-Aug-12 3:33am    
What happens if you try to execute your stored procedure alone ? did it return rows ?
Again everything is INNER JOIN so dont know what's the point of passing null id to store proc. null id will return nothing..

Probably one of your columns is NULL, try the following in your stored procedure:
SQL
...
TRIM(ISNULL(OM.FirstName,'') + ' ' + ISNULL(OM.LastName,'')) As CustomerName,
...
 
Share this answer
 
Try this
SQL
SELECT OrderMaster.FirstName + ' ' + OrderMaster.LastName AS Full_Name FROM OrderMaster
 
Share this answer
 
v2
Suspect the following line
AND OM.OrderID = ISNULL(NULLIF(@OrderID,0),OM.OrderID)
can be written as
AND OM.OrderID = COALESCE(@OrderID,0)


BUT the question here is do you have records with OrderID as zero in your table ?
 
Share this answer
 
Hi,

are u getting any error /or wrong result ....
 
Share this answer
 
Comments
Yatin chauhan 23-Aug-12 5:48am    
No LAKSS there is no any error behind that. when i debug its run clearly...!!
replace your SP

SQL
OM.FirstName + ' ' + OM.LastName like @CustomerName


with

SQL
OM.FirstName like @CustomerName OR OM.LastName like @CustomerName


and also add the '%' in with the parameter name in SP or else
C#
cmd.Parameters.AddWithValue("@CustomerName", "%" + SearchName + "%")
 
Share this answer
 
Comments
Kuthuparakkal 23-Aug-12 3:43am    
This cannot be right, becoz you have OR condition here.. Customers can have First Name in Common or last name in common... thats not the set we want.. voting this down.
Yatin chauhan 23-Aug-12 5:47am    
Hello Prabhakaran Soundarapandian,

I dont want to add in table..i have already add. this SP is for get the record and bind in datagrid only.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900