Select First then Insert it (Noob Question)

Last post 07-05-2008 4:28 AM by jordan3114. 10 replies.

Sort Posts:

  • Select First then Insert it (Noob Question)

    06-30-2008, 11:23 AM
    • Loading...
    • jordan3114
    • Joined on 07-15-2007, 5:04 AM
    • Posts 60

    Hi, Im new in ASP.NET and SQL Server, i have created 1 table named detail with 3 field which is d_name, d_age, d_address

     below is my table data
     d_name        d_age        d_country     d_gender
     iverson        32             USA                 male
     jordan         45             USA                  male
    in aspx file i have 3 textbox and 1 button
    txtName, txtAge, txtCountry, btnAdd 
    let say if i entered the iverson in txtName then it will auto retrieve the value after the textchanged   32 (txtAge) , USA (txtCountry) then insert new record again
     Pls Help..
     Thanks 
     
  • Re: Select First then Insert it (Noob Question)

    06-30-2008, 11:58 AM
    • Loading...
    • tjrice1906
    • Joined on 02-05-2008, 7:20 PM
    • Posts 14

    Jordan - I'm a little confused with your question. Are you saying you want the fields to automatically update back to the database without having a user input..... lets say an update button?

  • Re: Select First then Insert it (Noob Question)

    06-30-2008, 11:07 PM
    • Loading...
    • jordan3114
    • Joined on 07-15-2007, 5:04 AM
    • Posts 60

    not update, i want it retrieve the the data first (if the data is match from the d_name field) then insert new record again.

  • Re: Select First then Insert it (Noob Question)

    07-03-2008, 6:14 AM
    Hi jordan3114,
    From your description, it seems that you want to avoid inserting duplicate records.
    Please refer to my suggestion below:
     
    .aspx:

     

    <asp:TextBox ID="txtName" runat="server" AutoPostBack="True" ontextchanged="txtName_TextChanged"></asp:TextBox>
    <asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
    <asp:TextBox ID="txtCountry" runat="server" ></asp:TextBox>
    <asp:Button ID="btn_add" runat="server" Text="Button" onclick="Button1_Click" />

     .cs:

    protected void txtName_TextChanged(object sender, EventArgs e)
    {
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ToString()))// use your connection string here
        {
            SqlCommand cmd = new SqlCommand("select * from detail where d_name=@d_name", con);
            cmd.Parameters.AddWithValue("@d_name", txtName.Text);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr != null)
            {
                while (dr.Read())
                {
                    Response.Write("<script>alert('Name:" + dr["d_name"].ToString() + " already exists! \\nAge:" + dr["d_age"] + "\\nCountry:" + dr["d_country"] + " ')</script>");  
                }
    
            }
    
            // clear the textbox to insert new records.
            txtName.Text = "";
            txtAge.Text = "";
            txtCountry.Text = "";
    
           
        }
    }

    When we type a name which already exists in table “detail”,suppose “jordan”, the alert dialog will display the age and country information of “jordan”,
    then the textbox controls will be cleared up to insert another new record. 

    Hope this helps.

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Re: Select First then Insert it (Noob Question)

    07-04-2008, 9:14 PM
    • Loading...
    • jordan3114
    • Joined on 07-15-2007, 5:04 AM
    • Posts 60

    When the textchanged fired (auto postback) it will cause the error as below:

    Invalid attempt to read when no data is present.

      Can i prevent this error ? , because i need the textchanged is fired whether got data or no data then insert all textbox again ?

     Thanks


     

  • Re: Select First then Insert it (Noob Question)

    07-04-2008, 9:36 PM

    Hi jordan3114,
    Could you please provide your code?
    Usually, this error because you don’t call the “SqlDataReader.Read()”, which advances the “SqlDataReader” to the next row.

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Re: Select First then Insert it (Noob Question)

    07-05-2008, 3:39 AM
    • Loading...
    • jordan3114
    • Joined on 07-15-2007, 5:04 AM
    • Posts 60

    Hi, this is my code:

    protected void txtPartNo_TextChanged(object sender, EventArgs e)
            {
                string selectSQL;
                selectSQL = "SELECT * FROM part ";
                selectSQL += "WHERE part_no='" + txtPartNo.Text + "' ";
                SqlConnection con = new SqlConnection(connectionString);
                SqlCommand cmd = new SqlCommand(selectSQL, con);
                SqlDataReader reader;


                    con.Open();
                    reader = cmd.ExecuteReader();
                    reader.Read();

                    txtPartNo.Text = reader["part_no"].ToString();
                    txtDesc.Text = reader["part_desc"].ToString();
                    txtSize.Text = reader["part_size"].ToString();
                    txtQty.Text = reader["part_quantity"].ToString();
                    //txtLotNo.Text = reader["part_lot_no"].ToString();
                    //txtQC.Text = reader["part_qc_accept"].ToString();
                    reader.Close();
                    //txtPartNo.Text = "";
                    //txtDesc.Text = "";
                    //txtSize.Text = "";
                    lblResult.Text = "";

                    con.Close();
            }

            protected void btnInsert_Click(object sender, EventArgs e)
            {
                if (txtPartNo.Text == "")
                {
                    lblResult.Text = "Record require Item Part No";
                    return;
                }

                string insertSQL;
                insertSQL = "INSERT INTO part( ";
                insertSQL += "part_no, part_desc, part_size, part_quantity, part_lot_no, part_qc_accept) ";
                insertSQL += "VALUES (";
                insertSQL += "@part_no, @part_desc, @part_size, @part_quantity, @part_lot_no, @part_qc_accept) ";

                SqlConnection con = new SqlConnection(connectionString);
                SqlCommand cmd = new SqlCommand(insertSQL, con);

                cmd.Parameters.AddWithValue("@part_no", txtPartNo.Text);
                cmd.Parameters.AddWithValue("@part_desc", txtDesc.Text);
                cmd.Parameters.AddWithValue("@part_size", txtSize.Text);
                cmd.Parameters.AddWithValue("@part_quantity", txtQty.Text);
                cmd.Parameters.AddWithValue("@part_lot_no", txtLotNo.Text);
                cmd.Parameters.AddWithValue("@part_qc_accept", txtQC.Text);

                int added = 0;
                try
                {
                    con.Open();
                    added = cmd.ExecuteNonQuery();
                    lblResult.Text = added.ToString() + " Record is Added.";
                }
                catch (Exception error)
                {
                    lblResult.Text = "Error Adding Record. ";
                    lblResult.Text += error.Message;
                }
                finally
                {
                    con.Close();
                }

                if (added > 0)
                {
                    txtPartNo.Text = "";
                    txtDesc.Text = "";
                    txtSize.Text = "";
                    txtQty.Text = "";
                    txtLotNo.Text = "";
                    txtQC.Text = "";
                }
            }

  • Re: Select First then Insert it (Noob Question)

    07-05-2008, 3:52 AM
    Answer

    Hi jordan3114,
    Please add an IF statement in your code and check if it works.

    if(reader.Read())
    {
        txtPartNo.Text = reader["part_no"].ToString();
        txtDesc.Text = reader["part_desc"].ToString();
        txtSize.Text = reader["part_size"].ToString();
        txtQty.Text = reader["part_quantity"].ToString();
        //txtLotNo.Text = reader["part_lot_no"].ToString();
        //txtQC.Text = reader["part_qc_accept"].ToString();
    }
     

     

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Re: Select First then Insert it (Noob Question)

    07-05-2008, 4:13 AM
    • Loading...
    • jordan3114
    • Joined on 07-15-2007, 5:04 AM
    • Posts 60

    When i run textchanged it got error as below: 
    Invalid object name 'part'
     at this line: reader = cmd.ExecuteReader();
     

  • Re: Select First then Insert it (Noob Question)

    07-05-2008, 4:23 AM

    Hi jordan3114,
    It seems that the table named “part” cannot be found, so maybe there is something wrong with your connection string or T-SQL script, please check them.

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Re: Select First then Insert it (Noob Question)

    07-05-2008, 4:28 AM
    • Loading...
    • jordan3114
    • Joined on 07-15-2007, 5:04 AM
    • Posts 60

    You are right, i forgot my table is deleted Embarrassed, after put if (reader.Read()) it work great, My problem is solved.

    Thanks a lot  Jian Kang.

Page 1 of 1 (11 items)
Microsoft Communities
Page view counter