Archive for September, 2010

Hi all,

In one of my projects, I have coded like this.

code behind>>>>>>>>>>>>>>>>>>>

protected void Button2_Click(object sender, EventArgs e)
{
if (fupLogo.HasFile)
{
string fileFormat = fupLogo.PostedFile.ContentType;
Label1.Text = fupLogo.PostedFile.ContentType;
if (string.Compare(fileFormat, "image/jpeg", true) == 0 ||
string.Compare(fileFormat, "image/png", true) == 0 ||
string.Compare(fileFormat, "image/gif", true) == 0)
{
Label1.Text += "file format supported
";
}
else
{
Label1.Text +="file format not supported
";
}
}
else
{
Label1.Text += "file not exist
";
}
}

protected void Button1_Click(object sender, EventArgs e)
{
fupLogo.Visible = true;
Button2.Visible=true;
}

this behaves like on page load it shows a button then the onclick of the
button it shows a fileupload and upload button

Expected: when i cliks upload button after selecting any file
ouput must be “file format supported/not suported”

Actual:
but the output is “file not exist”.

As a solution I just found these,
Use style=”display:none” instead of Visible=”False”. In this case PostBackTrigger for Button2 works correctly.
Example:
..aspx file
asp:FileUpload ID="fupLogo" runat="server" style="display:none"
asp:Button ID="Button2" runat="server" Text="Upload"
OnClick="Button2_Click" style="display:none"

..cs file
protected void Button1_Click(object sender, EventArgs e)
{
fupLogo.Style.Remove("display");
Button2.Style.Remove("display");
}

Hi all,

In one of my projects I just had to come over a requirement where I have a search page with a search button and a gridview control which displays the search results. The search criteria list is a long one so, when the user clicks the search button, he/she may not even know that results have been displayed or not unless he/she scrolls down the page. In this example, I have the code for displaying the search results in button’s click event. So when the user clicks the button, the grid will get filled with the results and also a smooth scrolling is made to the position where the grid is placed.

For the above, you have just include these JQuery files.
http://code.jquery.com/jquery.min.js
http://gsgd.co.uk/sandbox/jquery/easing/

After including these files, just put the following code in the Page_Load
btnSearch.Attributes.Add("onclick", " $('html, body').stop().animate({scrollTop: $('#divCandidate').offset().top}, 5000, 'easeInOutExpo');;return true;");

In the above code “divCandidate” is the div in which the gridview is residing. 5000 is the scrolling time interval.

Also, make sure the criteria list (most probably, a table containing some controls) and the gridview are resided in an update panel.

Happy Scrolling!!

A JScript/VBScript Regex Lookahead Bug

Posted: September 6, 2010 in ASP.NET

Here’s one of the oddest and most significant regex bugs in Internet Explorer. It can appear when using optional elision within lookahead (e.g., via ?, *, {0,n}, or (.|); but not +, interval quantifiers starting from one or higher, or alternation without a zero-length option). An example in JavaScript:

/(?=a?b)ab/.test("ab");
// Should return true, but IE 5.5 – 8b1 return false

/(?=a?b)ab/.test("abc");
// Correctly returns true (even in IE), although the
// added "c" does not take part in the match

Thanks to a blog post by Steve that describes the bug with a password-complexity regex. However, the bug description there is incomplete and subtly incorrect, as shown by the above, reduced test case.

Fortunately, since the bug is predictable, it’s usually possible to work around. For example, you can avoid the bug with the password regex in Michael’s post (/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,15}$/) by writing it as /^(?=.{8,15}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*/ (the .{8,15}$ lookahead must come first here). The important thing is to be aware of the issue, because it can easily introduce latent and difficult to diagnose bugs into your code. Just remember that it shows up with variable-length lookahead. If you’re using such patterns, test the hell out of them in IE.

Passing array to stored procedure

Posted: September 1, 2010 in ASP.NET

Consider the following simple method for defining the stored procedure using dynamic SQL. The array parameter is defined simply as a string and the input will be expected to be comma-delimited. By forming the sql dyanmically with the input string, we can query against the values in the array by using the IN command.

Stored Procedure
CREATE PROCEDURE [dbo].[GetData]
@MyCodes as varchar(500) = ”, — comma delimited list of codes, ie: ”’ABC”, ”DEF”, ”GHI”’
AS
BEGIN
DECLARE @query as nvarchar(500)

set @query = ‘SELECT * FROM DATA WHERE Code IN (@p_MyCodes)’

exec SP_EXECUTESQL @query,
N’@p_MyCodes varchar(500)’,
@p_MyCodes = @MyCodes
END

The above stored procedure definition will accept a comma-delimited string, which we process as an array using the SQL IN command. Note, we had to use dyanmic SQL to properly form the query (which involves expanding the comma-delimited string).

Next, we need to define the method to pass the data and execute the stored procedure from C# .NET.

The first step is to convert our array of data into a comma-delimited string, which is what the stored procedure expects to receive. Depending on your data type, this code may vary. For this example, we are using a .NET collection.

string myCodes = string.Empty; // Initialize a string to hold the comma-delimited data as empty

foreach (MyItem item in MyCollection)
{
if (myCodes.Length > 0)
{
myCodes += ", "; // Add a comma if data already exists
}

myCodes += "'" + item.Name + "'";
}

The code above will create a string in the following format:
‘Blue’,’Green’,’Red’

Now that the collection has been converted to a string, we can pass the value as a parameter to the stored procedure by using the following code:

using System;
using System.Data;
using System.Data.SqlClient;

SqlConnection MyConnection = null;
SqlDataReader MyReader = null;

try
{
// Create the SQL connection.
MyConnection = new SqlConnection("Server=(local);DataBase=Northwind;Integrated Security=SSPI"))
MyConnection.Open();

// Create the stored procedure command.
SqlCommand MyCommand = new SqlCommand("GetData", MyConnection);

// Set the command type property.
MyCommand.CommandType = CommandType.StoredProcedure;


// Pass the string (array) into the stored procedure.
MyCommand.Parameters.Add(new SqlParameter("@MyCodes", myCodes));

// Execute the command
MyReader = MyCommand.ExecuteReader();

// ...
}

catch (Exception excep)
{
}

finally
{
if (MyReader != null)
{
MyReader.Close();
MyReader.Dispose();
}

if (MyConnection != null)
{
MyConnection.Close();
MyConnection.Dispose();
}

}