473,385 Members | 1,329 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,385 software developers and data experts.

Gridviews

147 100+
I am now frustrated! :(

I want to edit a gridview and all I need to do is update a value from the field Title to the Title field in the database.

So, as you know, when you click on edit you have the options update and cancel and the fields turn into textboxes, but how do I know what the text boxes are called as all I do is specify <asp:boundfield paramaters (which doesn't us an ID) > as a paramenter. The problem is, I don't know what the textbox is called! I am also not using a asp:gridview DataSourceID - I am doing this code behind

My code

html
Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="grdViewClients" runat="server" CellPadding="3" CellSpacing="1" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" OnRowEditing="grdViewClients_RowEdit" OnRowUpdating="grdViewClients_RowUpdating" OnRowCancelingEdit="grdViewClients_RowCancelEdit">
  2.  
  3.      <AlternatingRowStyle BackColor="White" />
  4.         <RowStyle BackColor="#DAFCD2" />
  5.         <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  6.         <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
  7.         <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
  8.         <HeaderStyle BackColor="#4F6F18" Font-Bold="True" ForeColor="White" />
  9.         <EditRowStyle BackColor="#2461BF" />
  10.  
  11.         <Columns>
  12.             <asp:CommandField HeaderText="Edit" ShowEditButton="True" ShowHeader="True" />
  13.             <asp:BoundField DataField ="Title" HeaderText="Title" />           
  14.         </Columns>
  15.  
  16.       </asp:GridView>  
and code behind (only showing the Updating sub)
Expand|Select|Wrap|Line Numbers
  1.  protected void grdViewClients_RowUpdating(object sender, GridViewUpdateEventArgs e)
  2.     {
  3.  
  4.         string strConn = ConfigurationManager.ConnectionStrings["kestrel"].ConnectionString;
  5.         SqlConnection Conn = new SqlConnection(strConn);
  6.  
  7.         SqlCommand SqlUpdate = new SqlCommand("UPDATE Clients SET Title=@Title WHERE ID = '"+Request.QueryString["id"]+"'", Conn);
  8.  
  9.         SqlUpdate.CommandType = CommandType.Text;
  10.  
  11.         //TextBox txtTitle = (TextBox)grdViewClients.Rows[e.RowIndex].Cells[0].FindControl("Title");
  12.  
  13.         TextBox txtTitle = (TextBox)grdViewClients.Rows[0].Cells[0].FindControl("Title");
  14.  
  15.  
  16.         SqlUpdate.Parameters.Add("@Title", SqlDbType.NVarChar).Value = txtTitle.Text;
  17.  
  18.         if (Conn.State == ConnectionState.Closed)
  19.         {
  20.             Conn.Open();
  21.         }
  22.             SqlUpdate.ExecuteNonQuery();
  23.  
  24.         if (Conn.State == ConnectionState.Open)
  25.         {
  26.             Conn.Close();
  27.         }
  28.     }
The error is
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Help please
Feb 25 '10 #1
6 1414
semomaniz
210 Expert 100+
This might be of help

http://msdn.microsoft.com/en-us/libr...wupdating.aspx
Feb 25 '10 #2
DaveRook
147 100+
Hi

This doesn't answer the question.

In grid view, we display an item like
<asp:BoundField DataField ="Title" HeaderText="Title" />.

I have also used
<asp:CommandField HeaderText="Edit" ShowEditButton="True" ShowHeader="True" />

When rendered in a browser, clicking the edit tag changes the table (which showed the Title field) into textboxes.

How do I find out what the textboxes names are?

Dave
Feb 26 '10 #3
semomaniz
210 Expert 100+
Expand|Select|Wrap|Line Numbers
  1.   void CustomersGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
  2.   {
  3.  
  4.     // Iterate through the NewValues collection and HTML encode all 
  5.     // user-provided values before updating the data source.
  6.     foreach (DictionaryEntry entry in e.NewValues)
  7.     {
  8.  
  9.       e.NewValues[entry.Key] = Server.HtmlEncode(entry.Value.ToString());
  10.  
  11.     }
  12.  
  13.   }
  14.  
  15.  
  16.  
The first example on the link should have given you an idea. Hope this is what you are looking for
Feb 26 '10 #4
DaveRook
147 100+
Semomaniz,

Thank you for being patient with me.

I understand that the foreach loop looks through each item/field for new values (I assume compared to a cache of the actual database).

Within the foreach loop you suggested, I have added
Expand|Select|Wrap|Line Numbers
  1. Response.Redirect("?err=anything");
and it doesn't forward.

Also, with this suggestion is puts the first column content into column 2 (the first cell being the edit column, the second being the Title column). This means when I change the Title field from 'Mr' to 'ABC' and click update, regardless of what I write in the text box it adds the word 'edit' to Title.

Dave
Feb 26 '10 #5
DaveRook
147 100+
ARRRGGGHHHH

Totally miss-read the MSDN link and page! Sorry. I'm now with it and can start working on it! Sorry

(if only their was a delete option to protect my pride....)
Feb 26 '10 #6
semomaniz
210 Expert 100+
I am glad its working for you
Feb 26 '10 #7

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

Similar topics

0
by: manuel.ricca | last post by:
Hello, I'm trying to create a table with 2 nested gridviews and then a DetailsView on the right. The DetailsView should show the details of the item selected in the 2nd (nested) GridView: My...
0
by: Roy | last post by:
Hey all, I must be losing my touch. I have made many pages in the 1.1 framework that utilize custom bidirectional paging in datagrids. We've converted over to 2.0 and I've been trying to use the...
1
by: Roy | last post by:
Hey all. Below is the nested syntax on how to make a "codeless" nested gridview embedded within another gridviews templatefield column. Only problem is that it loads slow. REAL SLOW. There has to...
2
by: Hennie | last post by:
I have 2 gridviews on a page, each with own SQL Datasource pointing to different tables on same database. In the 1st date format is "dd/MM/yyyy" Other 1 "MM/dd/yyyy hh:mm:ss" In the SQL...
1
by: Carlos | last post by:
Hi all, I need to place 5 gridviews next to each other. However, during esign time, the gridviews are placed under the previous.I played with the position style of the document but it did not...
0
by: HP | last post by:
Hi there I have a datalist control with some bound controls in its Item Template and a gridview bound to one of those fields (residing also in Item Template). I've found out that when I click...
0
by: shapper | last post by:
Hello, I created 2 GridViews, A and B. I created 2 datasets from Asp.Net 2.0 profile data. Each GridView has a Bound Field and a Button Field. When a button on GridView A is clicked the...
1
by: Chris | last post by:
I am creating a nested gridview as per the tutorial here (http://msdn2.microsoft.com/en-us/library/aa992038(vs.80).aspx). My gridviews work fine. I have a master gridview containing the customerid....
0
by: trint | last post by:
I have a 5 gridviews on one aspx page in c# that work like a java menu as selections are made by category. (The first one is on the left, as selections are made the following 4 appear across the...
8
by: stoogots2 | last post by:
I would like to reuse the code for gridview sorting for each of the several GridViews that I have on one page. I've not done this before, so I am seeking a more elegant and reusable solution than...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...

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.