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();
}
}