学校のプロジェクト用のアプリケーションを開発していますが、SQL データベースへのデータの追加に問題があります。計画では、ユーザーはボタンをクリックしてデータを「追加」します。これにより、データを入力するためのさまざまなテキストボックスを備えた新しいフォームが開きます。ユーザーが満足したら、[詳細の追加] をクリックすると、これらが sql データベースに追加されます。問題は、try catch がエラーを返し続けることです。私はその理由について途方に暮れています。新鮮な目を持つ人にとっては簡単な修正かもしれません。コードは次のとおりです。
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 CarsULike
{
public partial class AddCust : Form
{
public AddCust()
{
InitializeComponent();
}
private void AddCust_Load(object sender, EventArgs e)
{
}
private void AddCustBtn_Click(object sender, EventArgs e)
{
if (FirstNameTxtBox.Text == "")
{
MessageBox.Show("Please enter a First Name");
}
else if (SurnameTxtBox.Text == "")
{
MessageBox.Show("Please enter a Surname");
}
else if (TitleTxtBox.Text == "")
{
MessageBox.Show("Please enter a Title");
}
else if (Address1TxtBox.Text == "")
{
MessageBox.Show("Please enter an address");
}
else if (TownCityTxtBox.Text == "")
{
MessageBox.Show("Please enter a Town/City");
}
else if (PostCodeTxtBox.Text == "")
{
MessageBox.Show("Please enter a Postcode");
}
else
{
//stores the values entered as variables
string firstName = FirstNameTxtBox.Text;
string surname = SurnameTxtBox.Text;
string title = TitleTxtBox.Text;
string address1 = Address1TxtBox.Text;
string address2 = Address2TxtBox.Text;
string townCity = TownCityTxtBox.Text;
string postCode = PostCodeTxtBox.Text;
string mobPhone = MobPhoneTxtBox.Text;
string homePhone = HomePhoneTxtBox.Text;
}
//establishes a connection with the database
SqlConnection connection = new SqlConnection(@"Data Source=Lee-PC\SQLEXPRESS; Initial Catalog=CarsULike_P116274;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
//SqlDataReader datareader;
string sql;
try
{
// this query is for insertion into the Customer table
sql = "INSERT INTO Customer (FirstName, Surname, Title, AddressLine1, AddressLine2, TownCity, PostCode, EmailAddress, MobilePhoneNo, HomePhoneNo)";
sql += String.Format("VALUES, @FirstName, @Surname, @Title, @AddressLine1, @AddressLine2, @TownCity, @PostCode, @EmailAddress, @MobilePhoneNo, @HomePhoneNo)");
cmd = new SqlCommand(sql, connection);
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@FirstName", FirstNameTxtBox.Text);
cmd.Parameters.AddWithValue("@Surname", SurnameTxtBox.Text);
cmd.Parameters.AddWithValue("@Title", TitleTxtBox.Text);
cmd.Parameters.AddWithValue("@AddressLine1", Address1TxtBox.Text);
cmd.Parameters.AddWithValue("@AddressLine2", Address2TxtBox.Text);
cmd.Parameters.AddWithValue("@TownCity", TownCityTxtBox.Text);
cmd.Parameters.AddWithValue("@PostCode", PostCodeTxtBox.Text);
cmd.Parameters.AddWithValue("@EmailAddress", MobPhoneTxtBox.Text);
cmd.Parameters.AddWithValue("@MobilePhoneNo", HomePhoneTxtBox.Text);
cmd.Parameters.AddWithValue("@HomePhoneNo", HomePhoneTxtBox.Text);
connection.Open(); //opens connection
cmd.ExecuteNonQuery(); //writes to the database
MessageBox.Show("Details Added", "Successful");
}
catch (SqlException ex)
{
throw new Exception("Error adding details", ex);
}
finally
{
connection.Close(); // Close connection
}
}
private void CancelAddCustBtn_Click(object sender, EventArgs e)
{
this.Close(); // Close current form
}
}
私は周りを見回しましたが、探しているものが見つからないようです..または、探しているものがわかりません!