Wednesday, August 17, 2011

Thursday, August 11, 2011

Cannot use the special principal 'sa'.

ERROR: Cannot use the special principal 'sa'.

SOLUTION: run this command in the query editor window for that database

exec sp_changedbowner 'sa','true'


Wednesday, August 10, 2011

find out which column has the userId = 11 in a database

exec sp_msforeachtable 'Select ''[?]'' as Table_Name, * from ? where userId in (''11'')'



Convert column data from Binary to String with SQL Server

declare @stringPassowrd varchar(max)

select @stringPassowrd = sys.fn_sqlvarbasetostr(UserPassword) from Users where UserID = 2

print @stringPassowrd

where UserPassword is the binary column you would like to convert.

Thursday, July 28, 2011

Export DataTable to Excel

private void btnExportToExcel_Click(object sender, EventArgs e)

{

//Export to Excel code


try

{

string attachment = "attachment; filename=Contact.xls";


HttpContext.Current.Response.Clear();

HttpContext.Current.Response.Clear();

HttpContext.Current.Response.AddHeader("content-disposition", attachment);

HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

string sTab = "";

foreach (DataColumn dc in resultTable.Columns)

{

HttpContext.Current.Response.Write(sTab + dc.ColumnName);


sTab = "\t";

}

HttpContext.Current.Response.Write("\n");


int i;

foreach (DataRow dr in resultTable.Rows)

{

sTab = "";


for (i = 0; i < resultTable.Columns.Count; i++)

{

HttpContext.Current.Response.Write(sTab + dr[i].ToString());


sTab = "\t";

}

HttpContext.Current.Response.Write("\n");


}

HttpContext.Current.Response.End();


}

catch (Exception ex)


{

ErrorMessage = ex.Message;

ErrorLabel = new Label();


ErrorLabel.Text = ErrorMessage;

this.Controls.Add(ErrorLabel);


}

}