Click here to Skip to main content
15,906,335 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a code like this:
C#
private void checkError()
{
    if (txtName.Text == string.Empty)
    {
        WebMsgBox.Show("Please Enter User Name");
        txtName.Focus();
        return;
    }
    if (txtUserId.Text.Trim() == string.Empty)
    {
        WebMsgBox.Show("Please Enter UserId");
        txtUserId.Focus();
        return;
    }
    if (txtPasword.Text.Trim() == string.Empty)
    {
        WebMsgBox.Show("Please Enter Password");
        txtPasword.Focus();
        return;
    }
}


This function i have declare in buttion_click event before the save code.
protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            checkError();
            SqlConnection con = database.GetConnection();
            SqlCommand com = new SqlCommand("spRegistration", con);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.AddWithValue("@Name", txtName.Text);
            com.Parameters.AddWithValue("@UserId", txtUserId.Text);
            com.Parameters.AddWithValue("@Password", txtPasword.Text);
            com.Parameters.AddWithValue("@Date", txtDate.Text);
            com.ExecuteNonQuery();
            WebMsgBox.Show("Accounts Created Successfully");
            clearFields();

        }
        catch (Exception ex)
        {
            WebMsgBox.Show("Error Message:" + ex.Message);
        }
      
    }


Then if we click save button without any value in textbox, the null value will be insert into the database. How can i solve it.
Please help me.

Thanks in advance
Posted
Updated 19-May-11 2:45am
v2
Comments
[no name] 19-May-11 8:46am    
Edited for Code Block for easy understandability

 
Share this answer
 
Comments
rahul dev123 19-May-11 8:56am    
I use RequireFieldValidator but i also want to check in code behind
Insted of void use return a boolean that is true or false so here is how it goes


C#
private bool checkError()
{
    if (txtName.Text == string.Empty)
    {
        WebMsgBox.Show("Please Enter User Name");
        txtName.Focus();
        return false;
    }
    if (txtUserId.Text.Trim() == string.Empty)
    {
        WebMsgBox.Show("Please Enter UserId");
        txtUserId.Focus();
        return false;
    }
    if (txtPasword.Text.Trim() == string.Empty)
    {
        WebMsgBox.Show("Please Enter Password");
        txtPasword.Focus();
        return false;
    }
    else return true;

}



C#
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
if(checkError())
{
  SqlConnection con = database.GetConnection();
  SqlCommand com = new SqlCommand("spRegistration", con);
  com.CommandType = CommandType.StoredProcedure;
  com.Parameters.AddWithValue("@Name", txtName.Text);
  com.Parameters.AddWithValue("@UserId", txtUserId.Text);
  com.Parameters.AddWithValue("@Password", txtPasword.Text);
  com.Parameters.AddWithValue("@Date", txtDate.Text);
  com.ExecuteNonQuery();
  WebMsgBox.Show("Accounts Created Successfully");
  clearFields();
 }

}
catch (Exception ex)
{
WebMsgBox.Show("Error Message:" + ex.Message);
}

}
 
Share this answer
 
Comments
rahul dev123 19-May-11 9:03am    
Thanks For Your solution
Steven.Pinto2000 20-May-11 0:16am    
Welcome
Change your error routine to return a bool:
private bool checkError()
{
    if (txtName.Text == string.Empty)
    {
        WebMsgBox.Show("Please Enter User Name");
        txtName.Focus();
        return true;
    }
    if (txtUserId.Text.Trim() == string.Empty)
    {
        WebMsgBox.Show("Please Enter UserId");
        txtUserId.Focus();
        return true;
    }
    if (txtPasword.Text.Trim() == string.Empty)
    {
        WebMsgBox.Show("Please Enter Password");
        txtPasword.Focus();
        return true;
    }
    return false;
}
Then just check the return code:
protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (!checkError())
            {
                SqlConnection con = database.GetConnection();
                ....
                clearFields();
            }
        }
        catch (Exception ex)
        {
            WebMsgBox.Show("Error Message:" + ex.Message);
        }
      
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900