Wednesday, September 07, 2011

tsql rename table and column

--rename a table:
EXEC sp_rename 'OldTableName', 'NewTableName'
--rename a column:
EXEC sp_rename
@objname = 'TableName.OldColumnName',
@newname = 'NewColumnName',
@objtype = 'COLUMN'

Friday, September 02, 2011

c# assign values to object property by property name

Class1 obj= new Class1();
string propertyName = "name1";
string objValue = "";
Type type = obj.GetType();
PropertyInfo prop = type.GetProperty(propertyName );
prop.SetValue(obj, objValue , null);

Thursday, September 01, 2011

c# aspnet download file from server side

string text1 = this.Server.MapPath("~/" + [FileFolder]);
if (Directory.Exists(text1))
{
string text2 = text1 + @"\[File Name with extension]";
if (System.IO.File.Exists(text2))
{
FileStream stream1 = new FileStream(text2, FileMode.Open, FileAccess.Read);
byte[] buffer1 = new byte[((int)stream1.Length) + 1];
stream1.Read(buffer1, 0, (int)stream1.Length);
stream1.Close();

//Writes the file out the Response object
Response.Clear();
Response.Buffer = true;
Response.AddHeader("Content-Disposition", "attachment;filename=\"[File Name with extension]\";");
Response.WriteFile(text2, true);
Response.ContentType = "application/x-zip-compressed";
Response.BinaryWrite(buffer1);
Response.End();
}
}

Thursday, August 25, 2011

helpful linq to sql article

http://weblogs.asp.net/scottgu/archive/2007/07/11/linq-to-sql-part-4-updating-our-database.aspx

Wednesday, August 24, 2011

some useful keyboard shortcuts for Visual Studio

found this helpful
http://visualstudiohacks.com/general/confessions-of-a-keyboard-junkie/

Wednesday, August 17, 2011

import from excel c# aspnet

String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + RadUpload.UploadedFiles[0].FileName+";" +
"Extended Properties=Excel 8.0;";
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
OleDbCommand cmdSelectAdditions = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);
OleDbDataAdapter AdapterForAdditions = new OleDbDataAdapter();
AdapterForAdditions.SelectCommand = cmdSelectAdditions;
DataSet dsAdditions = new DataSet();
AdapterForAdditions.Fill(dsAdditions);
objConn.Close();

Tuesday, August 16, 2011

aspnet c# generate .pdf

this article helped me, hope it helps you too!
http://www.4guysfromrolla.com/articles/030911-1.aspx

I need to parse HTML into the pdf, the implementation is:

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Transactions.pdf");

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Document document = new Document(PageSize.A1, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(document, Response.OutputStream);

// Open the Document for writing
document.Open();

string documentContent = string.Empty;
document.NewPage();
documentContent = "Confirmation" + "
" + body;
StringReader sr = new StringReader(documentContent);
foreach (object item in HTMLWorker.ParseToList(sr, null).ToList())
{
document.Add((IElement)item);
}

document.Close();

Response.Write(document);
Response.End();

Thursday, August 11, 2011

How to query and display excel data by using ASP.NET, ADO.NET, and Visual C# .NET

found this article very helpful
http://support.microsoft.com/kb/306572/

I modified it a bit to work for me (I am using RadUpload to get the file):

// Create connection string variable. Modify the "Data Source"
// parameter as appropriate for your environment.
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + ruImporter.UploadedFiles[0].FileName+";" +
"Extended Properties=Excel 8.0;";

// Create connection object by using the preceding connection string.
OleDbConnection objConn = new OleDbConnection(sConnectionString);

// Open connection with the database.
objConn.Open();

// The code to follow uses a SQL SELECT command to display the data from the worksheet.

// Create new OleDbCommand to return data from worksheet.
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);


// Create new OleDbDataAdapter that is used to build a DataSet
// based on the preceding SQL SELECT statement.
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

// Pass the Select command to the adapter.
objAdapter1.SelectCommand = objCmdSelect;

// Create new DataSet to hold information from the worksheet.
DataSet objDataset1 = new DataSet();

// Fill the DataSet with the information from the worksheet.
objAdapter1.Fill(objDataset1);



// Clean up objects.
objConn.Close();

Tuesday, June 14, 2011

tsql split full name into firstname and lastname

this script only works with combination of firstname+' '+lastname.

--firstname
SUBSTRING(fullname, 1, CHARINDEX(' ', fullname) - 1) as firstname

--lastname
SUBSTRING(fullname, CHARINDEX(' ', fullname) + 1, LEN(fullname)) as lastname

Tuesday, June 07, 2011

tsql split up a comma seperated string into a table with duplicate records removed

thanks to many similar sources on the web, I came up with my script, it splits up a comma seperated string into a table with duplicate records removed



IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Spliter]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].Spliter
GO
CREATE FUNCTION Spliter(@String varchar(max), @Delimiter char(1))
returns @temptable TABLE (items varchar(max))
as
begin
declare @idx int
declare @slice varchar(8000)
select @idx = 1
if len(@String)<1 or @String is null return
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0 and (select count(items) from @temptable where items = @slice)=0)
insert into @temptable(Items) values(@slice)
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end
GO


SELECT i.items FROM dbo.Spliter(@tempstring,',') AS i where items not in (select comparesource from comparesourcetable)

Thursday, June 02, 2011

Stimulsoft Web Designer restrictions

found these properties quite handy when you want to give end user limited access when designing the report in web browser, especially when you want to hide the connection string credentials.

StiWebDesignerOptions.Dictionary.AllowModifyDictionary = true;
StiWebDesignerOptions.Dictionary.AllowModifyConnections = false;
StiWebDesignerOptions.Dictionary.ShowConnectionType = false;
StiWebDesignerOptions.Dictionary.AllowModifyDataSources = false;
StiWebDesignerOptions.Dictionary.AllowModifyVariables = true;


just put them before swdCustomReport.Design(report); in your code

Monday, May 30, 2011

MS SQL Server Management Studio 2008 cannot save table change

I got this message:
Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created. You have either made changes to a table that can't be re-created or enabled the option Prevent saving changes that require the table to be re-cretead.

the solution is:
Tools > Options > Designers > Table and Database Designers
unselect 'Prevent saving changes that require the table to be re-created' check box

Wednesday, May 25, 2011

find radtextbox and hide radtextbox in javascript



var rtbName = $find("<%= rtbName.ClientID %>");
rtbName.set_value('Hello World');

Wednesday, May 11, 2011

tsql remove leading zeros in a string that is 10 characters in length

SUBSTRING([TheString], patindex('%[^0]%',[TheString]), 10)

Monday, May 02, 2011

C# get opposite colour of a given colour

public static Color FindeOppositeColor(Color InputColour)
{
return Color.FromArgb(255 - InputColour.R, 255 - InputColour.G, 255 - InputColour.B);
}

Thursday, April 14, 2011

sometimes i need to open radwindow from server side, I use this code snippet from Telerik:


protected void Button1_Click(object sender, EventArgs e)
{
Telerik.WebControls.RadWindow newwindow = new Telerik.WebControls.RadWindow();
newwindow.ID = "RadWindow1";
newwindow.NavigateUrl = "http://www.google.com";
newwindow.VisibleOnPageLoad = true;
RadWindowManager1.Windows.Add(newwindow);
}


the intellisense in Visual Studio 2010 doesn't give me "width" or "height" when i try to see if it is allowed to specifiy size for radwindow. The answer is YES, forget about the intellisense, just type it in, there you are, worked!


newwindow.Width = Unit.Pixel(500);
newwindow.Height = Unit.Pixel(500);
newwindow.Behaviors = WindowBehaviors.Move | WindowBehaviors.Close;

Wednesday, March 30, 2011

internet explorer 8 developer tools window shows in task bar but not showing

what a frustrating 5 minutes....

try Windows key + Up key

enjoy

Tuesday, March 29, 2011

move all contents from one folder to another c#

oldPath and newPath should look like "c:/FolderABC" or "~/FolderABC"


System.IO.Directory.Move(@oldPath, @newPath);

Monday, March 21, 2011

sql shrink log files

found this useful to keep necessary usage of disk space with sql db installed

USE myDatabase
GO
DBCC SHRINKFILE(myDatabase_log, 1)
BACKUP LOG myDatabase WITH TRUNCATE_ONLY
DBCC SHRINKFILE(myDatabase_log, 1)

Tuesday, March 15, 2011

radeditor doesnt open properly when first randered within a grid html edit column

found the solution at:

http://www.telerik.com/community/forums/aspnet-ajax/editor/incorrect-rendering-of-radeditor-when-shown-with-ajax-in-initially-hidden-parent.aspx