---
title: "How to embed asp.net with SQL Server reading data and changing textbox field?"  
description: "How to embed asp.net with SQL Server reading data and changing textbox field?"  
author: "Barbara Jones"  
published: 2015-01-29  
updated: 2015-01-29  
canonical: https://www.mindstick.com/forum/12915/how-to-embed-asp-dot-net-with-sql-server-reading-data-and-changing-textbox-field  
category: "asp.net"  
tags: ["c#", ".net", "sql server"]  
reading_time: 1 minute  

---

# How to embed asp.net with SQL Server reading data and changing textbox field?

I am creating a [sql](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) project. I used a SqlDatareader and [textbox](https://www.mindstick.com/forum/12714/dynamic-textbox-in-gridview), but when I run it I got an [error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing)

**InvalidOperationException**

My [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) 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();                }            }        }
```

## Replies

### Reply by Tom Cruser

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.


---

Original Source: https://www.mindstick.com/forum/12915/how-to-embed-asp-dot-net-with-sql-server-reading-data-and-changing-textbox-field

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
