locked
Trying to retrieve SQL data and display in TextBoxes on asp webform and update back to SQL RRS feed

  • Question

  • User78096478 posted

    I'm trying to create a Search button that fills the text boxes on my webform with data from a SQL database.  And I'd like an update button as well so that if any changes were made the records would be updated.  Can someone please help?  I've found many resources online but I'm missing something.  Right now, basically nothing happens when I try to search or update.  I'm very new to asp.net and C#.  Many thanks in advance.

    asp form:

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">

    </head>
    <body>
    <form id="form1" runat="server">
    <div class="content">
    <h4>none</h4>
    <table class="provide-text0">
    <tr>
    <td class="th-text"><b>Unit Type:</b>
    </td>
    <td class="auto-style2">
    <asp:DropDownList ID="unittype" class="textboxnew" runat="server" OnSelectedIndexChanged="unittype_SelectedIndexChanged" DataSourceID="SqlDataSource1" DataTextField="type" DataValueField="type"></asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:sqlServer %>" SelectCommand="SELECT [type] FROM [unit_type]"></asp:SqlDataSource>
    <br />
    </td>
    </tr>
    <tr>
    <td class="th-text"><b>Status:</b>
    </td>
    <td class="auto-style2">
    <asp:DropDownList ID="status" class="textboxnew" runat="server" OnSelectedIndexChanged="status_SelectedIndexChanged" DataSourceID="SqlDataSource2" DataTextField="status" DataValueField="status"></asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:sqlServer %>" SelectCommand="SELECT [status] FROM [status]"></asp:SqlDataSource>
    </td>
    </tr>
    <tr>
    <td class="th-text"><b>Serial Number :</b>
    </td>
    <td class="auto-style2">
    <asp:TextBox ID="serialnumber" class="textboxnew" runat="server" OnTextChanged="serialnumber_TextChanged"></asp:TextBox>
    <asp:RequiredFieldValidator runat="server" ID="Reqserialnumber" ForeColor="Red" ControlToValidate="serialnumber" ErrorMessage="*" />
    </td>
    </tr>
    </table>
    </div>
    <div>
    <p>
    <asp:Button ID="SearchButton" runat="server" class="block" Text="Search" CausesValidation="False" BackColor="#3399FF" OnClick="SearchBtn_Click" />
    </p>
    <p>
    <asp:Button ID="UpdateButton" runat="server" class="block" Text="Update" CausesValidation="False" BackColor="#3399FF" OnClick="UpdateBtn_Click" />
    </p>
    <p>
    <asp:Button ID="BackButton" runat="server" class="block" Text="Back" CausesValidation="False" OnClick="BackBtn_Click" />
    </p>
    </div>

    </form>
    </body>
    </html>

    c#:

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace inventory
    {
    public partial class Search : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    PopulateFields();
    }
    }

    private void PopulateFields()
    {
    using (SqlConnection con1 = new SqlConnection("Data Source=USER-PC;Initial Catalog=webservice_database;Integrated Security=True"))
    {
    DataTable dt = new DataTable();
    con1.Open();
    SqlDataReader myReader = null;
    SqlCommand myCommand = new SqlCommand("select * from inventory where serialnumber='" + Session["serialnumber"] + "'", con1);

    myReader = myCommand.ExecuteReader();

    while (myReader.Read())
    {
    unittype.Text = (myReader["unittype"].ToString());
    status.Text = (myReader["status"].ToString());
    serialnumber.Text = (myReader["serialnumber"].ToString());
    }
    con1.Close();
    }//end using
    }

    protected void unittype_TextChanged(object sender, EventArgs e)
    {

    }

    protected void status_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void unittype_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void serialnumber_TextChanged(object sender, EventArgs e)
    {

    }

    protected void intrinsicallysafecheck_TextChanged(object sender, EventArgs e)
    {

    }

    protected void SubmitBtn_Click(object sender, EventArgs e)
    {

    }

    protected void BackBtn_Click(object sender, EventArgs e)
    {
    Response.Redirect("start.aspx");
    }

    protected void UpdateBtn_Click(object sender, EventArgs e)
    {
    SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=webservice_database;Integrated Security=True");
    SqlCommand cmd = new SqlCommand("UPDATE inventory SET unittype = @unittype, status = @status, serialnumber = @serialnumber where serialnumber='" + Session["serialnumber"] + "'", con);
    con.Open();
    cmd.Parameters.AddWithValue("@unittype", unittype.Text);
    cmd.Parameters.AddWithValue("@status", status.Text);
    cmd.Parameters.AddWithValue("@serialnumber", serialnumber.Text);
    cmd.ExecuteNonQuery();
    con.Close();
    }

    protected void SearchBtn_Click(object sender, EventArgs e)
    {

    }
    }
    }

    Thursday, January 9, 2020 6:49 PM

Answers

  • User288213138 posted

    Hi myaspaccount2019,

    I'm trying to create a Search button that fills the text boxes on my webform with data from a SQL database.  And I'd like an update button as well so that if any changes were made the records would be updated. 

    According to your description, I couldn’t understand your requirement clearly.

    Do you mean you want to display and update the data in the database? 

    If this is your requirement, I suggest you could try to below codes:

    <asp:TextBox ID="unittype" runat="server"></asp:TextBox>
                <asp:TextBox ID="status" runat="server"></asp:TextBox>
                <asp:TextBox ID="serialnumber" runat="server"></asp:TextBox>
                <asp:Button ID="SearchBtn" runat="server" Text="Search" OnClick="SearchBtn_Click" />
                <asp:Button ID="UpdateBtn" runat="server" Text="Update" OnClick="UpdateBtn_Click" />
    
    
    string constr = ConfigurationManager.ConnectionStrings["constr1"].ConnectionString;
            protected void UpdateBtn_Click(object sender, EventArgs e)
            {
                Session["serialnumber"] = 1;
                SqlConnection con = new SqlConnection(constr);
                SqlCommand cmd = new SqlCommand("UPDATE inventory SET unittype = @unittype, status = @status, serialnumber = @serialnumber where serialnumber='" + Session["serialnumber"] + "'", con);
                con.Open();
                cmd.Parameters.AddWithValue("@unittype", unittype.Text);
                cmd.Parameters.AddWithValue("@status", status.Text);
                cmd.Parameters.AddWithValue("@serialnumber", serialnumber.Text);
                cmd.ExecuteNonQuery();
                con.Close();
            }
    
            protected void SearchBtn_Click(object sender, EventArgs e)
            {
                using (SqlConnection con1 = new SqlConnection(constr))
                {
                    DataTable dt = new DataTable();
                    con1.Open();
                    SqlDataReader myReader = null;
                    SqlCommand myCommand = new SqlCommand("select * from inventory", con1);
    
                    myReader = myCommand.ExecuteReader();
    
                    while (myReader.Read())
                    {
                        unittype.Text = (myReader["unittype"].ToString());
                        status.Text = (myReader["status"].ToString());
                        serialnumber.Text = (myReader["serialnumber"].ToString());
                    }
                    con1.Close();
                }
            }

    If I misunderstand your requirement, please post more details information about your requirement.

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 10, 2020 6:11 AM

All replies

  • User-718146471 posted

    Based on what I'm seeing in your code, you have numerous methods but no actions indicated. I suspect that is why nothing is happening. What I'd suggest is set breakpoints at each method and watch the program flow. If you enter into a breakpoint on one of the empty methods, the business logic may not know what to do which is why you are not seeing anything.

    Thursday, January 9, 2020 8:10 PM
  • User409696431 posted

    What are you expecting to happen when you search?  The only thing that search button will do is post back.  You have no code in the function that handles the click :

        protected void SearchBtn_Click(object sender, EventArgs e)
        {
    
        }

    And when the page posts back, you do nothing:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack) //is hit on the initial page load, not on postback
            {
                PopulateFields();
            }
        }

    Show us code that you tried for search, and explain what happened.  Showing us no code simply means, well, of course nothing happened.  :)

    Thursday, January 9, 2020 10:33 PM
  • User288213138 posted

    Hi myaspaccount2019,

    I'm trying to create a Search button that fills the text boxes on my webform with data from a SQL database.  And I'd like an update button as well so that if any changes were made the records would be updated. 

    According to your description, I couldn’t understand your requirement clearly.

    Do you mean you want to display and update the data in the database? 

    If this is your requirement, I suggest you could try to below codes:

    <asp:TextBox ID="unittype" runat="server"></asp:TextBox>
                <asp:TextBox ID="status" runat="server"></asp:TextBox>
                <asp:TextBox ID="serialnumber" runat="server"></asp:TextBox>
                <asp:Button ID="SearchBtn" runat="server" Text="Search" OnClick="SearchBtn_Click" />
                <asp:Button ID="UpdateBtn" runat="server" Text="Update" OnClick="UpdateBtn_Click" />
    
    
    string constr = ConfigurationManager.ConnectionStrings["constr1"].ConnectionString;
            protected void UpdateBtn_Click(object sender, EventArgs e)
            {
                Session["serialnumber"] = 1;
                SqlConnection con = new SqlConnection(constr);
                SqlCommand cmd = new SqlCommand("UPDATE inventory SET unittype = @unittype, status = @status, serialnumber = @serialnumber where serialnumber='" + Session["serialnumber"] + "'", con);
                con.Open();
                cmd.Parameters.AddWithValue("@unittype", unittype.Text);
                cmd.Parameters.AddWithValue("@status", status.Text);
                cmd.Parameters.AddWithValue("@serialnumber", serialnumber.Text);
                cmd.ExecuteNonQuery();
                con.Close();
            }
    
            protected void SearchBtn_Click(object sender, EventArgs e)
            {
                using (SqlConnection con1 = new SqlConnection(constr))
                {
                    DataTable dt = new DataTable();
                    con1.Open();
                    SqlDataReader myReader = null;
                    SqlCommand myCommand = new SqlCommand("select * from inventory", con1);
    
                    myReader = myCommand.ExecuteReader();
    
                    while (myReader.Read())
                    {
                        unittype.Text = (myReader["unittype"].ToString());
                        status.Text = (myReader["status"].ToString());
                        serialnumber.Text = (myReader["serialnumber"].ToString());
                    }
                    con1.Close();
                }
            }

    If I misunderstand your requirement, please post more details information about your requirement.

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 10, 2020 6:11 AM
  • User78096478 posted

    Thanks so much for this code, it helped me understand how it this all works.  However, entering a serial that exists in the database and then clicking the search button only gives me data from the last row in the database.  Any idea why that would be?

    Thanks again.

    Friday, January 10, 2020 1:18 PM
  • User475983607 posted

    Thanks so much for this code, it helped me understand how it this all works.  However, entering a serial that exists in the database and then clicking the search button only gives me data from the last row in the database.  Any idea why that would be?

    There is a bug in your code/design.  Share your code if you want community debugging support.  Otherwise, learn how to troubleshoot using the Visual Studio debugger.

    https://docs.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2019

    Friday, January 10, 2020 1:31 PM