-1
Iこのシナリオは、名前、指定、参加年がデータベースに保存されます。保存ボタンで更新したい私はいくつかの問題に直面しています。リストビューの1つのボタンで更新して保存し、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 Student_Information
{
public partial class TeacherInformation : Form
{
SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-VTPQ9MQ\SQLEXPRESS;Initial Catalog=StudentDB;Integrated Security=True");
int Id = 0;
SqlCommand cmd;
SqlDataAdapter data;
DataTable dt = new DataTable();
public TeacherInformation()
{
InitializeComponent();
}
private void newButton_Click(object sender, EventArgs e)
{
EnableDisable();
}
private void saveButton_Click(object sender, EventArgs e)
{
try
{
if (saveButton.Text == "Save")
{
cmd = new SqlCommand("AddTeacher", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] param = new SqlParameter[3];
param[0] = new SqlParameter("@TeacherName", SqlDbType.VarChar);
param[0].Value = nameTextBox.Text.Trim();
param[1] = new SqlParameter("@Designation", SqlDbType.VarChar);
param[1].Value = designationComboBox.SelectedItem.ToString();
param[2] = new SqlParameter("@JoiningDate", SqlDbType.Date);
param[2].Value = joiningDateTimePicker.Text;
cmd.Parameters.AddRange(param);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("Insert Done", "Insert Data", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (saveButton.Text =="Update")
{
cmd = new SqlCommand("UpdateTeacher", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] param = new SqlParameter[4];
param[0] = new SqlParameter("@Id", SqlDbType.Int);
param[0].Value = Convert.ToInt32(Id);
param[1] = new SqlParameter("@TeacherName", SqlDbType.VarChar);
param[1].Value = nameTextBox.Text.Trim();
param[2] = new SqlParameter("@Designation", SqlDbType.VarChar);
param[2].Value = designationComboBox.SelectedItem.ToString();
param[3] = new SqlParameter("@JoiningDate", SqlDbType.Date);
param[3].Value = joiningDateTimePicker.Text;
cmd.Parameters.AddRange(param);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("Update Done", "Update Data", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("something is Wrong");
}
Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void deleteButton_Click_1(object sender, EventArgs e)
{
try
{
cmd = new SqlCommand("DeleteTeacher", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] param = new SqlParameter[1];
param[0] = new SqlParameter("@Id", SqlDbType.Int);
param[0].Value = Convert.ToInt32(Id);
cmd.Parameters.AddRange(param);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("Done", "Delete Data", MessageBoxButtons.OK, MessageBoxIcon.Information);
Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void EnableDisable()
{
saveButton.Enabled = true;
deleteButton.Enabled = true;
nameTextBox.Enabled = true;
designationComboBox.Enabled= true;
joiningDateTimePicker.Enabled = true;
}
public void Reset()
{
saveButton.Enabled = false;
deleteButton.Enabled = false;
nameTextBox.Enabled = false;
designationComboBox.Enabled = false;
joiningDateTimePicker.Enabled = false;
newButton.Enabled = true;
}
private void Clear()
{
nameTextBox.Text = "";
designationComboBox.SelectedIndex = -1;
joiningDateTimePicker.Text = "";
}
private void TeacherInformation_Load(object sender, EventArgs e)
{
SearchDataGridView();
Clear();
saveButton.Enabled = false;
deleteButton.Enabled = false;
newButton.Enabled = true;
nameTextBox.Enabled = false;
designationComboBox.Enabled = false;
joiningDateTimePicker.Enabled = false;
}
public void SearchDataGridView()
{
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlDataAdapter data = new SqlDataAdapter("SearchTeacherByName", conn);
data.SelectCommand.CommandType = CommandType.StoredProcedure;
data.SelectCommand.Parameters.AddWithValue("@TeacherName", searchTextBox.Text.Trim());
DataTable dt = new DataTable();
data.Fill(dt);
teacherListView.View = View.Details;
teacherListView.FullRowSelect = true;
foreach (DataRow row in dt.Rows)
{
ListViewItem listitem = new ListViewItem(row["Id"].ToString());
listitem.SubItems.Add(row["TeacherName"].ToString());
listitem.SubItems.Add(row["Designation"].ToString());
listitem.SubItems.Add(row["JoiningDate"].ToString());
teacherListView.Items.Add(listitem);
}
conn.Close();
}
private void searchButton_Click(object sender, EventArgs e)
{
try
{
SearchDataGridView();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void teacherListView_DoubleClick(object sender, EventArgs e)
{
{
if (teacherListView.SelectedItems.Count > -1)
// return;
Id = Convert.ToInt32(teacherListView.SelectedItems[0].SubItems[0].ToString());
nameTextBox.Text =teacherListView.SelectedItems[0].SubItems[1].Text;
designationComboBox.Text =teacherListView.SelectedItems[0].SubItems[2].Text;
joiningDateTimePicker.Value =Convert.ToDateTime(teacherListView.SelectedItems[0].SubItems[3].Text.ToString());
saveButton.Text = "Update";
deleteButton.Enabled = true;
}
}
private void resetButton_Click(object sender, EventArgs e)
{
Reset();
}
}
}
質問タイトルの前に「解決済み」と書かれた理由が分かります。 –
あなたの質問は最高ではっきりしていません。それが解決した場合は、質問を削除するか、回答を投稿してください。それ以外の場合は、同様の問題を解決する方法を探している他の人には役に立ちません。 –
私はコメントの下に解決策を提供します。 TIa – nazrul