Monday, August 10, 2009

Useful Techniques in Asp.Net

1. Finding the particular Data from the Dataset,Its Row Position and Current Page in Grid
DataSet dsQuoteList = objQuote.GetQuoteBankList();

Giving a constraints to the first column that is the ID primary key like:

dsQuoteList.Tables[0].Constraints.Add("pk", dsQuoteList.Tables[0].Columns[0], true);


The below code will find the perticular ID int the given dataset and return new Datarow..

DataRow dr = dsQuoteList.Tables[0].Rows.Find("10540");

Now finding the Row position (RowIndex) in the Dataset, like..:

int RowIndx = int.Parse(dsQuoteList.Tables[0].Rows.IndexOf(dr).ToString()

Finally, You will get the exact position of particular Data in the Dataset.

In Addition You can also find the Current Page in the Datagrid or GridView after getting Datarow:

int PageSize = 10;

double CurrPage = Math.Ceiling(Convert.ToDouble(int.Parse(dsQuoteList.Tables[0].Rows.IndexOf(dr).ToString()) / PageSize ));


That's it !

2. Find the Exact words from string

using System.Text;
using System.Text.RegularExpressions;

Write in the Button Click Event :

private void button1_click(object sender, EventArgs e)
string strInput = "Hello World I am Here with the new sunrise";
string strOutput = FindFirstWords(strInput, 4);

// Here the result of strOutput variable is : Hello World I am

Function to get the Words from string:

private string FindFirstWords(string input, int HowManyFind)
string REGEX = @"([\w]+\s+){" + HowManyFind + "}";

StringBuilder output = new StringBuilder();

foreach (Capture capture in Regex.Match(input, REGEX).Captures)
return output.ToString();

3. foreach loop in Enum

objDataImport = new DataImport();
foreach (string s in Enum.GetNames(typeof(DataImport.eTotalFeature)))
objDataImport.CurrentFeature = (DataImport.eTotalFeature)Enum.Parse(typeof(DataImport.eTotalFeature), s);

4. Read line by line from text file in C#

string strFile = @"C:\Files\new.txt";

StreamReader objStreamReader = File.OpenText(strFile);

string strGetLine;
while ((strGetLine = objStreamReader.ReadLine()) != null)

How to download any file with Asp.Net

string FileName = Server.MapPath("style.css").ToString();

Response.ContentType = "text/plain";
Response.AddHeader("Content-Disposition", "attachment; filename=Name.css;");

byte[] buffer = System.IO.File.ReadAllBytes(FileName);

System.IO.MemoryStream mem = new System.IO.MemoryStream();
mem.Write(buffer, 0, buffer.Length);


Render GridView to Html String

In many times I have faced a problem when you want to
render html string for GridView Control, but you
probably find error like...

"Control '' of type 'GridView' must be placed inside a form tag with runat=server."

In this case you have to use one method to avoid this error..

public override void VerifyRenderingInServerForm(Control control)
// Confirms that an HtmlForm control is rendered for the
// specified ASP.NET server control at run time.

// No code required here.

and in the page directives just add this code marked in Bold Text..

No comments: