0

I am a newbie with c# and window form

I am doing a web service, and use a form which contain a datagridview, but am having quite a lot of problems.

How do I validate user input with a database, for example, when the user inserts a username, however the username already exists in the database, I should prompt the user to enter another username.

I tried before in previous thread that I validate that particular column with only user can enter 0 or 1....

how to validate particular column cell field when editing Is it something similar?

Do I need a web method for this?

p.s basically what I want is to have a textbox field in a window form, in which the user will key in a username to add it to the database, however, when they click on the add button, if the user name already exists, there will be a prompt. If not it will add it to database using the insert web method.

The data is retrieve by web method... meaning that I have to use a web method to help me with this check right...but how?

my code for web method

[WebMethod]  
public DataSet validateUserName()  
{   

    SqlConnection conn = 
        new SqlConnection("Data Source=.\\SQLEXPRESS;
            Initial Catalog=User;Integrated Security=True");   
    SqlCommand dbCommand = new SqlCommand();  
    dbCommand.CommandText = 
        @"SELECT COUNT(*) 
        FROM User 
        WHERE UserName=@UserName"; 
    // this textusername is from the window form  
    dbCommand.Connection = conn;  
    SqlDataAdapter da;   
    da = new SqlDataAdapter();   
    da.SelectCommand = dbCommand;

    DataSet ds = new DataSet();  
    da.Fill(ds);   
    return ds;

}

this is my window form code

private void btnAdd_Click(object sender, EventArgs e)

{
    WSBrandData validate = new WSBRandData();
    if (validate.validateUserName(txtUserName.Text))
    {
        MessageBox.Show("UserName is allocated");
        txtUserName.Text = "";
    }
    else
    {
       WSBrandData add = new WSBRandData();

        String[] names = null;

        names = Convert.ToString(DGVBrand.CurrentRow.Cells[1].Value).Split(';');
        String tagg = txtUserName.Text + ";";
        names[1] = tagg;

        DGVBrand.DataSource = add.addUserName(Convert.ToInt32(DGVBrand.CurrentRow.Cells[0].Value), names[0], names[1], Convert.ToInt32(DGVBrand.CurrentRow.Cells[3].Value));
        BindBrand();

    }




}
1
  • @Akram .... how to use web method to help me to validate the textbox which is in the window form...i not sure how can i call in the button...cause...user input username...then click on add button...the button should consume the web method which check with the database whether there is similar data Commented Jun 22, 2011 at 6:51

3 Answers 3

1

Read about ASP.NET Web Services.

Sign up to request clarification or add additional context in comments.

6 Comments

@Akram i dont use asp...i use window form...thanks for ur help...still got other link?
@cutexxxbabies: Do you know how to work with database ?? What is you question here ?
@cutexxxbabies from your question: 'I am doing a web service...'
@Akram... i am using window form ...not asp,....then i want to validate a textbox in my window form against the database...
@cutexxxbabies: First of all, why are you using webservices here? If you realy need to use webservices then you should read the tutorial I have provided in my answer ..
|
0

Start here :

[WebMethod]  
public Boolean UserNameExists(String userName)  
{   

    SqlConnection conn = 
        new SqlConnection("Data Source=.\\SQLEXPRESS;
            Initial Catalog=User;Integrated Security=True");   
    SqlCommand dbCommand = new SqlCommand();  
    dbCommand.CommandText = 
        @"SELECT COUNT(*) 
        FROM User 
        WHERE UserName='" + userName + "'";
    // this textusername is from the window form  
    dbCommand.Connection = conn;  

    conn.Open();

    int matchesCount = int.Parse(dbCommand.ExecuteScalar().ToString());

    conn.Close();  

    return matchesCount != 0;

}

In your forms application:

if (webReference.Type.UserNameExists(this.userNameTextBox.Text) )
{
    // Do something
}
else
{
    // Do something else
}

16 Comments

@Akram then how can call this web method in the button to validate input in the textbox in window form
@Akram ....int matchesCount = int.Parse(dbCommand.ExecuteScalar()); is underline in red
@cutexxxbabies: int matchesCount = int.Parse(dbCommand.ExecuteScalar().ToString()); is the right statement
@Akram ...then in my window...how should i call it...i know the proxy thing...but...can roughly show me how...cause...i havent done before checking of input with database
@Akram so i not need to use the proxy...like eg.... wsusername validate = new wsusername(); validate.UserNameExists(username.Text);
|
0

If you are using some webmethod to insert the data, then there must be some method to extract the data, based upon some value. Now pass the UserName to this method and search for it, if found then ask the user to provide a different UserName.

And if this not what you are looking for, then please explain your question a bit more.

1 Comment

do u mean i must create a web method just for validate...then how to call in the window form...i show my code for the web method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.