Tuesday, November 24, 2009

SMTP Server

To fix this, we will have to make sure that no other mailservers are running. Then we have to startup SMTP service (via services). After which, Inside Manager Server, "Manage Mail Server", "add a POP3 acccount". Use that as the default SMTP details.

Automatic Thumbnail C#

HtmlTable dTable = new HtmlTable();
dTable.Width = "500px";
dTable.CellPadding = 2;
dTable.CellSpacing = 0;
dTable.Border = 0;

int col = 5;

HtmlTableRow dTRow = new HtmlTableRow();
for (int count = 0; count < tmp.Length; count++)
{
if ((count % col == 0) && (count != 0))
{
dTable.Controls.Add(dTRow);
dTRow = new HtmlTableRow();
}
HtmlTableCell dTCell = new HtmlTableCell();
Photo result = tmp[count];
dTCell.InnerHtml = "

";
dTRow.Controls.Add(dTCell);
}

dTable.Controls.Add(dTRow);
Panel1.Controls.Add(dTable);

C# SQL Encode/Decode

public string SqlEncode(string inStr)
{
inStr = inStr.Replace("'", "''");
inStr = inStr.Replace("<", "<");
inStr = inStr.Replace(">", ">");
return inStr;
}
public string SqlDecode(string inStr)
{
if (inStr == "")
return null;

inStr = inStr.Replace("''", "'");
inStr = inStr.Replace("<", "<");
inStr = inStr.Replace(">", ">");

return inStr;
}

C# Binary Image Upload

// adapted from http://snippets.dzone.com/posts/show/1485
public static byte[] ResizeImageFile(byte[] imageFile, int targetSize)
{
System.Drawing.Image original = System.Drawing.Image.FromStream(new MemoryStream(imageFile));
int targetH, targetW;
if (original.Height > original.Width)
{
targetH = targetSize;
targetW = (int)(original.Width * ((float)targetSize / (float)original.Height));
}
else
{
targetW = targetSize;
targetH = (int)(original.Height * ((float)targetSize / (float)original.Width));
}
System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(new MemoryStream(imageFile));
// Create a new blank canvas. The resized image will be drawn on this canvas.
Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(72, 72);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel);
// Save out to memory and then to a file. We dispose of all objects to make sure the files don't stay locked.
MemoryStream mm = new MemoryStream();
bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
original.Dispose();
imgPhoto.Dispose();
bmPhoto.Dispose();
grPhoto.Dispose();
return mm.GetBuffer();
}


public int uploadImage()
{
int photonum = 1;
Stream imgStream = UploadFile.PostedFile.InputStream;
int imgLen = UploadFile.PostedFile.ContentLength;
string imgContentType = UploadFile.PostedFile.ContentType;
byte[] imgBinaryData = new byte[imgLen];
int n = imgStream.Read(imgBinaryData, 0, imgLen);
byte[] imgBinaryData2 = ResizeImageFile(imgBinaryData, 200);
string imgName = UploadFile.PostedFile.FileName;
int newImageLen = imgBinaryData2.Length;
String cdate = DateTime.Now.ToString();
int RowsAffected = SaveToDB(imgName, imgBinaryData2, imgContentType, newImageLen, cdate);
if (RowsAffected > 0)
{
OleDbDataReader rdr = null;
strConnection = ConfigurationManager.AppSettings["DBKey"];
String strSQL = "SELECT img_id FROM tblImages where created_date='" + cdate + "'";
conUser = new OleDbConnection(strConnection);
if (conUser.State.ToString().Equals("Open")) { conUser.Close(); }
conUser.Open();
cmdUser = new OleDbCommand(strSQL, conUser);
rdr = cmdUser.ExecuteReader();
while (rdr.Read())
{
photonum = rdr.GetInt32(0);
}
return photonum;
}

return -1;
}

C# Binary Image Upload

// adapted from http://snippets.dzone.com/posts/show/1485
public static byte[] ResizeImageFile(byte[] imageFile, int targetSize)
{
System.Drawing.Image original = System.Drawing.Image.FromStream(new MemoryStream(imageFile));
int targetH, targetW;
if (original.Height > original.Width)
{
targetH = targetSize;
targetW = (int)(original.Width * ((float)targetSize / (float)original.Height));
}
else
{
targetW = targetSize;
targetH = (int)(original.Height * ((float)targetSize / (float)original.Width));
}
System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(new MemoryStream(imageFile));
// Create a new blank canvas. The resized image will be drawn on this canvas.
Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(72, 72);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel);
// Save out to memory and then to a file. We dispose of all objects to make sure the files don't stay locked.
MemoryStream mm = new MemoryStream();
bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
original.Dispose();
imgPhoto.Dispose();
bmPhoto.Dispose();
grPhoto.Dispose();
return mm.GetBuffer();
}


public int uploadImage()
{
int photonum = 1;
Stream imgStream = UploadFile.PostedFile.InputStream;
int imgLen = UploadFile.PostedFile.ContentLength;
string imgContentType = UploadFile.PostedFile.ContentType;
byte[] imgBinaryData = new byte[imgLen];
int n = imgStream.Read(imgBinaryData, 0, imgLen);
byte[] imgBinaryData2 = ResizeImageFile(imgBinaryData, 200);
string imgName = UploadFile.PostedFile.FileName;
int newImageLen = imgBinaryData2.Length;
String cdate = DateTime.Now.ToString();
int RowsAffected = SaveToDB(imgName, imgBinaryData2, imgContentType, newImageLen, cdate);
if (RowsAffected > 0)
{
OleDbDataReader rdr = null;
strConnection = ConfigurationManager.AppSettings["DBKey"];
String strSQL = "SELECT img_id FROM tblImages where created_date='" + cdate + "'";
conUser = new OleDbConnection(strConnection);
if (conUser.State.ToString().Equals("Open")) { conUser.Close(); }
conUser.Open();
cmdUser = new OleDbCommand(strSQL, conUser);
rdr = cmdUser.ExecuteReader();
while (rdr.Read())
{
photonum = rdr.GetInt32(0);
}
return photonum;
}

return -1;
}

Form does not get value from readonly fields

To fix this, don't put readonly property for the controls by default, do it programatically. This has something to the storage of values for disabled properties. Apparently .NET does not store the value of disabled controls and hence does not pass the value on.

txtStartDate.Attributes.Add("readonly", "readonly");
txtExpDate.Attributes.Add("readonly", "readonly");

ASP.NET function calling from HTML

<%# formatColor(DataBinder.Eval(Container.DataItem, "status").ToString()) %>

Monday, November 23, 2009

HTML code for em-dash

is —

SQL Stored Procedure for Number Format

CREATE FUNCTION [dbo].[fnFormatPhoneNumber](@PhoneNo VARCHAR(20))
RETURNS VARCHAR(25)
AS
BEGIN
DECLARE @Formatted VARCHAR(25)

IF (LEN(@PhoneNo) >= 10)
SET @Formatted = LEFT(@PhoneNo, 3) + '.' + SUBSTRING(@PhoneNo, 4, 3) + '.' + SUBSTRING(@PhoneNo, 7, 4)
ELSE
SET @Formatted = @PhoneNo


RETURN @Formatted
END
GO
Use it as follows:
SELECT [dbo].[fnFormatPhoneNumber](PhoneNumber) AS PhoneNumber FROM SomeTable

Tuesday, November 17, 2009

Foreign Keys in PhpMyAdmin

Only available with innodb, set the relationships via normal 'linking' and then you can view the relationships via 'designer'.

http://www.phpmyadmin.net/documentation/#faqpdf

6.8 How can I produce a PDF schema of my database?

First the configuration variables "relation", "table_coords" and "pdf_pages" have to be filled in.

Then you need to think about your schema layout. Which tables will go on which pages?

* Select your database in the left frame.
* Choose "Operations" in the navigation bar at the top.
* Choose "Edit PDF Pages" near the bottom of the page.
* Enter a name for the first PDF page and click Go. If you like, you can use the "automatic layout," which will put all your linked tables onto the new page.
* Select the name of the new page (making sure the Edit radio button is selected) and click Go.
* Select a table from the list, enter its coordinates and click Save.
Coordinates are relative; your diagram will be automatically scaled to fit the page. When initially placing tables on the page, just pick any coordinates -- say, 50x50. After clicking Save, you can then use the graphical editor to position the element correctly.
* When you'd like to look at your PDF, first be sure to click the Save button beneath the list of tables and coordinates, to save any changes you made there. Then scroll all the way down, select the PDF options you want, and click Go.
* Internet Explorer for Windows may suggest an incorrect filename when you try to save a generated PDF. When saving a generated PDF, be sure that the filename ends in ".pdf", for example "schema.pdf". Browsers on other operating systems, and other browsers on Windows, do not have this problem.

Monday, November 16, 2009

SQL Namepipe issues

Try starting/restarting the sql server/sql server agent.

The SQL Server service terminated with service-specific error 126.
Start->All Programs -> Microsoft Sql server 2005 -> Configuration Tools -> SQL Server Configuration Manager.
In the SQL SERVER configuration Manager Window, click the plus (+) sign against SQL SERVER 2005 Network Configuration.
Highlight 'Protocols for MS SQL SERVER'
In the right pane you'd find VIA in protocol name column , you can right click it san select 'Disable'

Friday, November 13, 2009

SQL manipulating date time using CONVERT

SELECT status, event_id, title, CONVERT(VARCHAR(12),event_date,107) as edate, dbo.udf_DayOfWeek(event_date) AS DayOfWeek from tblEvent where Convert(datetime,Convert(varchar(12), event_date, 103),103) >= Convert(datetime,Convert(varchar(12), GETDATE(),103),103) order by event_date

SELECT status, event_id, title, CONVERT(VARCHAR(12),event_date,107) as edate, dbo.udf_DayOfWeek(event_date) AS DayOfWeek from tblEvent where Convert(datetime,Convert(VARCHAR(12), event_date, 103),103) < Convert(datetime,Convert(VARCHAR(12), GETDATE(),103),103) AND Convert(datetime,Convert(VARCHAR(12), event_date, 103),103) > Convert(datetime,datediff(day, 30, GETDATE()),103) order by event_date DESC

Wednesday, November 11, 2009

Unable to deploy war file

It seems that there is some permission issues with Windows Vista, if we run tomcat as administrator the problem would be solved. When deploying the .war file, make sure it is deployed within the webapp directory above ROOT. After running tomcat, the server would extract the files into a folder and then we should be able to run tomcat from then.

Sunday, November 8, 2009

Error loading feeds from flash and opening asp.net program without sln file

1. to load the xml feeds, make sure that the url is correct, then recompile it for projector mode (in exe)

2. within vs.net, create a new project and then import all the files from the other server into the folder (if you have subversion, commit it to SVN). The program should not work and should prompt errors mainly about being unable to find certain objects, even though it clearly exist.

The idea is that designer.cs file is missing for the individual document, hence you need to generate it by right clicking on the file and select the "Convert to web application" option. Sometimes if the designer file exist by default and it still throw the error, remove the designer file and try again.

empty datetime inside a datagrad

if (edate.Equals("")) {
param3.Value = DBNull.Value;
} else {
param3.Value = DateTime.Parse(edate);
}

submit form on enter key asp.net

can do so using the defaultbutton option inside asp.
form defaultbutton="button1" runat="server"

Friday, November 6, 2009

Creating user in SQL Server

INside Microsoft SQL Server Management after you login via windows authentication, select the security table and insert new user, you also need to assign userrole such as sysadmin to the user.

After that, you need to modify the properties of the connection instance to WINDOWS + SQL Server mode. Either that modify it via the Registry settings.

Creating user in SQL Server

INside Microsoft SQL Server Management after you login via windows authentication, select the security table and insert new user, you also need to assign userrole such as sysadmin to the user.

Export SQL Server to MDF

The best way to do so is by using SQL Script and not by any other methods.

To do so, simply go to the sql tools: "tasks" -> "generate script" -> remember to set the option for data export to be true.

VS.NET Namescape could not be found

Make sure that the class (which normally resides in app_code) has its build action set to compile and not just content.