Friday, April 30, 2010

Display all related data in comma separated list sql server

Concetenate the two fields and display the records with comma separated string in one field.

SELECT p_scrid,
SUBSTRING(
(
SELECT (' ,' + screen_name)
FROM tbl_SCRMst t2
WHERE t1.p_scrid = t2.p_scrid
ORDER BY
p_scrid,
screen_name
FOR XML PATH( '')
),3,1000)
FROM tbl_SCRMst t1
GROUP BY p_scrid

Thanks & Regards
Santosh

Saturday, April 3, 2010

Check record exist or not using asp.net and c#

This code show how to check record exist in database.Method get custid as a parameter and pass to stored procedure that will check record exist or not.

public bool IsCustomerExist(string strCust)
{
//SqlConnection objSqlConnection = null;
SqlCommand objSqlCommand = null;
try
{
//objSqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["StrConnect"].ToString());
objSqlCommand = new SqlCommand("usp_CheckCustomerExists", con);
objSqlCommand.CommandType = CommandType.StoredProcedure;
objSqlCommand.Parameters.Add("CustomerID", SqlDbType.Int, 4).Value = Convert.ToInt32(strCust);
con.Open();
SqlDataReader objSqlDataReader = objSqlCommand.ExecuteReader();
if (objSqlDataReader.HasRows)
return true;
else
return false;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
con.Close();
}
}

Thanks & Regards
Santosh