Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making sure a field is valid

Status
Not open for further replies.

arbo80

Programmer
Jan 5, 2006
53
US
Hi all,

I have designed a web application (ASP.Net/ C#). I have a textbox field called "ID" and the goal is to make sure that the users entered a valid ID (stored in the database--SQL2000). How can I make sure that a user gets an error message if the ID entered does not match anything in the database?

Thanks,
A.
 
use a stored procedure to check the ID against the database, and return the ID (if found) or a value that will NEVER be used as an ID to indicate failure. Then base your "error" handling on discovery of this specific value.

Hope this helps,

Alex

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
why not pre-populate a dropdown list with user friend text and the id as the value of the list option. then the user must select a value from the drop and all you need is a required field validator to ensure something was selected.

since the values came from the database, you know they will be valid. sure there are edge cases where a record could be deleted between the time the list was created and a value selected, but this should be handled by the domain, not the GUI.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I have tried using a stored procedure but it still doesn't work. I'm getting an error message. To be honnest, I'm not really good in SQL or C#.

Here is my stored procedure:
---------------------------
CREATE PROCEDURE isValidID @PARENT_ID varchar(6)
AS

Begin

return (select count(*) from CUSTOMER_MASTER where CUST_NUM = @PARENT_ID )

END
GO

Here is my function:
--------------------
private void ValidParent()
{
SqlConnection cn = new SqlConnection(SqlCnStr);
SqlDataReader isParent = null;
string reccount = null;
string PARENT_NUM=Request.QueryString["PARENT_NUM"];
try
{
SqlCommand cmd = new SqlCommand("isValidID", cn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter myParm = cmd.Parameters.Add("@PARENT_ID", SqlDbType.VarChar, 6);
myParm.Value=PARENT_NUM;
cmd.Connection=cn;
cn.Open();
isParent = cmd.ExecuteReader();

while (isParent.Read())
{
reccount= isParent[0].ToString();
if ((reccount =="0"))
{
//Error message here
}
}
}
catch (Exception e)
{
Response.Write (e.ToString());
}
finally
{
if (isParent != null)
isParent.Close();
if (cn.State== ConnectionState.Open)
cn.Close();
}

}

I have the feeling that I'm missing a lot of things.
 
Wait a minute, I thought you meant a User ID (such as for a login). If its' just a record ID, then you have the opportunity to make 99% sure the user cannot possibly enter an invalid one.

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Code:
create proc IsValidId
 @id varchar(6)
as
 select count(1) from [table] where [column] = @id
Code:
public bool IsValid(int id)
{
   using(IDbConnection connection = new SqlConnection())
   {
       connection.Open();
       IDbCommand command = connection.CreateCommand();
       command.CommandText = "IsValidId";
       command.CommandType = CommandType.StoredProcedure;
       IDbParameter parameter = command.CreateParameter("id");
       parameter.Value = id;
       command.Parameters.Add(parameter);
       return (int)command.ExecuteScalar() > 0;
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks guys for your replies!

Jason - I tried your code but it didn't work
 
most likely a syntax error. simple enough to fix using <f1> and msdn.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks all!!
I finally got it. I modified my stored procedure.

A.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top