[英]Can you help me to show datagridview and Refresh after update?
This is my code and error message when you running say: 这是我的代码和错误消息,当您运行时说:
An unhandled exception of type System.Data.SqlClient.SqlException occurred in System.Data.dll
System.Data.SqlClient.SqlException类型的未处理的异常发生在System.Data.dll中
on this da.fill(dt);
在这个
da.fill(dt);
SqlConnection con = new SqlConnection("Data Source=ANTONIANGGA-PC\\SQLEXPRESS;Initial Catalog=FullandStarving;Integrated Security=True");
SqlCommand cmd;
SqlDataAdapter da;
DataTable dt = new DataTable();
public FormProduksi()
{
InitializeComponent();
showgridview();
}
private void showgridview()
{
con.Open();
dt.Clear();
cmd = new SqlCommand("SELECT * FROM Produksi", con);
//cmd.CommandType = CommandType.StoredProcedure; done :D
da = new SqlDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
private void button2_Click(object sender, EventArgs e)
{
//Datetimepicker to Database
string dProduksi = DateTime.Parse(dtmProduksi.Text).ToString("yyyy-MM-dd");
try{
con.Open();
cmd = new SqlCommand("insert into Produksi (IDProduksi,IDPhoto,TanggalProduksi,NamaKaryawan,KeteranganPhoto) Values('" + txtIdpro.Text + "','" + txtIdPhoto.Text + "','" + dProduksi + "','" + txtNamaKaryawan.Text + "','" + rxtKtrphoto.Text + "')", con);
cmd.ExecuteNonQuery();
MessageBox.Show("Update telah di jalankan");
showgridview();
clear();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
that update successfully but cant refresh, so i do quit that form and open can see it 该更新成功,但无法刷新,所以我退出了该表单并打开可以看到它
You are closing the connection 您正在关闭连接
con.Close();
and then using 然后使用
da.Fill(dt);
Just swap this lines: 只需交换以下行:
showgridview();
con.Close();
For example with DbDataAdapter.Fill
: 例如,使用
DbDataAdapter.Fill
:
Notes: 笔记:
Yoy should use parametrized queries so you avoid SQL Injection attacks : Yoy应该使用参数化查询,以便避免SQL注入攻击 :
var cmd = new SqlCommand("SELECT EmpName FROM Employee WHERE EmpID = @id", con);
cmd.Parameters.AddWithValue("@id", id.Text);
Wrap SqlConnection
and SqlCommand
into using
so any resources used by those would disposed : 将
SqlConnection
和SqlCommand
包装为using
以便那些using
任何资源都将被处置 :
string position;
using (SqlConnection con = new SqlConnection("server=free-pc\\FATMAH; Integrated Security=True; database=Workflow; "))
{
con.Open();
using (var cmd = new SqlCommand("SELECT EmpName FROM Employee WHERE EmpID = @id", con))
{
cmd.Parameters.AddWithValue("@id", id.Text);
var name = cmd.ExecuteScalar();
if (name != null)
{
position = name.ToString();
Response.Write("User Registration successful");
}
else
{
Console.WriteLine("No Employee found.");
}
}
}
Just change the showgridview()
function as below where connection is opened & closed properly. 只需如下更改
showgridview()
函数,即可正确打开和关闭连接。
Also check your sql query , provide space and maintain syntax of query : 还要检查您的sql查询, 提供空间并维护查询的语法 :
SELECT * FROM Produksi
Error screenshot clearly depicts that stored procedure with such name don't exist 错误屏幕截图清楚地说明了具有该名称的存储过程不存在
comment out those lines as code below : 将这些行注释为以下代码 :
void showgridview()
{
con.Open();
dt.Clear();
cmd = new SqlCommand("SELECT * FROM Produksi", con);
//cmd.CommandType = CommandType.StoredProcedure;
da = new SqlDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
Then you wont be having connection issues and errors related . 这样您就不会遇到连接问题和相关的错误。
Button Click code change the closing connection as below: 按钮单击代码可如下更改闭合连接 :
private void button2_Click(object sender, EventArgs e)
{
//Datetimepicker to Database
string dProduksi = DateTime.Parse(dtmProduksi.Text).ToString("yyyy-MM-dd");
try
{
con.Open();
cmd = new SqlCommand("insert into Produksi (IDProduksi,IDPhoto,TanggalProduksi,NamaKaryawan,KeteranganPhoto) Values('" + txtIdpro.Text + "','" + txtIdPhoto.Text + "','" + dProduksi + "','" + txtNamaKaryawan.Text + "','" + rxtKtrphoto.Text + "')", con);
cmd.ExecuteNonQuery();
MessageBox.Show("Update telah di jalankan");
con.Close();
showgridview();
clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Also, for further reading: 另外,为进一步阅读:
parameterized queries vs. SQL injection 参数化查询与SQL注入
Why do we always prefer using parameters in SQL statements? 为什么我们总是喜欢在SQL语句中使用参数?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.