Thursday 2 January 2020

power


SELECT TOP 1  @DIRECT_COUNT_LEVEL=X.LEVEL, @TEAM_SIZE=X.P FROM (
SELECT LEVEL,COUNT(DISTINCT JCODE) AS P,POWER(4,LEVEL) AS QM FROM AMTPAIRDTL WHERE LEVEL<=5 AND  MEMB_CODE=@PLACID GROUP BY LEVEL
) AS X WHERE X.P=X.QM ORDER BY X.LEVEL DESC

Saturday 28 December 2019

Save and Display Image / Video on database


  //Create Class

public class Common
    {
 public string ViewImage(byte[] arrayImage)
        {
            return "data:image/png;base64," + Convert.ToBase64String(arrayImage, 0, arrayImage.Length);
        }

        public string ViewVideo(byte[] arrayImage)
        {
            return "data:video/mp4;base64," + Convert.ToBase64String(arrayImage, 0, arrayImage.Length);
        }

 //****
        public String ConvertImageURLToBase64(String url)
        {
            StringBuilder _sb = new StringBuilder();
            Byte[] _byte = this.GetImage(url);
            _sb.Append(Convert.ToBase64String(_byte, 0, _byte.Length));
            return _sb.ToString();
        }

        public byte[] GetImage(string url)
        {
            Stream stream = null;
            byte[] buf;

            try
            {
                WebProxy myProxy = new WebProxy();
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                stream = response.GetResponseStream();

                using (BinaryReader br = new BinaryReader(stream))
                {
                    int len = (int)(response.ContentLength);
                    buf = br.ReadBytes(len);
                    br.Close();
                }

                stream.Close();
                response.Close();
            }
            catch (Exception exp)
            {
                buf = null;
            }

            return (buf);
        }
}
//************************** html code *************

Show image by url

 <img src="@cm.ViewImage( cm.GetImage("https://www.aswtech.in/Content/Home/images/slider/fraction/3.png"))" width="300px" height="200px" alt="Alternate Text" /> 

Show Image /Vidwo From Database

 <video   src='@cm.ViewVideo((byte[])dt.Rows[i]["XDATA"])' controls="true"  width="400" height="350" loop="true"/>


 <img src="@cm.ViewImage((byte[])dt.Rows[i]["XDATA"])" width="300px" height="200px" alt="Alternate Text" /> 

Database Table

CREATE TABLE [dbo].[PROMOTION](
[SRNO] [int] IDENTITY(1,1) NOT NULL,
[TDATE] [datetime] NULL,
[XDATA] [varbinary](max) NULL
)

Convert uploaded File to bytes  array
 public ActionResult AddPromotion(FormCollection frm, HttpPostedFileBase file1)
        {

byte[] bytes = null;
       
            if (file1 != null)
            {
                  path = Path.GetFileName(file1.FileName);


                using (BinaryReader br = new BinaryReader(file1.InputStream))
                {
                    bytes = br.ReadBytes(file1.ContentLength);
                }
            }
//Save bytes to database

}




Thursday 26 December 2019

Convert DataTable to List In C#

  1. Using a Loop.
  1. Using LINQ.
  1. Using a Generic Method.
Convert DataTable to List Using a Loop

DataTable dt = new DataTable("Student"); 
dt.Columns.Add("StudentId", typeof(Int32)); 
dt.Columns.Add("StudentName", typeof(string)); 
dt.Columns.Add("Address", typeof(string)); 
dt.Columns.Add("MobileNo", typeof(string)); 
    //Data 
dt.Rows.Add(1, "Manish", "Hyderabad","0000000000"); 
dt.Rows.Add(2, "Venkat", "Hyderabad", "111111111"); 
dt.Rows.Add(3, "Namit", "Pune", "1222222222"); 
dt.Rows.Add(4, "Abhinav", "Bhagalpur", "3333333333");
public void StudentList() 

    //  DataTable dt = new DataTable("Branches"); 
    DataTable dt = new DataTable("Student"); 
    dt.Columns.Add("StudentId", typeof(Int32)); 
    dt.Columns.Add("StudentName", typeof(string)); 
    dt.Columns.Add("Address", typeof(string)); 
    dt.Columns.Add("MobileNo", typeof(string)); 
    //Data 
    dt.Rows.Add(1, "Manish", "Hyderabad", "0000000000"); 
    dt.Rows.Add(2, "Venkat", "Hyderabad", "111111111"); 
    dt.Rows.Add(3, "Namit", "Pune", "1222222222"); 
    dt.Rows.Add(4, "Abhinav", "Bhagalpur", "3333333333"); 
 
    List<Student> studentList = new List<Student>(); 
    for (int i = 0; i < dt.Rows.Count; i++) 
    { 
        Student student = new Student(); 
        student.StudentId = Convert .ToInt32 (dt.Rows[i]["StudentId"]); 
        student.StudentName = dt.Rows[i]["StudentName"].ToString(); 
        student.Address = dt.Rows[i]["Address"].ToString(); 
        student.MobileNo = dt.Rows[i]["MobileNo"].ToString(); 
        studentList.Add(student); 
    } 
}

Convert DataTable to List Using Linq


public void StudentListUsingLink() 

    //  DataTable dt = new DataTable("Branches"); 
    DataTable dt = new DataTable("Student"); 
    dt.Columns.Add("StudentId", typeof(Int32)); 
    dt.Columns.Add("StudentName", typeof(string)); 
    dt.Columns.Add("Address", typeof(string)); 
    dt.Columns.Add("MobileNo", typeof(string)); 
    //Data 
    dt.Rows.Add(1, "Manish", "Hyderabad", "0000000000"); 
    dt.Rows.Add(2, "Venkat", "Hyderabad", "111111111"); 
    dt.Rows.Add(3, "Namit", "Pune", "1222222222"); 
    dt.Rows.Add(4, "Abhinav", "Bhagalpur", "3333333333"); 
    List<Student> studentList = new List<Student>(); 
    studentList = (from DataRow dr in dt.Rows 
            select new Student() 
            { 
                StudentId = Convert .ToInt32 (dr["StudentId"]), 
                StudentName = dr["StudentName"].ToString(), 
                Address = dr["Address"].ToString(), 
                MobileNo = dr["MobileNo"].ToString() 
            }).ToList(); 
   

Convert DataTable to List using a Generic Method

private static List<T> ConvertDataTable<T>(DataTable dt) 

    List<T> data = new List<T>(); 
    foreach (DataRow row in dt.Rows) 
    { 
        T item = GetItem<T>(row); 
        data.Add(item); 
    } 
    return data; 

private static T GetItem<T>(DataRow dr) 

    Type temp = typeof(T); 
    T obj = Activator.CreateInstance<T>(); 
 
    foreach (DataColumn column in dr.Table.Columns) 
    { 
        foreach (PropertyInfo pro in temp.GetProperties()) 
        { 
            if (pro.Name == column.ColumnName) 
                pro.SetValue(obj, dr[column.ColumnName], null); 
            else 
                continue; 
        } 
    } 
    return obj; 
}
List< Student > studentDetails = new List< Student >(); 
studentDetails = ConvertDataTable< Student >(dt);
x

Bind value to multi class


*******************Singel Item Binding********************
 public class Student
    {
        public int StudentId { get; set; }
        [Display(Name = "Name")]
        public string StudentName { get; set; }
        public int Age { get; set; }
        public Standard standard { get; set; }
   
    }

    public class Standard
    {
        public int StandardId { get; set; }
        public string StandardName { get; set; }
    }

bind to class

 Student s = new Student();
       s.Age = 25;
       s.standard.StandardId = 25;
       s.standard.StandardName = "New Sand";
       s.StudentId=2;
       s.StudentName = "Samir";

***********************************

*******************List of Item Binding********************

  public class Student
    {
        public int StudentId { get; set; }
        [Display(Name = "Name")]
        public string StudentName { get; set; }
        public int Age { get; set; }
        public List<Standard> listStad { get; set; }
    }

    public class Standard
    {
        public int StandardId { get; set; }
        public string StandardName { get; set; }
    }

bind to class
 Student s = new Student();
            s.Age = 25;
            s.listStad =
                      new List<Standard>() {
                                        new Standard { StandardId = 25, StandardName = "Samir" },
                                        new Standard { StandardId = 26, StandardName = "samir 02" }
                         };
            s.StudentId=2;
            s.StudentName = "Samir";

******************************************************

Bind data By Datatable

public class Group
    {
        public string name { get; set; }
        public string id { get; set; }
        public bool expanded { get; set; }
        public List<Resoruce> children { get; set; }
    }
    public class Resoruce
    {
        public string name { get; set; }
        public string id { get; set; }
        public string groupid { get; set; }
    }


public ActionResult ShowData()
        {
            dal.ClearParameters();
            dal.AddParameter("@MODE", "SELECT_GROUP", "IN");
            DataTable dtgroup = dal.GetTable("SP_EVENTS", ref Message);

            dal.ClearParameters();
            dal.AddParameter("@MODE", "SELECT_SCHE_GROUP_MEMBER_SHOW", "IN");
            DataTable dtres = dal.GetTable("SP_EVENTS", ref Message);

            List<Group> groups = new List<Group>();
            for (int i = 0; i < dtgroup.Rows.Count; i++)
            {
                Group group = new Group
                {
                    id = dtgroup.Rows[i]["ID"].ToString(),
                    name = dtgroup.Rows[i]["name"].ToString(),
                    expanded = true,
                    children = getResoruce(dtgroup.Rows[i]["groupId"].ToString(), dtres)
                };
                groups.Add(group);
            }
            ViewBag.Datax = groups;
            return View();
        }
        public List<Resoruce> getResoruce(string groupId, DataTable resours)
        {
            DataRow[] result = resours.Select("groupId='" + groupId + "'");
            List<Resoruce> detail = new List<Resoruce>();
            foreach (var item in result)
            {
                detail.Add(new Resoruce
                    {
                        groupid = item["groupId"].ToString(),
                        id = item["id"].ToString(),
                        name = item["name"].ToString()
                    });
            }

            return detail;
        }