SirReadAlot
asked on
date grid update
Hi experts, I am trying to update a datagrind with this code.
the update statement works.
when i had code it
example
//updateCommand.CommandTex t = "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(Obje ct Sender, DataGridCommandEventArgs e)
{
string employerId = e.Item.Cells[0].Text;
string deduction = ((DropDownList)(e.Item.Fin dControl(" DeductDrop DownList") )).Selecte dItem.Valu e;
Response.Write(deduction);
string ConnectionString = ConfigurationSettings.AppS ettings["c onnectLoca l"];
SqlConnection myConnection = new SqlConnection(ConnectionSt ring);
SqlCommand updateCommand = new SqlCommand();
updateCommand.Connection = myConnection;
//updateCommand.CommandTex t = "Update DestinationElements set Deduction = 'E' where EmployerId = '136728'";
updateCommand.CommandText = "Update DestinationElements set Deduction = @Deduction where EmployerId = @EmployerId";
updateCommand.Parameters.A dd("@Emplo yerId", SqlDbType.NVarChar, 50).Value = employerId;
updateCommand.Parameters.A dd("@Deduc tion", SqlDbType.Char,5).Value = deduction;
try
{
myConnection.Open();
updateCommand.ExecuteNonQu ery();
}
catch (SqlException SqlEx)
{
lblStatus.Text = SqlEx.Message.ToString();
}
catch (Exception ex)
{
lblStatus.Text = ex.Message.ToString();
}
finally
{
myConnection.Close();
}
dgCompensation.EditItemInd ex = -1;
BindGrid();
}
the update statement works.
when i had code it
example
//updateCommand.CommandTex
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(Obje
{
string employerId = e.Item.Cells[0].Text;
string deduction = ((DropDownList)(e.Item.Fin
Response.Write(deduction);
string ConnectionString = ConfigurationSettings.AppS
SqlConnection myConnection = new SqlConnection(ConnectionSt
SqlCommand updateCommand = new SqlCommand();
updateCommand.Connection = myConnection;
//updateCommand.CommandTex
updateCommand.CommandText = "Update DestinationElements set Deduction = @Deduction where EmployerId = @EmployerId";
updateCommand.Parameters.A
updateCommand.Parameters.A
try
{
myConnection.Open();
updateCommand.ExecuteNonQu
}
catch (SqlException SqlEx)
{
lblStatus.Text = SqlEx.Message.ToString();
}
catch (Exception ex)
{
lblStatus.Text = ex.Message.ToString();
}
finally
{
myConnection.Close();
}
dgCompensation.EditItemInd
BindGrid();
}
ASKER
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
is making the error
NOT ERRORING
but i remove the @parameters
ASKER
what shoul i do with these
@ parameters
updateCommand.Parameters.A dd("@Emplo yerId", SqlDbType.NVarChar, 50).Value = employerId;
updateCommand.Parameters.A dd("@Deduc tion", SqlDbType.Char,5).Value = deduction;
@ parameters
updateCommand.Parameters.A
updateCommand.Parameters.A
ASKER
i have tried everything and still no joy
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("{label name}") as Label;
string employerId = l.Text;
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("{label
string employerId = l.Text;
ASKER
this is what i have
string deduction = ((DropDownList)(e.Item.Fin dControl(" DeductDrop DownList") )).Selecte dItem.Valu e;
string deduction = ((DropDownList)(e.Item.Fin
What about the EmployerID? What type of control is used to display it?
ASKER
none actually
what can i do
what can i do
ASKER
please see original post
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.
ASKER
like this
i set emploper col to false
<Columns>
<asp:BoundColumn DataField="EmployerId" HeaderText="Employer ID" ReadOnly="True" HeaderStyle-Font-Bold="Tru e" Visible="False" />
<asp:BoundColumn DataField="DestinationElem ent" HeaderText="Description" ReadOnly="True" HeaderStyle-Font-Bold="Tru e" />
<asp:TemplateColumn HeaderText="Deductions" HeaderStyle-Font-Bold="Tru e">
<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>
i set emploper col to false
<Columns>
<asp:BoundColumn DataField="EmployerId" HeaderText="Employer ID" ReadOnly="True" HeaderStyle-Font-Bold="Tru
<asp:BoundColumn DataField="DestinationElem
<asp:TemplateColumn HeaderText="Deductions" HeaderStyle-Font-Bold="Tru
<ItemTemplate>
<asp:Label id="lblDeduct" runat="server" Text='<%# DataBinder.Eval(Container.
</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>
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.
ASKER
SORRY,
I WAS JUST looking at the database
updateCommand.CommandText = "Update DestinationElements set @DestinationElement = @DestinationElement , @Deduction = @Deduction where EmployerId = @EmployerId and DestinationElements = @DestinationElements";
I WAS JUST looking at the database
updateCommand.CommandText = "Update DestinationElements set @DestinationElement = @DestinationElement , @Deduction = @Deduction where EmployerId = @EmployerId and DestinationElements = @DestinationElements";
ASKER
the value is
1345678
thats the emp id
1345678
thats the emp id
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
yeah, it was a trim problem
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