DotNet Blogs

Articles of ASP.NET, C# & SQL Server

Archive for October, 2008

Show Image from SQL Server in ASP.NET Using C#

Posted by Sujit Kumar on October 3, 2008

Show Image from SQL Server in ASP.NET Using C#

SqlConnection objConnection;
SqlCommand objCommand;
SqlDataAdapter objAdapter;
DataSet objDS=new DataSet();

using (objConnection = new SqlConnection(“server=sujitkumar\\sqlexpress;database=livechat;uid=sa;pwd=1234″))
{
objConnection.Open();
using (objCommand = new SqlCommand())
{
objCommand.Connection = objConnection;

objCommand.CommandType = CommandType.Text;
objCommand.CommandText = “select img_online from image_master”;

objAdapter = new SqlDataAdapter(objCommand);
objAdapter.Fill(objDS, “CustomerAttributes”);
}
objConnection.Close();
}

MemoryStream stream = new MemoryStream();
byte[] image = (byte[])objDS.Tables[0].Rows[0][0];
Response.ContentType = “image/jpeg”;
Response.Expires = 0; Response.Buffer =true;
Response.Clear();
Response.BinaryWrite(image);
Response.End();

The above code will write image on the page.

You can show this image in any image button or image control like:


Posted in asp.net | Tagged: , | Leave a Comment »

Find LastIndex in SQL Server

Posted by Sujit Kumar on October 1, 2008

CREATE FUNCTION dbo.LASTINDEX(@STRING VARCHAR(8000), @CHAR CHAR)
RETURNS INT
AS
BEGIN
RETURN LEN(@STRING) – CHARINDEX(@CHAR, REVERSE(@STRING), 1 + 1)
END

Posted in asp.net | Tagged: | 1 Comment »