using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
namespace SolutionCW
{
/// <summary>
/// Interaction logic for addCustomer.xaml
/// </summary>
public partial class addCustomer : Window
{
public addCustomer()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e) // add customer button
{
string inputName = nameinput.Text;
string inputLastname = secondnameinput.Text;
string Number = phonenumber.Text;
string address = addressinput.Text;
string Pcode = postcode.Text;
string cmdstring = "INSERT INTO Customer (FirstName, LastName, PhoneNumber, Address, Postcode) VALUES (@Name, @LastName, @Phone, @Addinput, @Pcode)";
string connString = MainWindow.DBconnection;
using (SqlConnection conn = new SqlConnection(connString))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
comm.CommandText = cmdstring;
comm.Parameters.AddWithValue("@Name", nameinput.Text);
comm.Parameters.AddWithValue("@LastName", secondnameinput.Text);
comm.Parameters.AddWithValue("@Phone", phonenumber.Text);
comm.Parameters.AddWithValue("@Addinput", addressinput.Text);
comm.Parameters.AddWithValue("@Pcode", postcode.Text);
try
{
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
Customer basic = new Customer();
basic.updateTable();
}
catch(SqlException)
{
}
this.Close();
}
}
}
}
我已经显示了用于将表单中的客户记录添加到我的数据库的代码,但是当我按下添加按钮时,它在打开数据库时不会显示新输入的数据。当我退出数据库/刷新并返回时,它只显示新输入的记录?感谢任何帮助,谢谢
答案 0 :(得分:0)
我不确定您是如何显示数据的。但是,问题是您的按钮事件执行,然后将数据发送到数据库,但显示您的数据的控件尚未重新呈现。根据控件的类型,刷新命令会有所不同。
// Form:
var form = new ApplicationForm();
form.Refresh();
// Update Panel:
updatePanelSample.Update();
如果没有关于您如何显示数据的更多信息,我们将无法帮助您更新或重绘已呈现的应用程序以显示更新。