Sunday, January 30, 2005

Microsoft Data Access Application Block

Microsoft Data Access Application Block has made programmers life easier, but at the same time it does not provide the flexibility which sometimes a hardcore programmer require. One thing I hate about Data Access Application block is that its very very hard to find bugs. If the bug is in my code I can find it and correct it but sometimes the DAB gives stupid errors that are very hard to remove.

Saturday, January 29, 2005

Returning Identity from the Database

Sometimes we need to return the next generated number of the identity column.

We can use simple Pl/SQl to achieve this:

SELECT @@IDENTITY FROM tblPerson

This will return the next generated value of the identity column of the tblPerson table.

Saturday, January 15, 2005

Inserting Images to the Database

This is the code to insert image into a database and also copy the image into the Folder on the server.


string strFileName = File1.PostedFile.FileName;
string c = System.IO.Path.GetFileName(strFileName);
try
{
File1.PostedFile.SaveAs(@"C:\ServerFolder\"+c);
Response.Write(@"C:\ServerFolder\"+c);
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
FileStream o = null;
StreamReader r = null;
string jpgFile = @"C:\ServerFolder\"+c;
o = new FileStream(jpgFile,FileMode.Open,FileAccess.Read,FileShare.Read);
r = new StreamReader(o);
try
{
byte[] FileByteArray = new byte[o.Length-1];
OleDbCommand CmdObj = new OleDbCommand("InsertImage",myConnection);
CmdObj.CommandType = CommandType.StoredProcedure;
CmdObj.Parameters.Add("@ImageUrl",OleDbType.VarChar,50);
CmdObj.Parameters["@ImageUrl"].Value = jpgFile;
CmdObj.Parameters.Add("@Image",OleDbType.Binary);
CmdObj.Parameters["@Image"].Value = FileByteArray;
myConnection.Open();
CmdObj.ExecuteNonQuery();
myConnection.Close();

}
catch(Exception ex)
{
Response.Write(ex.Message);
}

Saturday, January 01, 2005

Extracting URLS from Website and binding to datagrid

Here is a small snippet that you can use to get the links from a certain URL.


WebClient objWebClient = new WebClient();
aRequestHTML = objWebClient.DownloadData(strUrl);
UTF8Encoding utf8 = new UTF8Encoding();
myString = utf8.GetString(aRequestHTML);

Regex r = new Regex("href\\s*=\\s*(?:(?:\\\"(?[^\\\"]*)\\\")|(?[^\\s]* ))");
MatchCollection mcl = r.Matches(myString);
Response.Write(r.ToString());

foreach(Match ml in mcl)
{
foreach(Group g in ml.Groups)
{
string b = g.Value + "
";
a.Add(b);


}
}

DataGrid1.DataSource = a;
DataGrid1.DataBind();