私はVSとC#の新機能です。私が扱っている製品のバックエンドの理解を深めるために教えています。私はいくつかの情報とログインフォームを持つ小さなデータベースを作成しました。すべてが正しくコンパイルされているように見えますが、クリックするとログインボタンが反応せず、キャンセルボタンも表示されません。私が紛失しているかどうかは分かりませんが、コードは次のとおりです。C#ログインフォーム、ログインボタンが応答しない
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace CorpLogin
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//DB Connection String
string cs = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\CORPORATION.mdf;" +
"Integrated Security=True";
//Login Button clicked
private void LoginButton1_Click(object sender, EventArgs e)
{
//Validates text entered
if (userNameText1.Text == "")
{
MessageBox.Show("USERNAME and PASSWORD are required fields.");
return;
}
if (passwordText1.Text == "")
{
MessageBox.Show("USERNAME and PASSWORD are required fields.");
return;
}
try
{
//Connect to SQL
SqlConnection con = new SqlConnection(cs);
con.Open();
SqlCommand cmd = new SqlCommand("select * from USERS where [email protected]" +
"and [email protected]", con);
cmd.Parameters.AddWithValue("@username", userNameText1.Text);
cmd.Parameters.AddWithValue("@password", passwordText1.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
int count = ds.Tables[0].Rows.Count;
//Show new form or fail message
if (count == 1)
{
this.Hide();
CorpView cv = new CorpView();
cv.Show();
}
else
{
MessageBox.Show("ACCESS DENIED");
}
}
//Catch program exceptions
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//Cancel Button Clicked
private void CancelButton1_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
何か助けていただければ幸いです。ありがとう。
Winformsを使用していますか?また、どのバージョンのVS? – Hank
偶然にあなたのボタンの名前を変更しましたか?イベントハンドラは大丈夫です。 – Fang
フォームデザイナーのボタンをクリックして、この「LoginButton1_Click」のイベントを見て、ボタンのドロップダウンをクリックしてください。そのメソッドを選択してください。そうでない場合は、ボタンクリックイベントをダブルクリックして、 – MethodMan