Translate

Home > January 2018

January 2018

C# HttpWebRequest FormData post and return Json

Wednesday, January 24, 2018 Category : , 0


        public static Result POSTFormData(string url, string identifier, string FormData)
        {
            // Setup the POST data
            //string poststring = String.Format("qrCodeData=90320388203195564382");
            Result result = new Result();
            try
            {

                HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);

                httpRequest.Method = "POST";
                httpRequest.ContentType = "application/x-www-form-urlencoded";
                httpRequest.Headers.Add("secret", identifier);


                // Convert the post string to a byte array
                byte[] bytedata = System.Text.Encoding.UTF8.GetBytes(FormData);
                httpRequest.ContentLength = bytedata.Length;

                // Create the stream
                Stream requestStream = httpRequest.GetRequestStream();
                requestStream.Write(bytedata, 0, bytedata.Length);
                requestStream.Close();

                // Get the response from remote server
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();

                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                using (StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        sb.Append(line);
                    }
                }

                result.ResultState = true;
                result.JsonString = sb.ToString();

            }
            catch (Exception ex)
            {
                result.ResultState = false;
                result.SqlError = ex.Message;
            }

            return result;
        }



            object theImg = dt.Rows[0]["Picture"];
if (!DBNull.Equals(theImg, DBNull.Value) && ((byte[])theImg).Length > 0)
            {
                 pictureBox1.BackgroundImage = new ImageHelper().ByteToImage((byte[])theImg);
}

C# Image , Byte , URL To Image Convert

Category : 0

    public class ImageHelper
    {
        public byte[] ImageToByte(Image imageIn)
        {

            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray();
        }

        public Image ByteToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }

        public Image GenerateThumbnailImage(string imgUrl)
        {
            Image imgThumb = null;
            try
            {
                Image image = null;
                if (imgUrl != String.Empty)
                    image = Image.FromFile(imgUrl);

                if (image != null)
                {
                    imgThumb = image.GetThumbnailImage(130, 170, null, new IntPtr());
                    //this.Refresh();
                }
                return imgThumb;
            }
            catch
            {              
                return imgThumb;
            }
        }

    }

SQL Store Procedure To Class Wrapper

Wednesday, January 3, 2018 Category : 0



DECLARE @sql NVARCHAR(MAX) = N'EXEC ShopMenu_GermanClub.dbo.SP_ReportRM_PO_Challan ww,admin,Y;';

--SELECT name, system_type_name
--    FROM sys.dm_exec_describe_first_result_set(@sql, NULL, 1) AS col


declare @TableName sysname = 'SP_ReportRM_PO_Challan'
declare @Result varchar(max) = 'public class ' + @TableName + '
{'

select @Result = @Result + '
    public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }
'
from
(
    select
        replace(col.name, ' ', '_') ColumnName,
       
        case --col.system_type_name
            WHEN (CHARINDEX('nvarchar',system_type_name)>0) then 'string'
            when (system_type_name='bigint') then 'long'
            when (system_type_name='binary') then 'byte[]'
            when (system_type_name='bit') then 'bool'
            when (system_type_name='char') then 'string'
            when (system_type_name='date') then 'DateTime'
            when (system_type_name='datetime') then 'DateTime'
            when (system_type_name='datetime2') then 'DateTime'
            when (system_type_name='datetimeoffset') then 'DateTimeOffset'
            when (CHARINDEX('decimal',system_type_name)>0) then 'decimal'
            when (system_type_name='float') then 'float'
            when (system_type_name='image') then 'byte[]'
            when (system_type_name='int') then 'int'
            when (system_type_name='money') then 'decimal'
            when (system_type_name='nchar') then 'string'
            when (system_type_name='ntext') then 'string'
            when (CHARINDEX('numeric',system_type_name)>0) then 'decimal'
            when (system_type_name='real') then 'double'
            when (system_type_name='smalldatetime') then 'DateTime'
            when (system_type_name='smallint') then 'short'
            when (system_type_name='smallmoney') then 'decimal'
            when (system_type_name='text') THEN 'string'
            when (system_type_name='time') then 'TimeSpan'
            when (system_type_name='timestamp') then 'DateTime'
            when (system_type_name='tinyint') then 'byte'
            when (system_type_name='uniqueidentifier') then 'Guid'
            when (system_type_name='varbinary') then 'byte[]'
            when (system_type_name='varchar') then 'string'
            ELSE ('UNKNOWN_') + col.system_type_name
        end ColumnType,
        case
            when col.is_nullable = 1 and col.name in ('bigint', 'bit', 'date', 'datetime', 'datetime2', 'datetimeoffset', 'decimal', 'float', 'int', 'money', 'numeric', 'real', 'smalldatetime', 'smallint', 'smallmoney', 'time', 'tinyint', 'uniqueidentifier')
            then '?'
            else ''
        end NullableSign
    -- name, system_type_name
    FROM sys.dm_exec_describe_first_result_set(@sql, NULL, 1) AS col

) t
--order by ColumnId

set @Result = @Result  + '
}'

print @Result

Powered by Blogger.