Tuesday, July 19, 2016

Export binary data from database using LINQPAD

Switch LinqPad to c# Program

void Main()
{
    var students = Students.Where(st => st.Photo != null).Select(st => new
    {
        st.Id,
        st.Photo
    });
    foreach (var student in students)
    {
        File.WriteAllBytes(@"D:\Photos\" + student.Id + ".jpg", student.Photo.ToArray());
    }
}

LINQ to Entities does not recognize the method 'System.String PadLeft(Int32)'

Convert to list then do padleft

The entity type List`1 is not part of the model for the current context

where db is List

db.Entry(bd).State = EntityState.Modified;

DbContext.Entry(object) expects a single object, not a collection.

so use a loop instead

foreach (var item in bd)
{
    db.Entry(item).State = EntityState.Modified;
}

Thursday, July 14, 2016

Convert dictionary to list collection in C#

using linq
accountTypes.Select(kvp => new
            {
                account_type_no = kvp.Key,
                account_type_name = kvp.Value
            }).ToList()
using loop
foreach (var item in accountTypes)
{
    accountTypeList.Add(item.Key);
}

Friday, March 4, 2016

How to get schema from stored procedure sql server

ADO.NET

DataTable schema = reader.GetSchemaTable();

SQL


SELECT * FROM sys.dm_exec_describe_first_result_set ('qualified.storedprocname', @params, 0) ;

How to submit / save changes in LinqPad

void Main()
{
    var students = Student.Select(st => st);
    foreach (var student in students)
    {
        student.FatherName = "Father";
    }
    SubmitChanges();
}

Monday, February 29, 2016

How to change a value of a DataItem in a GridViews RowDataBound Event

if (e.Row.RowType == DataControlRowType.DataRow)
{
 DataRow row = ((DataRowView)e.Row.DataItem).Row;
 for (int i = 0; i < row.ItemArray.Length; i++)
 {
  var obj = row.ItemArray[i];
  if (obj.GetType().Name == "DateTime")
  {
   e.Row.Cells[i + 1].Text = ((DateTime)obj).ToShortDateString();
  }
  else if (obj.GetType().Name == "Boolean")
  {
   e.Row.Cells[i + 1].Text = ((Boolean)obj).ToYesNo();
  }
 }
}

jQuery: get the file name selected from

$('input[type=file]')[0].files[0].name;

$("input[type=file]")[0].files[0].type;

$("input[type=file]")[0].files[0].size;

Wednesday, February 24, 2016

How do you check if a certain index exists in a sql server table?

IF EXISTS(SELECT * FROM sys.indexes WHERE name='[IndexName]' AND object_id = OBJECT_ID('[Schema].[Table]'))
BEGIN
    --statements
END

Tuesday, February 23, 2016

Sunday, February 21, 2016

How to disable all triggers MS SQL Server

To disable all trigger

sp_msforeachtable 'ALTER TABLE ? DISABLE TRIGGER all'

Or to enable or triggers

sp_msforeachtable 'ALTER TABLE ? ENABLE TRIGGER all'

Enable / Disable trigger in MS SQL server

DISABLE

ALTER TABLE table_name DISABLE TRIGGER tr_name

ENABLE

ALTER TABLE table_name ENABLE TRIGGER tr_name

SQL Server: Database stuck in “Restoring” state

RESTORE DATABASE MyDatabase
FROM DISK = 'MyDatabase.bak'
WITH REPLACE,RECOVERY

Sunday, January 24, 2016

Tuesday, January 19, 2016

How to store string in a cookie and retrieve it

Writing a cookie

HttpCookie myCookie = new HttpCookie("MyTestCookie");
DateTime now = DateTime.Now;
 
// Set the cookie value.
myCookie.Value = now.ToString();
// Set the cookie expiration date.
myCookie.Expires = now.AddYears(50); // For a cookie to effectively never expire
 
// Add the cookie.
Response.Cookies.Add(myCookie);

Reading a cookie

HttpCookie myCookie = Request.Cookies["MyTestCookie"];
 
// Read the cookie information and display it.
if (myCookie != null)
    Response.Write("<p>" + myCookie.Name + "<p>" + myCookie.Value);
else
    Response.Write("not found");

why cookies are always null after postback

private static string GetCookie(string name)
{
    return HttpContext.Current.Request != null ? HttpContext.Current.Request.Cookies[name].Value : string.Empty;
}
 
private static void SetCookie(string name, string value)
{
    HttpContext.Current.Request.Cookies[name].Value = value;
    HttpContext.Current.Request.Cookies[name].Expires = DateTime.Now.AddDays(100);
}

Monday, January 11, 2016

How to check if column exists in SQL Server table

IF EXISTS(SELECT * FROM sys.columns 
WHERE Name = N'columnName' AND Object_ID = Object_ID(qualified.table.name')) 
BEGIN 
    PRINT 'Column Exists'
END
--OR
IF COL_LENGTH('qualified.table.name','column_name') IS NULL
BEGIN
    PRINT 'Column does not exist or caller does not have permission to view the object'
END