473,395 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

GridView: Binding Database Fields to Textboxes

63
I am connecting to a database table to pass values to textboxes in a gridview. I get the following error:

Object reference not set to an instance of an object.

Thanks in advance for your help!

Expand|Select|Wrap|Line Numbers
  1.  
  2. private void ReadProjects()
  3.     {
  4.  
  5.         SqlDataReader rdr;
  6.  
  7.         SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
  8.  
  9.         SqlCommand cmd = new SqlCommand("SELECT * FROM Projects", conn);
  10.  
  11.         cmd.CommandType = CommandType.Text;
  12.  
  13.         int rowIndex = 0;
  14.  
  15.         if (ViewState["CurrentTable"] != null)
  16.         {
  17.  
  18.             DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
  19.             DataRow dr = null;
  20.  
  21.             if (dtCurrentTable.Rows.Count > 0)
  22.             {
  23.  
  24.                 for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
  25.                 {
  26.  
  27.                     TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("project_name");
  28.                     TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("project_manager");
  29.                     TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("team_members");
  30.                     TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("status");
  31.  
  32.                     using (conn)
  33.                     {
  34.                         //open connection
  35.                         conn.Open();
  36.  
  37.                         rdr = cmd.ExecuteReader();
  38.  
  39.                         while (rdr.Read())
  40.                         {
  41.                             dr = dtCurrentTable.NewRow();
  42.  
  43.                             box1.Text = rdr["ProjectName"].ToString();
  44.                             box2.Text = rdr["ProjectID"].ToString();
  45.                             box3.Text = rdr["CompanyID"].ToString();
  46.                             box4.Text = rdr["Description"].ToString();
  47.  
  48.                             dtCurrentTable.Rows.Add(dr);
  49.  
  50.                             dtCurrentTable.AcceptChanges();
  51.                         }
  52.                     }
  53.  
  54.                     rowIndex++;
  55.  
  56.                 }
  57.  
  58.                 ViewState["CurrentTable"] = dtCurrentTable;
  59.  
  60.  
  61.             }
  62.  
  63.        }
  64.  
  65.         else
  66.         {
  67.  
  68.             Response.Write("ViewState is null");
  69.  
  70.         }
  71.  
  72.     }
  73. }
  74.  
  75.  
Dec 4 '10 #1
0 1198

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Sven Mayer | last post by:
Assume a database (e.g. Oracle) field is declared as a) NUMBER (length=10) b) NUMBER (length=26) c) VARCHAR2 (length=1) d) VARCHAR2 (length=50) e) DATE How do I retrieve the contents from...
2
by: alpoor | last post by:
I have lots of textboxes on my webform. I need to bind them with fields from table. I am not sure how to bind with textboxes. I have seen so many samples for dropdown boxes and datagrid binding,...
1
by: Smash | last post by:
I have problem binding gridview.... At first i didn't want to bind gridview at first page load...so in my objectdatasource_selecting event i wrote this: If Not Page.IsPostBack Then e.Cancel =...
3
by: mateo | last post by:
Hello, I have a GridView inside which i want to show 3 dropdownList (one for each Column). So i've created 3 TemplateField Continent Country City
7
by: genc_ymeri | last post by:
Hi, I would like to bind the hyperlink column but I don't see how to. I tried somethink like this but instead of showing ID nummbers it shows "ID=ID" Any tip is very much appreciated. Genc
5
by: Amit | last post by:
Hello, I have a simple search screen, with two drop-downs and a text box. There's also a GridView control that is using a SqlDataSource control to show the matching results. The SqlDataSource uses...
8
by: AG | last post by:
ASP.NET 2.0, VS 2005 I have a gridview with paging enabled, bound to an objectdatasource. Both were dropped on to the page and configured via the wizards. Basically working as expected. The...
0
by: ashish ranjan | last post by:
Hi all, I have to add a new row in footer of the gridview.All columns are bounded (value coming from table in database).But footer Should not be bounded And this footer row will have one "Add"...
4
by: beton3000 | last post by:
Hello! I'm building a GridView using code to add template fields, because there is a random number of columns in result set from database. I'm using a class with ITemplate interface for defining...
5
by: mpundu | last post by:
This is my form: =========================== <% form_tag :action => 'update', :id => @book.id, :title => @book.title, :price => @book.price, :description => @book.description %> <%= render...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.