HOW TO EMBED ASP.NET WITH SQL SERVER READING DATA AND CHANGING TEXTBOX FIELD?

Barbara Jones

Total Post:13

Points:91
Posted by  Barbara Jones
 914  View(s)
Ratings:
Rate this:

I am creating a sql project. I used a SqlDatareader and textbox, but when I run it I got an error

InvalidOperationException

My code is this, thanks for your help.

    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedIndex == 0)
        {
                string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
                using (SqlConnection con = new SqlConnection(CS))
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand
                        ("USE [PRODUCTS] SELECT QUALITIES FROM dbo.COMPUTERS WHERE ID = 0", con);
                    SqlDataReader reader;
                    reader = cmd.ExecuteReader();
                    TextBox1.Text = reader["QUALITIES"].ToString();
                }
            }
        }

  1. Tom Cruser

    Post:28

    Points:196
    Re: How to embed asp.net with SQL Server reading data and changing textbox field?

    You are not accounting for the case where your data reader has no rows.

    Try this:

    while(reader.Read())
    {
        TextBox1.Text =reader["QUALITIES"].ToString();
    }

    Also note that the "Qualities" field in your database could potentially be null. You will want to protect against this as well.

      Modified On Apr-09-2018 12:30:14 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!