locked
Update function not working RRS feed

  • Question

  • User-290759768 posted

    This is my first assignment for C#, and I can't figure out what goes wrong with my C# coding.  My database is using Postgres and the statement had been run well.
    Can anyone help me with this?

    protected void btnUpdation_Click(object sender, EventArgs e)
    {
    try /* Updation After Validations*/
    {
    using (NpgsqlConnection connection = new NpgsqlConnection())
    {
    connection.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ToString();
    connection.Open();
    NpgsqlCommand cmd = new NpgsqlCommand();
    cmd.Connection = connection;
    cmd.CommandText = "UPDATE studentdb SET f_name=@f_name, l_name=@l_name, ic=@ic WHERE id=3";
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.Add(new NpgsqlParameter("@id", Convert.ToInt32(DropDownList2.SelectedItem.Value)));
    cmd.Parameters.Add(new NpgsqlParameter("@f_name", txtFname.Text));
    cmd.Parameters.Add(new NpgsqlParameter("@l_name", txtLname.Text));
    cmd.Parameters.Add(new NpgsqlParameter("@ic", txtIC.Text));
    cmd.Dispose();
    connection.Close();
    Bind();
    txtFname.Text = "";
    txtLname.Text = "";
    txtIC.Text = "";
    }
    }
    catch (Exception ex)
    {
    throw ex;
    }
    }// end of Updation

    Friday, September 6, 2019 7:18 AM

Answers

  • User288213138 posted

    Hi Happy Bee,

    when you update the data, you should  execute cmd.ExecuteNonQuery();

    i made demo for you in sql.

    The code:

    <asp:GridView ID="GridView1" runat="server"></asp:GridView><br />
               txtFname: <asp:TextBox ID="txtFname" runat="server"></asp:TextBox><br />
              txtLname : <asp:TextBox ID="txtLname" runat="server"></asp:TextBox><br />
               txtIC: <asp:TextBox ID="txtIC" runat="server"></asp:TextBox><br />
                <asp:Button ID="Button1" runat="server" Text="updata" OnClick="Button1_Click" />
    
    aspx.cs:
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    Bind();
                }
            }
            public string constr= ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            public void Bind()
            {
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand("SELECT * FROM studentdb"))
                    {
                        using (SqlDataAdapter sda = new SqlDataAdapter())
                        {
                            cmd.Connection = con;
                            sda.SelectCommand = cmd;
                            using (DataTable dt = new DataTable())
                            {
                                sda.Fill(dt);
                                GridView1.DataSource = dt;
                                GridView1.DataBind();
                            }
                        }
                    }
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                try /* Updation After Validations*/
                {
                    using (SqlConnection connection = new SqlConnection(constr))
                    {
                        connection.Open();
                        SqlCommand cmd = new SqlCommand();
                        cmd.Connection = connection;
                        cmd.CommandText = "UPDATE studentdb SET f_name=@f_name, l_name=@l_name, ic=@ic WHERE id=3";
                        cmd.CommandType = CommandType.Text;
                        //cmd.Parameters.Add(new SqlParameter("@id", Convert.ToInt32(DropDownList2.SelectedItem.Value)));
                        cmd.Parameters.Add(new SqlParameter("@f_name", txtFname.Text));
                        cmd.Parameters.Add(new SqlParameter("@l_name", txtLname.Text));
                        cmd.Parameters.Add(new SqlParameter("@ic", txtIC.Text));
                        cmd.ExecuteNonQuery();
                        cmd.Dispose();
                        connection.Close();
                        Bind();
                        txtFname.Text = "";
                        txtLname.Text = "";
                        txtIC.Text = "";
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 6, 2019 7:56 AM

All replies

  • User288213138 posted

    Hi Happy Bee,

    when you update the data, you should  execute cmd.ExecuteNonQuery();

    i made demo for you in sql.

    The code:

    <asp:GridView ID="GridView1" runat="server"></asp:GridView><br />
               txtFname: <asp:TextBox ID="txtFname" runat="server"></asp:TextBox><br />
              txtLname : <asp:TextBox ID="txtLname" runat="server"></asp:TextBox><br />
               txtIC: <asp:TextBox ID="txtIC" runat="server"></asp:TextBox><br />
                <asp:Button ID="Button1" runat="server" Text="updata" OnClick="Button1_Click" />
    
    aspx.cs:
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    Bind();
                }
            }
            public string constr= ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            public void Bind()
            {
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand("SELECT * FROM studentdb"))
                    {
                        using (SqlDataAdapter sda = new SqlDataAdapter())
                        {
                            cmd.Connection = con;
                            sda.SelectCommand = cmd;
                            using (DataTable dt = new DataTable())
                            {
                                sda.Fill(dt);
                                GridView1.DataSource = dt;
                                GridView1.DataBind();
                            }
                        }
                    }
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                try /* Updation After Validations*/
                {
                    using (SqlConnection connection = new SqlConnection(constr))
                    {
                        connection.Open();
                        SqlCommand cmd = new SqlCommand();
                        cmd.Connection = connection;
                        cmd.CommandText = "UPDATE studentdb SET f_name=@f_name, l_name=@l_name, ic=@ic WHERE id=3";
                        cmd.CommandType = CommandType.Text;
                        //cmd.Parameters.Add(new SqlParameter("@id", Convert.ToInt32(DropDownList2.SelectedItem.Value)));
                        cmd.Parameters.Add(new SqlParameter("@f_name", txtFname.Text));
                        cmd.Parameters.Add(new SqlParameter("@l_name", txtLname.Text));
                        cmd.Parameters.Add(new SqlParameter("@ic", txtIC.Text));
                        cmd.ExecuteNonQuery();
                        cmd.Dispose();
                        connection.Close();
                        Bind();
                        txtFname.Text = "";
                        txtLname.Text = "";
                        txtIC.Text = "";
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 6, 2019 7:56 AM
  • User-290759768 posted

    Thanks, you are God!  It works!

    Friday, September 6, 2019 8:35 AM