Solved

date grid update

Posted on 2006-07-05
16
249 Views
Last Modified: 2010-04-16
Hi experts, I am trying to update a datagrind with this code.

the update statement works.

when i had code it

example
//updateCommand.CommandText = "Update DestinationElements set Deduction = 'E' where EmployerId = '136728'";

but it does not update  with the normal code

updateCommand.CommandText = "Update DestinationElements set Deduction = @Deduction where EmployerId = @EmployerId";

also could it be because of this

string employerId = e.Item.Cells[0].Text;

where by employerId = e.Item.Cells[0].Text has no text control in my grid.

what can i do thanks


public void dgCompensation_Update(Object Sender, DataGridCommandEventArgs e)
            {
   
                  string employerId = e.Item.Cells[0].Text;
                  string deduction = ((DropDownList)(e.Item.FindControl("DeductDropDownList"))).SelectedItem.Value;

                  Response.Write(deduction);
            
                  string ConnectionString = ConfigurationSettings.AppSettings["connectLocal"];
                  SqlConnection  myConnection = new SqlConnection(ConnectionString);
                  
                  
                  SqlCommand updateCommand = new SqlCommand();

                  updateCommand.Connection = myConnection;
                 //updateCommand.CommandText = "Update DestinationElements set Deduction = 'E' where EmployerId = '136728'";
            
                    updateCommand.CommandText = "Update DestinationElements set Deduction = @Deduction where EmployerId = @EmployerId";
            
                     updateCommand.Parameters.Add("@EmployerId", SqlDbType.NVarChar, 50).Value = employerId;
                              updateCommand.Parameters.Add("@Deduction", SqlDbType.Char,5).Value = deduction;
               
                  try
                  {
                        myConnection.Open();
                        updateCommand.ExecuteNonQuery();
                      }
   
                  catch (SqlException SqlEx)
                  {
                        lblStatus.Text = SqlEx.Message.ToString();
                  }
   
                  catch (Exception ex)
                  {
                        lblStatus.Text = ex.Message.ToString();
                  }
   
                  finally
                  {
                        myConnection.Close();
                  }
   
                  dgCompensation.EditItemIndex = -1;
                  BindGrid();
   
            }
            
0
Comment
Question by:SirReadAlot
  • 10
  • 5
16 Comments
 
LVL 5

Expert Comment

by:t_itanium
ID: 17042457
hi

first of all there is no point to use parameters if you are not using stored procedures...you use the variables directly into your query

example
"Update DestinationElements set Deduction = '" + deduction + "' where EmployerId = '" + employerId + "'";

then make sure that the types of these variables are the same as used in your database..... i mean if you are using varchar for a variable in the database..you have to use string in the code.. if int you have to use int and not string..

second trace your code using breakpoints and f11 to see where the code is stopping and executing.....also as you said the value of e.Item.Cells[0].Text;
is making the error
0
 

Author Comment

by:SirReadAlot
ID: 17042545
second trace your code using breakpoints and f11 to see where the code is stopping and executing.....also as you said the value of e.Item.Cells[0].Text;
is making the error


NOT ERRORING

but i remove the @parameters
0
 

Author Comment

by:SirReadAlot
ID: 17042581
what shoul i do with these

@ parameters
updateCommand.Parameters.Add("@EmployerId", SqlDbType.NVarChar, 50).Value = employerId;
                  updateCommand.Parameters.Add("@Deduction", SqlDbType.Char,5).Value = deduction;
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:SirReadAlot
ID: 17042661
i have tried everything and still no joy
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 17043249
Actually, parameters are a good idea because they prevent SQL injection attacks.  That is not your core problem here.

What is the name of the control that you are trying to get the value from?  For example, if it is a label I would use:

Label l = e.Item.FindControl("{labelname}") as Label;
string employerId = l.Text;


0
 

Author Comment

by:SirReadAlot
ID: 17043383
this is what i have

   string deduction = ((DropDownList)(e.Item.FindControl("DeductDropDownList"))).SelectedItem.Value;
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 17043394
What about the EmployerID?  What type of control is used to display it?  
0
 

Author Comment

by:SirReadAlot
ID: 17043407
none actually


what can i do
0
 

Author Comment

by:SirReadAlot
ID: 17043419
please see original post
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 17043424
It needs to be displayed in a server-side control if you want to access it from code-behind.  Text displayed via direct script code (<% %> blocks) will not be accessible from server-side code.
0
 

Author Comment

by:SirReadAlot
ID: 17043451
like this

i set emploper col to false

<Columns>
                                                                  <asp:BoundColumn DataField="EmployerId" HeaderText="Employer ID" ReadOnly="True" HeaderStyle-Font-Bold="True" Visible="False" />
                                                                  <asp:BoundColumn DataField="DestinationElement" HeaderText="Description" ReadOnly="True" HeaderStyle-Font-Bold="True" />
                                                                  <asp:TemplateColumn HeaderText="Deductions" HeaderStyle-Font-Bold="True">
                                                                        <ItemTemplate>
                                                                              <asp:Label id="lblDeduct" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Deduction") %>'/>
                                                                        </ItemTemplate>
                                                                        <EditItemTemplate>
                                                                              <asp:DropDownList id="DeductDropDownList" Runat="Server" DataSource="<%# GetDeductionDataTable() %>" DataTextField="Deduction" DataValueField="Deduction" />
                                                                        </EditItemTemplate>
                                                                  </asp:TemplateColumn>
                                                                  <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="save" ItemStyle-Width="50" ButtonType="LinkButton" />
                                                            </Columns>
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 17043676
What does "e.Item.Cells[0].Text " show in the debugger?  What is the count of e.Item.Cells[0]? (should be 4) I ran a quick test with your code and e.Item.Cells[0].Text showed a value.  
0
 

Author Comment

by:SirReadAlot
ID: 17043984
SORRY,

I WAS JUST looking at the database
updateCommand.CommandText = "Update DestinationElements set @DestinationElement = @DestinationElement , @Deduction = @Deduction  where EmployerId = @EmployerId and DestinationElements  = @DestinationElements";
                  
0
 

Author Comment

by:SirReadAlot
ID: 17044015
the value is

1345678

thats the emp id
0
 
LVL 25

Accepted Solution

by:
dstanley9 earned 500 total points
ID: 17044070
Are you getting an exception, or is it just not updating?  You could look at the activity in SQL Profiler and see what command is being executed, check the parameter values, etc.  Your SQL and parameter syntax looks OK.
You might also try calling Trim() on the string to make sure there are no excess spaces.
0
 

Author Comment

by:SirReadAlot
ID: 17044171
yeah,  it was a trim problem
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Entity Framework 7 41
Wav problem 4 25
What can cause the styling on a .NET site to not be found? 17 24
Printing 1 50
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question