locked
How can I clear the data of text box and list box after insert the data of them to data base in asp.net RRS feed

  • Question

  • User-1092307275 posted

    I have a web form with some textbox and list box in it. in this web form, I get the data from users and insert them to the database. after insert, I want to clear the data of controls immediately. I use these ways:

    TxtDescription2.Text = "";

    ListSexuality.Text = "";

    ListSession.Text = "";

    ListWeekDay1.Text = "";

    or reload my web form by this code:

    Response.Redirect("Course.aspx?Value1=" + LblSubjectStudy.Text);

    but it doesn't work. what's the best way to do this action?!

    Monday, December 31, 2018 12:28 PM

Answers

  • User-893317190 posted

    Hi dorsa2,

    I have made a test for inserting, but it could clear the textbox successfully.

    Below is my code. I insert  into a table named employee, every employee has a department. Dropdownlist is used to bind data of department.

     <form id="form1" runat="server">
            <asp:Label ID="Label1" runat="server" Text="name"></asp:Label><asp:TextBox ID="TextBox1" runat="server">  </asp:TextBox><br />
    <asp:Label ID="Label2" runat="server" Text="salary"></asp:Label><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
            department:<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
            <br />
            <asp:Button ID="Button1" runat="server" Text="submit" OnClick="Button1_Click" />
        </form>

    Code behind.

      private static string constr = ConfigurationManager.ConnectionStrings["EntityDb"].ConnectionString;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                   
    
                        using (SqlDataAdapter adapter = new SqlDataAdapter("select * from department", constr))
                        {
    
                           
                            DataTable table = new DataTable();
                            adapter.Fill(table);
                            DropDownList1.DataTextField = "department_name";
                            DropDownList1.DataValueField = "id";
                            DropDownList1.DataSource = table;
                            DropDownList1.DataBind();
                        }
                    
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand com = new SqlCommand("insert into employee (name, salary, department_id) values(@name, @salary, @department_id)", con))
                    {
                        com.Parameters.AddWithValue("name", TextBox1.Text);
                        com.Parameters.AddWithValue("salary", TextBox2.Text);
                        com.Parameters.AddWithValue("department_id", DropDownList1.SelectedValue);
                        con.Open();
                        com.ExecuteNonQuery();
    // clear textbox TextBox1.Text = ""; TextBox2.Text = "";
    // reset the dropdownlist's selected index DropDownList1.SelectedIndex = 0; } } }

    The result.

    If it is not your case , could you post your code to let us reproduce your problem?

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 1, 2019 5:30 AM

All replies

  • User475983607 posted

    One option is disabling the controls ViewState.

    https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-3.0/75x4ha6s%28v%3dvs.85%29

    Setting the Text property should work.  I recommend using the Visual Studio debugger to verify your code.  If you need the community to review your code then post all the relevant bits.

    Monday, December 31, 2018 12:36 PM
  • User753101303 posted

    Hi,

    It should work. You are using the same form to edit existing rows ? For now it seems your form is coded so that after the insert you are on edit mode on the newly inserted row rather than showing a blank row again for another insert.

    Monday, December 31, 2018 12:42 PM
  • User-1092307275 posted

    Hi <g class="gr_ gr_45 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="45" data-gr-id="45">PatriceSc</g>,

    in this form, just I insert the data to my Table in DB. To insert my new information, I want to clear the textbox controls and list boxes of the form.

    Monday, December 31, 2018 12:49 PM
  • User-893317190 posted

    Hi dorsa2,

    I have made a test for inserting, but it could clear the textbox successfully.

    Below is my code. I insert  into a table named employee, every employee has a department. Dropdownlist is used to bind data of department.

     <form id="form1" runat="server">
            <asp:Label ID="Label1" runat="server" Text="name"></asp:Label><asp:TextBox ID="TextBox1" runat="server">  </asp:TextBox><br />
    <asp:Label ID="Label2" runat="server" Text="salary"></asp:Label><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
            department:<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
            <br />
            <asp:Button ID="Button1" runat="server" Text="submit" OnClick="Button1_Click" />
        </form>

    Code behind.

      private static string constr = ConfigurationManager.ConnectionStrings["EntityDb"].ConnectionString;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                   
    
                        using (SqlDataAdapter adapter = new SqlDataAdapter("select * from department", constr))
                        {
    
                           
                            DataTable table = new DataTable();
                            adapter.Fill(table);
                            DropDownList1.DataTextField = "department_name";
                            DropDownList1.DataValueField = "id";
                            DropDownList1.DataSource = table;
                            DropDownList1.DataBind();
                        }
                    
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand com = new SqlCommand("insert into employee (name, salary, department_id) values(@name, @salary, @department_id)", con))
                    {
                        com.Parameters.AddWithValue("name", TextBox1.Text);
                        com.Parameters.AddWithValue("salary", TextBox2.Text);
                        com.Parameters.AddWithValue("department_id", DropDownList1.SelectedValue);
                        con.Open();
                        com.ExecuteNonQuery();
    // clear textbox TextBox1.Text = ""; TextBox2.Text = "";
    // reset the dropdownlist's selected index DropDownList1.SelectedIndex = 0; } } }

    The result.

    If it is not your case , could you post your code to let us reproduce your problem?

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 1, 2019 5:30 AM
  • User-1092307275 posted

    Thx Dear Ackerly Xu,

    this is my code:

        ////insert the data to EDU_CourseSessionPlan
                    string Sqlstr_SPlan = " Insert Into EDU_CourseSessionPlan (" + " CourseId," + " RoomId," + " StartTime," + " EndTime," 
    + " WeekDay," + " Description " + " ) " + "VALUES ('" + Int32.Parse(LblCodeId.Text) + "','" + Int32.Parse(RoomId1) + "' ,'" + StartTime11 + "' ,'" + EndTime11 + "'
    , '" + ListWeekDay1.Text + "' , '" + TxtDescription.Text + "' )"; sqlcom.CommandText = Sqlstr_SPlan; con.Open(); sqlcom.Connection = con; sqlcom.ExecuteNonQuery(); sqlcom.Connection.Close(); string myStringVariable = "your data insert is sauccessfuly"; ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true); clear();
     public void clear()
     { 
    TxtDescription.Text = "";
    ListWeekDay1.Text = "";
    ListStartHour1.Text = "";
    ListEndHour1.Text = "";
    ListStartMinute1.Text = "";
    ListEndMinute1.Text = "";
    ListRoom1.Items.Clear(); }

    Tuesday, January 1, 2019 7:37 AM
  • User-893317190 posted

    Hi dorsa2,

    I still couldn't reproduce your problem.

    Could you show your mark  up in aspx?

    Maybe controls in your aspx cause your problem, for example you use updatepanel in your aspx.

    Best regards,

    Ackerly Xu

    Wednesday, January 2, 2019 2:55 AM