私は初心者の C# プログラマーです。C#でゲームを作ってみました。私の登録ページでエラーが発生しました。ExecuteNonQuery: Connection property has not been initializedat at this line "int result = command.ExecuteNonQuery();".Here は私の C# コードです。この問題を解決するのを手伝ってください。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WiimoteTest
{
public partial class regispage : Form
{
private SqlConnection connection;
private string sql;
private SqlCommand command = new SqlCommand();
public regispage()
{
InitializeComponent();
}
private void back_Click(object sender, EventArgs e)
{
startpage back = new startpage();
back.Show();
this.Close();
}
private void regispage_Load(object sender, EventArgs e)
{
string conStr = @"Data Source=.\SQLEXPRESS;
AttachDbFilename=C:\Users\vaio\Downloads\Compressed\WiimotLib_1.7\WiimotLib_1.7\samples\WiimoteTestCS_5_AllpagePuppyGameTestBirdResizepicTestDB\Angee.mdf;
Intregrated Security=True;User Instance=True;";
connection = new SqlConnection(conStr);
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
}
private bool IsCompleteData()
{
string errMsg = "";
if (username.Text == "")
{
errMsg = "Please input username";
}
else if (password.Text == "")
{
errMsg = "Please input password";
}
else if (fname.Text == "")
{
errMsg = "Please input firstname";
}
else if (lname.Text == "")
{
errMsg = "Please input lastname";
}
else if (nickname.Text == "")
{
errMsg = "Please input nickname";
}
else if (gender.SelectedIndex == -1)
{
errMsg = "Please select gender";
}
else if (age.Text == "")
{
errMsg = "Please input age";
}
else if (symptom.Text == "")
{
errMsg = "Please input symptom";
}
if (errMsg == "")
{
return true;
}
else
{
MessageBox.Show(errMsg);
return false;
}
}
private void AddParamterValues()
{
command.Parameters.AddWithValue("username", username.Text);
command.Parameters.AddWithValue("password", password.Text);
command.Parameters.AddWithValue("fname", fname.Text);
command.Parameters.AddWithValue("lname", lname.Text);
command.Parameters.AddWithValue("nickname", nickname.Text);
command.Parameters.AddWithValue("gender", gender.SelectedItem.ToString());
command.Parameters.AddWithValue("age", age.Text);
command.Parameters.AddWithValue("symptom", symptom.Text);
}
private void submit_Click(object sender, EventArgs e)
{
if (!IsCompleteData())
{
return;
}
InsertData();
}
private void InsertData()
{
using (var connection = new SqlConnection())
using (var command = connection.CreateCommand())
{
sql = @"INSERT INTO User(username, password, fname, lname, nickname, gender, age, symptom)
VALUES(@username, @password, @fname, @lname, @nickname, @gender, @age, @symptom),this.connection";
**connection.Open();**
var affectedRows = command.ExecuteNonQuery();
command.Parameters.Clear();
command.CommandText = sql;
AddParamterValues();
if (affectedRows < 1)
{
MessageBox.Show("Error to insert data");
return;
}
else
{
MessageBox.Show("Insert data complete");
menupage submit = new menupage();
submit.Show();
this.Close();
}
}
}
private void regispage_FormClosing(object sender, FormClosingEventArgs e)
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
}
}
}