this is the most common problem we face and most often we keep the page small in size and use less images.
But, what if my homepages a lot of stuffs to get loaded which is a must requirement? The only thing you can do it to Ajax it! you need to separate a page into several html block of codes and load it separately.
What i did is using the ASP.NET AJAX update panel to warp the html code and use timer to load it later. I think this is not really a correct why but it is better then doing nothing.
What else i can do? do you have any solution suggest for me?
Friday, June 6, 2008
Sunday, June 1, 2008
A simple way to get a content from an URL over the internet
sometimes we want to get the content over the internet and process the content, here is a super simple method that i discovered.
using System.Net;
using System.IO;
public static string GetWebContentString(string
url)
{
HttpWebRequest httpReq =
(HttpWebRequest)WebRequest.Create(url);
HttpWebResponse httpRes =
(HttpWebResponse)httpReq.GetResponse();
Stream fs =
httpRes.GetResponseStream();
StreamReader sr = new
StreamReader(fs);
return sr.ReadToEnd();
}
Monday, May 26, 2008
How to call store procedure using .NET
This is how I call store procedure using .NET having passing input value and return by output value
public static int GetSearchCount(string country)
{
int count = 0;
using (SqlConnection conn = new
SqlConnection(ConfigurationManager.ConnectionStrings["dbnamestr"].ConnectionString))
{
conn.Open();
SqlCommand objCmd = new
SqlCommand("sp_SearchCount", conn);
objCmd.CommandType =
CommandType.StoredProcedure;
SqlParameter paramTotal = new
SqlParameter("@Total", SqlDbType.Int);
paramTotal.Direction =
ParameterDirection.ReturnValue;
objCmd.Parameters.Add(paramTotal);
objCmd.Parameters.AddWithValue("@Country", country);
objCmd.ExecuteScalar();
count = (int)paramTotal.Value;
}
return
count;
}
Using stored procedure and inline sql in your application
Programmers always ask which is better and which should they choose. Is store procedure has better performance or inline sql with ORM is better at gaining productivity? Well depends who you are, what applications you develop, the answers are very different from person to person.
I use store procedure when I knew the query is complicated which requires multiple joins tables and I knew that i will query it very often, hence using store procedure will make a significant different in performance gains. For example, you run the optimized precompile sql statements. you delegate the computation of data within the sql server and minimize sql query back and forth and also you make your query pretty secured by using SP
Meanwhile using inline sql is not a good practise unless you are using Object Relational Mapping (ORM) tools to generate those inline sql for you and all you need is to use the objects created. It is the fastest and simple way to query and manipulate the database with the power of OOP. If you are using inline sql as a short cut way to query data, you gotta becareful and takes various precautions steps to overcome sql injection attacks. Sometimes a string of sql statement hard coded in your program can be a maintenance problem because it is quite difficult to test the validity of sql statement.
FYI, I use SP for complicated queries. I use inline sql (usually generated by ORM) for simple CRUD to direct display data that do not involve calculations. Query using ORM can be more complicated and requires more resources than using the SP, so just keep things simple and understandable.
I am not bias to one or another, I just want to point out that by using the right technology in your application will have a greater benefit of sticking to one technology. Tool is created for making us easier, if you found that the tool is great, just use it, otherwise get another alternative.
I use store procedure when I knew the query is complicated which requires multiple joins tables and I knew that i will query it very often, hence using store procedure will make a significant different in performance gains. For example, you run the optimized precompile sql statements. you delegate the computation of data within the sql server and minimize sql query back and forth and also you make your query pretty secured by using SP
Meanwhile using inline sql is not a good practise unless you are using Object Relational Mapping (ORM) tools to generate those inline sql for you and all you need is to use the objects created. It is the fastest and simple way to query and manipulate the database with the power of OOP. If you are using inline sql as a short cut way to query data, you gotta becareful and takes various precautions steps to overcome sql injection attacks. Sometimes a string of sql statement hard coded in your program can be a maintenance problem because it is quite difficult to test the validity of sql statement.
FYI, I use SP for complicated queries. I use inline sql (usually generated by ORM) for simple CRUD to direct display data that do not involve calculations. Query using ORM can be more complicated and requires more resources than using the SP, so just keep things simple and understandable.
I am not bias to one or another, I just want to point out that by using the right technology in your application will have a greater benefit of sticking to one technology. Tool is created for making us easier, if you found that the tool is great, just use it, otherwise get another alternative.
Thursday, April 3, 2008
Upload image to web server using webservice .NET
To transfer a file to the web server, it is not necessary to use FTP to upload. You can use webservice to do so. Here is the trick:
public string CreateFileToBase64String(string path)
{
Stream fs = new FileStream(path, FileMode.Open);
MemoryStream ms = new MemoryStream();
const int size = 4096;
byte[] bytes = new byte[4096];
int numBytes;
while ((numBytes = fs.Read(bytes, 0, size)) > 0)
ms.Write(bytes, 0, numBytes);
ms.Close();
fs.Close();
return Convert.ToBase64String(ms.GetBuffer());
}
public void CreateFileFromBase64String(string imageData, string path)
{
MemoryStream msf = new MemoryStream(Convert.FromBase64String(imageData));
Stream stem = new FileStream(path, FileMode.Create);
msf.WriteTo(stem);
msf.Close();
stem.Close();
}
you see things is so simple.
public string CreateFileToBase64String(string path)
{
Stream fs = new FileStream(path, FileMode.Open);
MemoryStream ms = new MemoryStream();
const int size = 4096;
byte[] bytes = new byte[4096];
int numBytes;
while ((numBytes = fs.Read(bytes, 0, size)) > 0)
ms.Write(bytes, 0, numBytes);
ms.Close();
fs.Close();
return Convert.ToBase64String(ms.GetBuffer());
}
public void CreateFileFromBase64String(string imageData, string path)
{
MemoryStream msf = new MemoryStream(Convert.FromBase64String(imageData));
Stream stem = new FileStream(path, FileMode.Create);
msf.WriteTo(stem);
msf.Close();
stem.Close();
}
you see things is so simple.
How to get web content from the web .NET
Sometimes I need to download the html content from the internet, therefore this method is very useful.
public static void GetWebContent(string url, string saveToPath)
{
if (IsValidUrl(url))
{
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse();
Stream fs = httpRes.GetResponseStream();
FileStream fss = new FileStream(saveToPath, FileMode.Create);
const int size = 4096;
byte[] bytes = new byte[4096];
int numBytes;
while ((numBytes = fs.Read(bytes, 0, size)) > 0)
fss.Write(bytes, 0, numBytes);
fss.Close();
}
}
public static void GetWebContent(string url, string saveToPath)
{
if (IsValidUrl(url))
{
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse();
Stream fs = httpRes.GetResponseStream();
FileStream fss = new FileStream(saveToPath, FileMode.Create);
const int size = 4096;
byte[] bytes = new byte[4096];
int numBytes;
while ((numBytes = fs.Read(bytes, 0, size)) > 0)
fss.Write(bytes, 0, numBytes);
fss.Close();
}
}
Find the image height and width using .NET
See as simple as this:
System.Drawing.Imaging.BitmapData bit = new
System.Drawing.Imaging.BitmapData();
int height = bit.Height;
Subscribe to:
Posts (Atom)