简体   繁体   中英

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

on this 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 :

Notes:

1

Yoy should use parametrized queries so you avoid SQL Injection attacks :

var cmd = new SqlCommand("SELECT EmpName FROM Employee WHERE EmpID = @id", con);
cmd.Parameters.AddWithValue("@id", id.Text);

2

Wrap SqlConnection and SqlCommand into using so any resources used by those would disposed :

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.");
    }
  }
}

Credit

Just change the showgridview() function as below where connection is opened & closed properly.

Also check your sql query , provide space and maintain syntax of query :

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

Why do we always prefer using parameters in SQL statements?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM