2011-06-22 25 views
0

我是一個用c#和窗口形式的新手如何驗證窗口窗體的數據庫C#的用戶輸入?

我正在做一個web服務,並使用一個包含datagridview的表單,但我有很多問題。

如何驗證數據庫的用戶輸入,例如,當用戶插入用戶名,但用戶名已存在於數據庫中時,我應該提示用戶輸入另一個用戶名。

我在以前的線程嘗試過,我驗證,只有用戶特定的列可以輸入0或1 ....

how to validate particular column cell field when editing 它是類似的東西?

我需要一個網絡方法嗎?

ps基本上我想要的是在窗體中有一個文本框字段,其中用戶鍵入一個用戶名將其添加到數據庫中,但是,當他們點擊添加按鈕時,如果用戶名已經存在,會有提示。如果沒有,它將使用插入網絡方法將其添加到數據庫。

數據是通過網絡方法檢索...這意味着我必須使用Web方法來幫助我檢查這個檢查...但是如何?

我對Web方法

[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 [email protected]"; 
    // 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; 

} 

代碼,這是我的窗口形式的代碼

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

    } 




} 
+0

你需要什麼?究竟 –

+0

@Akram ....如何使用web方法來幫助我驗證在窗體窗體中的文本框...我不知道如何在按鈕中調用...導致...用戶輸入用戶名.. 。然後點擊添加按鈕...該按鈕應該消耗檢查數據庫的網絡方法是否有類似的數據 – cutexxxbabies

回答

0

從這裏開始:

[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; 

} 

在您的應用形式:

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

@Akram然後如何調用這個web方法在按鈕來驗證輸入窗口中的文本框 – cutexxxbabies

+0

@Akram .... int matchesCount = int.Parse(dbCommand .ExecuteScalar());是紅色的下劃線 – cutexxxbabies

+0

@cutexxxbabies:'int matchesCount = int.Parse(dbCommand.ExecuteScalar()。ToString());'是正確的語句 –

1
+0

@Akram我不使用ASP ...我使用窗體窗體...感謝您的幫助...還有其他鏈接? – cutexxxbabies

+0

@cutexxxbabies:你知道如何使用數據庫?你在這裏有什麼問題? –

+0

@cutexxxbabies從你的問題:'我正在做一個網絡服務...' –

0

如果您使用某種webmethod插入數據,那麼必須有一些方法來根據某些值提取數據。現在將UserName傳遞給此方法並搜索它,如果找到,則請求用戶提供不同的UserName。

如果這不是你正在尋找的,那麼請解釋你的問題多一點。

+0

你的意思是我必須創建一個Web方法只是爲了驗證...然後如何調用窗口窗體...我顯示我的代碼爲web方法 – cutexxxbabies