Links
Calendar
<<  August 2008  >>
MoTuWeThFrSaSu
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567
Site statistics
Keywords

GridView  - Auto Generated SQL Insert, Update And Delete Statements

ASP.NET 2.0 with SQL Server 2005

I recently ran into the error ‘Must declare the scalar variable “@ID” ’ when attempting to delete rows from a GridView.  I had created the GridView via the smart tag and wizard options, so for a while I was stumped as to what the was the cause of the problem. I felt I knew what the error message was indicating, but could not figure out why it was occurring. A little Googling confirmed my suspicion that the parameter @ID was not being set in the SQL generated by the SQLDataSource but none of the posts I read revealed how I might fix it.

Because I had built the page using the various wizards I thought there was nothing further for me to do, but after a little further digging I discovered I had not set the DataKeyNames property for the GridView. Without this property set, the GridView cannot automatically wire up the parameter values and thus the generated SQL queries fail.

Double check the DataKeyNames property should you be experiencing this problem.

Depending how you have created the GridView and SQLDataSource, you may have to manually set the parameters in response to an SQLDataSource Inserting, Updating or Deleting event.


Pasting from here! I think this is useful article.

Currently rated 1.0 by 1 people

  • Currently 1/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
rezashirazi , posted on 30. June 2008, 14:33

ASP.NET States dropdown list:

<asp:DropDownList id="UserState" runat="server">
<asp:ListItem Value="" Selected="true">Select State / Province</asp:ListItem>
<asp:ListItem Value="">—- US States —-</asp:ListItem>
<asp:ListItem Value="AL">Alabama</asp:ListItem>
<asp:ListItem Value="AK">Alaska</asp:ListItem>
<asp:ListItem Value="AZ">Arizona</asp:ListItem>
<asp:ListItem Value="AR">Arkansas</asp:ListItem>
<asp:ListItem Value="CA">California</asp:ListItem>
<asp:ListItem Value="CO">Colorado</asp:ListItem>
<asp:ListItem Value="CT">Connecticut</asp:ListItem>
<asp:ListItem Value="DC">District of Columbia</asp:ListItem>
<asp:ListItem Value="DE">Delaware</asp:ListItem>
<asp:ListItem Value="FL">Florida</asp:ListItem>
<asp:ListItem Value="GA">Georgia</asp:ListItem>
<asp:ListItem Value="HI">Hawaii</asp:ListItem>
<asp:ListItem Value="ID">Idaho</asp:ListItem>
<asp:ListItem Value="IL">Illinois</asp:ListItem>
<asp:ListItem Value="IN">Indiana</asp:ListItem>
<asp:ListItem Value="IA">Iowa</asp:ListItem>
<asp:ListItem Value="KS">Kansas</asp:ListItem>
<asp:ListItem Value="KY">Kentucky</asp:ListItem>
<asp:ListItem Value="LA">Louisiana</asp:ListItem>
<asp:ListItem Value="ME">Maine</asp:ListItem>
<asp:ListItem Value="MD">Maryland</asp:ListItem>
<asp:ListItem Value="MA">Massachusetts</asp:ListItem>
<asp:ListItem Value="MI">Michigan</asp:ListItem>
<asp:ListItem Value="MN">Minnesota</asp:ListItem>
<asp:ListItem Value="MS">Mississippi</asp:ListItem>
<asp:ListItem Value="MO">Missouri</asp:ListItem>
<asp:ListItem Value="MT">Montana</asp:ListItem>
<asp:ListItem Value="NE">Nebraska</asp:ListItem>
<asp:ListItem Value="NV">Nevada</asp:ListItem>
<asp:ListItem Value="NH">New Hampshire</asp:ListItem>
<asp:ListItem Value="NJ">New Jersey</asp:ListItem>
<asp:ListItem Value="NM">New Mexico</asp:ListItem>
<asp:ListItem Value="NY">New York</asp:ListItem>
<asp:ListItem Value="NC">North Carolina</asp:ListItem>
<asp:ListItem Value="ND">North Dakota</asp:ListItem>
<asp:ListItem Value="OH">Ohio</asp:ListItem>
<asp:ListItem Value="OK">Oklahoma</asp:ListItem>
<asp:ListItem Value="OR">Oregon</asp:ListItem>
<asp:ListItem Value="PA">Pennsylvania</asp:ListItem>
<asp:ListItem Value="RI">Rhode Island</asp:ListItem>
<asp:ListItem Value="SC">South Carolina</asp:ListItem>
<asp:ListItem Value="SD">South Dakota</asp:ListItem>
<asp:ListItem Value="TN">Tennessee</asp:ListItem>
<asp:ListItem Value="TX">Texas</asp:ListItem>
<asp:ListItem Value="UT">Utah</asp:ListItem>
<asp:ListItem Value="VT">Vermont</asp:ListItem>
<asp:ListItem Value="VA">Virginia</asp:ListItem>
<asp:ListItem Value="WA">Washington</asp:ListItem>
<asp:ListItem Value="WV">West Virginia</asp:ListItem>
<asp:ListItem Value="WI">Wisconsin</asp:ListItem>
<asp:ListItem Value="WY">Wyoming</asp:ListItem>
<asp:ListItem Value="">—- Canadian Provinces —-</asp:ListItem>
<asp:ListItem Value="AB">Alberta</asp:ListItem>
<asp:ListItem Value="BC">British Columbia</asp:ListItem>
<asp:ListItem Value="MB">Manitoba</asp:ListItem>
<asp:ListItem Value="NB">New Brunswick</asp:ListItem>
<asp:ListItem Value="NL">Newfoundland and Labrador</asp:ListItem>
<asp:ListItem Value="NT">Northwest Territories</asp:ListItem>
<asp:ListItem Value="NS">Nova Scotia</asp:ListItem>
<asp:ListItem Value="NU">Nunavut</asp:ListItem>
<asp:ListItem Value="ON">Ontario</asp:ListItem>
<asp:ListItem Value="PE">Prince Edward Island</asp:ListItem>
<asp:ListItem Value="QC">Quebec</asp:ListItem>
<asp:ListItem Value="SK">Saskatchewan</asp:ListItem>
<asp:ListItem Value="YT">Yukon</asp:ListItem>
</asp:DropDownList>

 Another way is adding states onSelectedIndexChange of the country dropdownlist, this is the code:

Private Sub fillStates()

Select Case cboCountry.SelectedValue

Case "US"

With cboState.Items

.Clear()

.Add(
New System.Web.UI.WebControls.ListItem("Alabama", "AL"))

.Add(New System.Web.UI.WebControls.ListItem("Alaska", "AK")).Add(

New System.Web.UI.WebControls.ListItem("Arizona", "AZ"))

.Add(New System.Web.UI.WebControls.ListItem("Arkansas", "AR")).Add(

New System.Web.UI.WebControls.ListItem("California", "CA"))

.Add(New System.Web.UI.WebControls.ListItem("Colorado", "CO")).Add(

New System.Web.UI.WebControls.ListItem("Connecticut", "CT"))

.Add(New System.Web.UI.WebControls.ListItem("District of Columbia", "DC")).Add(

New System.Web.UI.WebControls.ListItem("Delaware", "DE"))

.Add(New System.Web.UI.WebControls.ListItem("Florida", "FL")).Add(

New System.Web.UI.WebControls.ListItem("Georgia", "GA"))

.Add(New System.Web.UI.WebControls.ListItem("Hawaii", "HI")).Add(

New System.Web.UI.WebControls.ListItem("Idaho", "ID"))

.Add(New System.Web.UI.WebControls.ListItem("Illinois", "IL")).Add(

New System.Web.UI.WebControls.ListItem("Indiana", "IN"))

.Add(New System.Web.UI.WebControls.ListItem("Iowa", "IA")).Add(

New System.Web.UI.WebControls.ListItem("Kansas", "KS"))

.Add(New System.Web.UI.WebControls.ListItem("Kentucky", "KY")).Add(

New System.Web.UI.WebControls.ListItem("Louisiana", "LA"))

.Add(New System.Web.UI.WebControls.ListItem("Maine", "ME")).Add(

New System.Web.UI.WebControls.ListItem("Maryland", "MD"))

.Add(New System.Web.UI.WebControls.ListItem("Massachusetts", "MA")).Add(

New System.Web.UI.WebControls.ListItem("Michigan", "MI"))

.Add(New System.Web.UI.WebControls.ListItem("Minnesota", "MN")).Add(

New System.Web.UI.WebControls.ListItem("Mississippi", "MS"))

.Add(New System.Web.UI.WebControls.ListItem("Missouri", "MO")).Add(

New System.Web.UI.WebControls.ListItem("Montana", "MT"))

.Add(New System.Web.UI.WebControls.ListItem("Nebraska", "NE")).Add(

New System.Web.UI.WebControls.ListItem("Nevada", "NV"))

.Add(New System.Web.UI.WebControls.ListItem("New Hampshire", "NH")).Add(

New System.Web.UI.WebControls.ListItem("New Jersey", "NJ"))

.Add(New System.Web.UI.WebControls.ListItem("New Mexico", "NM")).Add(

New System.Web.UI.WebControls.ListItem("New York", "NY"))

.Add(New System.Web.UI.WebControls.ListItem("North Carolina", "NC")).Add(

New System.Web.UI.WebControls.ListItem("North Dakota", "ND"))

.Add(New System.Web.UI.WebControls.ListItem("Ohio", "OH")).Add(

New System.Web.UI.WebControls.ListItem("Oklahoma", "OK"))

.Add(New System.Web.UI.WebControls.ListItem("Oregon", "OR")).Add(

New System.Web.UI.WebControls.ListItem("Pennsylvania", "PA"))

.Add(New System.Web.UI.WebControls.ListItem("Rhode Island", "RI")).Add(

New System.Web.UI.WebControls.ListItem("South Carolina", "SC"))

.Add(New System.Web.UI.WebControls.ListItem("South Dakota", "SD")).Add(

New System.Web.UI.WebControls.ListItem("Tennessee", "TN"))

.Add(New System.Web.UI.WebControls.ListItem("Texas", "TX")).Add(

New System.Web.UI.WebControls.ListItem("Utah", "UT"))

.Add(New System.Web.UI.WebControls.ListItem("Vermont", "VT")).Add(

New System.Web.UI.WebControls.ListItem("Virginia", "VA"))

.Add(New System.Web.UI.WebControls.ListItem("Washington", "WA")).Add(

New System.Web.UI.WebControls.ListItem("West Virginia", "WV"))

.Add(New System.Web.UI.WebControls.ListItem("Wisconsin", "WI")).Add(

New System.Web.UI.WebControls.ListItem("Wyoming", "WY"))

End With

Case "CA"

With cboState.Items

.Clear()

.Add(
New System.Web.UI.WebControls.ListItem("Alberta", "AB"))

.Add(New System.Web.UI.WebControls.ListItem("British Columbia", "BC")).Add(

New System.Web.UI.WebControls.ListItem("Manitoba", "MB"))

.Add(New System.Web.UI.WebControls.ListItem("New Brunswick", "NB")).Add(

New System.Web.UI.WebControls.ListItem("Newfoundland and Labrador", "NL"))

.Add(New System.Web.UI.WebControls.ListItem("Northwest Territories", "NT")).Add(

New System.Web.UI.WebControls.ListItem("Nova Scotia", "NS"))

.Add(New System.Web.UI.WebControls.ListItem("Nunavut", "NU")).Add(

New System.Web.UI.WebControls.ListItem("Ontario", "ON"))

.Add(New System.Web.UI.WebControls.ListItem("Prince Edward Island", "PE")).Add(

New System.Web.UI.WebControls.ListItem("Quebec", "QC"))

.Add(New System.Web.UI.WebControls.ListItem("Saskatchewan", "SK")).Add(

New System.Web.UI.WebControls.ListItem("Yukon", "YT"))

End With

End Select

End Sub

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Hi ,

 I find a solution for adding websites to IIS with asp.net, this is very amazing! the first step is adding Microsoft.Web.Administration.dll to your project's refrences, it is at "windir\system32\inetsrv\" folder.

 Here is the code:

C# version:

using System;

using Microsoft.Web.Administration;

class Program

{

static void Main(string[] args)

{

ServerManager mgr =
new ServerManager();

mgr.Sites.Add("MyWebSite", "http", "198.1.1.0:80:",

@"d:\MyWebSiteRootWebAppRootVirDir");

mgr.CommitChanges();

}

}

 VB.NET version:

Imports Microsoft.VisualBasic

Imports Microsoft.Web.Administration

Imports System.Configuration

Public Class clsCreateWebsite

Public Function createWebsite(ByVal websiteName As String, ByVal domainName As String, ByVal physicalPath As String) As Boolean

Dim IIS_manager As New ServerManager

Try

IIS_manager.Sites.Add(websiteName, "http", "*:80:" + domainName, physicalPath) IIS_manager.CommitChanges()

Return True

Catch ex As Exception

Return False

Finally

IIS_manager = Nothing

End Try

End Function

End Class

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

This is an error I get in my asp.net page when I want to bing my repeater to datasource. I have a hyperlink component in my page, look at the below code:

 <asp:HyperLink ID="HyperLink2" runat="server" Text="Details..." NavigateUrl='<%# "product_details.aspx?id=" + Eval("ID") %>'></asp:HyperLink>

I get this error because the type of "ID" field is numeric, so ASP.NET can not concat it with string value, I change the code and it works!

<asp:HyperLink ID="HyperLink2" runat="server" Text="Details..." NavigateUrl='<%# "product_details.aspx?id=" + cstr(Eval("ID")) %>'></asp:HyperLink>

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Hi,

 CustomValidator is very useful control for validating your form's controls.For example if you want to check if user entered at least 5 character in  password field you can use CustomValidator1_ServerValidate event :

Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate

If txtPassword.Text.Length >= 5 Then

args.IsValid = True

Else

args.IsValid = False

End If

End Sub

Good luck!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Hi ,

 If you want to raise the click event of a button by press enter key on a textbox in your asp.net form. you can easily add a <asp:panel> on your page and set the defaultButton property of it to your button. for example see below code :

<asp:Panel ID="pnlSearch" runat="server" DefaultButton="btnSearch">

<table width="100%">

<tr>

<td><asp:TextBox ID="txtSearch" runat="server" Width="128px"></asp:TextBox>

</td>

<td>

<asp:ImageButton ID="btnSearch" runat="server" ImageUrl="~/images/search.png" /></td>

</tr>

</table>

</asp:Panel>

Currently rated 1.0 by 1 people

  • Currently 1/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
rezashirazi , posted on 19. April 2008, 12:09

Hi ,

 In previous year I wrote a photo mosaic maker engine with a fast and flexible algorithm, It is now online! you can check it here.

 PicArtia helps you create your photo mosaic online for free. It’s fast and easy to use.

 Enjoy it!

Currently rated 1.0 by 1 people

  • Currently 1/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
SELECT email,
COUNT(email) AS NumOccurrences
FROM users
GROUP BY email
HAVING ( COUNT(email) > 1 )

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
What Regular Expression?
A regular expression is a pattern that can match various text strings, used for validations.

Where and when to use Regular Expression?
It can be used in the programming languages which supports or has regular expression class as in built or it supports third party regular expression libraries.

Regular expressions can be used to valid different type of data without increase the code with if and case conditions. A number of if conditions can be omitted with single line of regular expression checking.

Benefits of Regular Expression:
The following are benefits (not all included) of use of Regular Expression.
a) # line of code can be reduced.
b) Speed Coding.
c) Easy maintenance (you don’t need to change if validation criteria changes, just check the regular expression string).
d) Easy to understand (you don’t need to understand the programmer logic on large if statements and case statements).

Elements of Regular Expression:
Here are the basic elements of regular expression characters/literals, which can be used to build big regular expressions:

^ ---->Start of a string.
$ ---->End of a string.
. ----> Any character (except \n newline)
{...}----> Explicit quantifier notation.
[...] ---->Explicit set of characters to match.
(...) ---->Logical grouping of part of an expression.
* ---->0 or more of previous expression.
+ ---->1 or more of previous expression.
? ---->0 or 1 of previous expression; also forces minimal matching when an expression might match several strings within a search string.
\ ---->Preceding one of the above, it makes it a literal instead of a special character. Preceding a special matching character, see below.
\w ----> matches any word character, equivalent to [a-zA-Z0-9]
\W ----> matches any non word character, equivalent to [^a-zA-Z0-9].
\s ----> matches any white space character, equivalent to [\f\n\r\v]
\S----> matches any non-white space characters, equivalent to [^\f\n\r\v]
\d ----> matches any decimal digits, equivalent to [0-9]
\D----> matches any non-digit characters, equivalent to [^0-9]

\a ----> Matches a bell (alarm) \u0007.
\b ----> Matches a backspace \u0008 if in a [] character class; otherwise, see the note following this table.
\t ---->Matches a tab \u0009.
\r ---->Matches a carriage return \u000D.
\v ---->Matches a vertical tab \u000B.
\f ---->Matches a form feed \u000C.
\n ---->Matches a new line \u000A.
\e ---->Matches an escape \u001B

$number ----> Substitutes the last substring matched by group number number (decimal).
${name} ----> Substitutes the last substring matched by a (? ) group.
$$ ----> Substitutes a single "$" literal.
$& ----> Substitutes a copy of the entire match itself.
$` ----> Substitutes all the text of the input string before the match.
$' ----> Substitutes all the text of the input string after the match.
$+ ----> Substitutes the last group captured.
$_ ----> Substitutes the entire input string.

(?(expression)yes|no) ----> Matches yes part if expression matches and no part will be ommited.


Simple Example:
Let us start with small example, taking integer values:
When we are talking about integer, it always has fixed series, i.e. 0 to 9 and we will use the same to write this regular expression in steps.

a) Regular expression starts with “^”
b) As we are using set of characters to be validated, we can use [].
c) So the expression will become “^[1234567890]”
d) As the series is continues we can go for “-“ which gives us to reduce the length of the expression. It becomes “^[0-9]”
e) This will work only for one digit and to make it to work for n number of digits, we can use “*”, now expression becomes “^[0-9]*”
f) As with the starting ending of the expression should be done with “$”, so the final expression becomes “^[0-9]*$”

Note: Double quotes are not part of expression; I used it just to differentiate between the sentences.

Is this the way you need to write:
This is one of the way you can write regular expression and depending on the requirements and personal expertise, regular expression could be compressed much shorter, for example above regular expression could be reduced as.

a) Regular expression starts with “^”
b) As we are checking for the digits, there is a special character to check for digits “\d”
c) And digits can follow digits , we use “*”
d) As expression ends with “$”, the final regular expression will become
"^\d*$”

Digits can be validated with different ways of regular expressions:

1) ^[1234567890]*$
2) ^[0-9]*$
3) ^\d*$

Which one to choose?
Every one of above expressions will work in the same way, choose the way you are comfort, it is always recommended to have a smaller and self expressive and understandable, as these will effect when you write big regular expression.

Example on exclude options:
There are many situation which demands us to exclude only certain portion or certain characters,
Eg: a) Take all alpha numeric and special symbols except “&&#8221;
b) Take all digits except “7”
then we cannot prepare a big list which includes all instead we use the symbol of all and exclude the characters / symbols which need to be validated.
Eg: “^\w[^&]*$” is the solution to take all alpha numeric and special symbols except “&&#8221;.

Other Examples:
a) There should not be “1” as first digit,?
^[^1]\d*$ ? this will exclude 1 as first digit.

b) There should not be “1” at any place?
^\d[^1]*$ ? this will exclude the 1 at any place in the sequence.

Note: Here ^ operator is used not only to start the string but also used to negate the values.

Testing of Regular expression:
There are several ways of testing this
a) You can write a windows based program.
b) You can write a web based application.
c) You can even write a service based application.


Windows base sample code:
Here are steps which will be used for regular expression checking in dotNet:

a) Use System.Text.RegularExpression.Regex to include the Regex class.
b) Create an Regex object as follows:
Regex regDollar= new System.Text.RegularExpressions.Regex("^[0-9]*$ ");
c) Call the IsMatch(string object) of the Regex call, which will return true or flase.
d) Depending on the return state you can decide whether passed string is valid for regular expression or not.]

Here is the snap shot code as function:

Public boolean IsValid(string regexpObj, string passedString)
{
//This method is direct method without any exceptional throwing..
Regex regDollar= new System.Text.RegularExpressions.Regex(regexpObj);
return regDollar.IsMatch(passedString);
}

With minor changes to the above function it can be used in windows or webbased or even as a service.

Another way -- Online checking:
At last if you are fed up with above and you have internet connection and you don’t have time to write sample, use the following link to test online

http://www.regexplib.com/RETester.aspx


MORE INFO:
You can find more information on these type of characters at

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconcharacterescapes.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconcharacterclasses.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpcongroupingconstructs.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconcharacterclasses.asp

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Symptom:

When entering a value with angled brackets into a text box on a .NET application the following error is generated in the browser:

Server Error in '/<folder>' Application.

A potentially dangerous Request.Form value was detected from the client (TextBoxN="...")

Cause

The .NET framework is throwing up an error because it detected something in the entered text which looks like an HTML statement. The text doesn't need to contain valid HTML, just anything with opening and closing angled brackets ("<...>").

The reason behind the error is as a security precaution. Developers need to be aware that users might try to inject HTML (or even a script) into a text box which may affect how the form is rendered. For further details see www.asp.net/learn/whitepapers/request-validation/.

This checking was not performed in the .NET 1.0 framework and was introduced with the .NET 1.1 framework.

Remedy:

The remedy is in two parts and you MUST action both:

  1. To disable request validation on a page add the following directive to the existing "page" directive in the file - you will need to switch to the HTML view for this:

    ValidateRequest="false"

    for example if you already have:

    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="MyForm.aspx.vb" Inherits="Proj.MyForm"%>

    then this should become:

    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="MyForm.aspx.vb" Inherits="Proj.MyForm" ValidateRequest="false"%>

    Alternately, you can globally turn request validation off (but in which case be sure to implement item two below). To globally turn request validation off add the following to your web.config file:

    <pages validateRequest="false" />

    this should go within the <system.web> section. This will turn off request validation for every page in your application.

    Warning

    With request validation turned off, users will be able to enter html into text boxes on the page. For example entering:

    <script>alert('Oops!')</script>