如何解决C#Windows Form App CRUD未更新记录
我正在尝试使用C#创建一个CRUD应用程序,以跟踪项目,分配给他们的人员及其状态。我的进度很好,但是无法使用“更新”功能。每当我尝试更新记录时,都会收到我自己的错误消息,而不是“成功更新”。
有人知道是什么导致我的if语句无法正确执行吗?
using System;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
namespace SFTool
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
SqlConnection con = new SqlConnection("my connection string here");
public int ProjectID;
private void MainForm_Load(object sender,System.EventArgs e)
{
// load in sql data to the data grid view
GetProjectsDataset();
}
private void GetProjectsDataset()
{
SqlCommand cmd = new SqlCommand("SELECT * FROM sfProjects",con);
DataTable dt = new DataTable();
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
dt.Load(sdr);
con.Close();
dataGridView.DataSource = dt;
}
private void insertButton_Click(object sender,EventArgs e)
{
// allow user to insert data into the database,or create new records
if (IsValid())
{
SqlCommand cmd = new SqlCommand("INSERT INTO sfProjects VALUES (@Project_ID,@FirstName,@LastName,@Project_Status)",con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Project_ID",projectIDTextbox.Text); // maps @Project_ID to the project ID textbox
cmd.Parameters.AddWithValue("@FirstName",firstNameTextBox.Text);
cmd.Parameters.AddWithValue("@LastName",lastNameTextBox.Text);
cmd.Parameters.AddWithValue("@Project_Status",projectStatusTextbox.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("New record has been successfully added to the database","Saved",MessageBoxButtons.OK,MessageBoxIcon.Information);
// now update/REFRESH the database so it shows the new record
GetProjectsDataset();
// reset form controls
ResetFormControls();
}
}
// data validation. (Make sure project id is never empty)
private bool IsValid()
{
if (projectIDTextbox.Text == string.Empty)
{
MessageBox.Show("Project ID is required.","Error",MessageBoxIcon.Error);
return false;
}
// else return true (it is valid)
return true;
}
private void resetButton_Click(object sender,EventArgs e)
{
// resets,or clears,the textboxes
ResetFormControls();
}
private void ResetFormControls()
{
projectIDTextbox.Clear();
firstNameTextBox.Clear();
lastNameTextBox.Clear();
projectStatusTextbox.Clear();
projectIDTextbox.Focus();
}
private void dataGridView_CellClick(object sender,DataGridViewCellEventArgs e)
{
// WHen you click on a row in the data grid,the textboxes will automatically populate with that rows values
ProjectID = Convert.ToInt32(dataGridView.Rows[0].Cells[0].Value);
projectIDTextbox.Text = dataGridView.SelectedRows[0].Cells[0].Value.ToString();
firstNameTextBox.Text = dataGridView.SelectedRows[0].Cells[1].Value.ToString();
lastNameTextBox.Text = dataGridView.SelectedRows[0].Cells[2].Value.ToString();
projectStatusTextbox.Text = dataGridView.SelectedRows[0].Cells[3].Value.ToString();
}
private void updateButton_Click(object sender,EventArgs e)
{
if (ProjectID > 0)
{
SqlCommand cmd = new SqlCommand("UPDATE sfProjects SET (Project_ID = @Project_ID,FirstName = @FirstName,LastName = @LastName,Project_Status = @Project_Status WHERE ProjectID = @Project_ID)",projectStatusTextbox.Text);
cmd.Parameters.AddWithValue("@Project_ID",this.ProjectID);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record is successfully updated","Updated",MessageBoxIcon.Information);
// now update/REFRESH the database so it shows the new record
GetProjectsDataset();
// reset form controls
ResetFormControls();
}
else
{
MessageBox.Show("Please select a record to update",MessageBoxIcon.Error);
}
}
}
}
更新:我终于明白了
ProjectID = Convert.ToInt32(dataGridView.Rows[0].Cells[0].Value);
需要成为
ProjectID = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。