Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 28th, 2007, 05:51 AM
Authorized User
 
Join Date: May 2007
Posts: 95
Thanks: 5
Thanked 0 Times in 0 Posts
Default Failed to convert parameter value from a String to

Hi All

I am just a novice in C# and got stuck on one point. I am using Javascript to validate my input in textbox1, The input has to be in numeric only. regular expression used in javascript is RE = new RegExp("[0-9]"). But when i am entering alphanumeric value, i am getting error as Failed to convert parameter value from a String to a Int32.

I am buiding a login page using duwamish architecture.

my access.cs code is:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace DataAccess
{
    public class Access
    {
        public bool LoginDetails(string struserid, string strpassword)
        {

            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
         @"Data source= C:\Documents and Settings\10082\" +
         @"My Documents\user.mdb";
            OleDbCommand cmd = new OleDbCommand("select Name,Department,Vertical,Location from user1 where userid=@userid and password=@password", conn);
            cmd.Parameters.Add("@userid", OleDbType.Integer, 10);
            cmd.Parameters["@userid"].Value = struserid;
            cmd.Parameters.Add("@password", OleDbType.WChar, 50);
            cmd.Parameters["@password"].Value = strpassword;
            conn.Open();
            OleDbDataReader reader = cmd.ExecuteReader();

            if (reader.HasRows)
                return true;
            else
                return false;

            //OleDbDataAdapter ad = new OleDbDataAdapter(cmd);
            //DataSet ds = new DataSet();
            //ad.Fill(ds, "user1");

            // Verify whether the user is existing from the returned dataset.

            // In case no user details exist with the provided credentials,
            //return false. Otherwise true.


        }

        public DataSet GetDetails(string struserid)
        {
            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
         @"Data source= C:\Documents and Settings\10082\" +
         @"My Documents\user.mdb";
            string strSelectQuery = "select Name,Department,Vertical,Location from user1 where userid=@userid";
            OleDbCommand cmd1 = new OleDbCommand(strSelectQuery, conn);
            cmd1.Parameters.Add("@userid", OleDbType.Integer, 10);
            cmd1.Parameters["@userid"].Value = struserid;
            //cmd.Parameters.Add("@password", OleDbType.WChar, 50);
            //cmd.Parameters["@password"].Value = strpassword;
            conn.Open();
            OleDbDataAdapter ad = new OleDbDataAdapter(cmd1);
            DataSet ds = new DataSet();
            ad.Fill(ds);
            return ds;


        }

    }
}


Please help....

 
Old November 28th, 2007, 06:24 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

If you are sure your string only contains digits, and as you haven't shown the script that validates it's hard to tell, then try:
Code:
int myInt;
bool isInteger = int.TryParse(myString, out myInt);
if (isInteger)
{
  //myInt now holds legitimate value
}
else
{
  //error
}
--

Joe (Microsoft MVP - XML)
 
Old November 28th, 2007, 06:27 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Your regular expression will only check that the input contains a single number, not that is consists only of numbers.

Try "^[0-9]+$" instead.

Obviously you should be doing the same check on the server as javascript may be turned off or bypassed on the client.

This is exactly what the Validation controls are for, as they can be confirmed on the server side as well (by checking Page.IsValid) while still providing client side feedback.

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using string parameter as an XPath whelanm XSLT 3 August 15th, 2008 09:55 AM
String as Parameter ndr1977 .NET Web Services 2 June 10th, 2008 03:23 AM
convert to string prafullaborade XML 7 May 29th, 2008 02:59 PM
can we do string manipulation on a parameter? Feng BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 July 31st, 2004 11:20 AM
convert string to integer kcheung Classic ASP Databases 1 July 28th, 2003 04:39 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.