Errors with Update and Delete functions of gridview control

D

Danielle

Hello all -

Thank you in advance for any help you are able to provide.

I am populating a gridview from a stored procuedure. The returned data
is a name, phone number, email and guid of a contact in our database.

I also need to use the Edit and Delete funtions in the gridview.
Neither seems to be working as intended.

DeleteCommand="delete from contactTable where Name = @ContactName and
Email = @ContactEmail and Phone = @ContactPhone and
(CAST(_ResourceGuid AS varchar(36)) = @Guid)"

When I try to delete a contact, I get this error

System.Data.SqlClient.SqlException: Must declare the scalar variable
"@ContactName".

So, I added the Delete Parametres:

<DeleteParameters>
<asp:parameter Name="Contactame" Type="string" />
<asp:parameter Name="ContactEmail" Type="string" />
<asp:parameter Name="ContactPhone" Type="string" />
<asp:parameter Name="Guid" Type="string" />
</DeleteParameters>

Now, when I try to delete, I get this:

System.InvalidCastException: Object must implement IConvertible.

The strange thing is, I am able to Update without having the
parameters specified using this

UpdateCommand="update Altiris.dbo.Inv_Current_Allowed_Contacts SET
Name = @ContactName, Email = @ContactEmail, Phone = @ContactPhone
WHERE (CAST(_ResourceGuid AS varchar(36)) = @Guid)"

but once I add in a set of UpdateParamters similar to the parameters
indicated above I get the IConvertible error again.

I'm coding in VS Web Developer 2005. The full code for the gridview
control (with names changed to protect the innocent) is below. By
the way, I am showing the footer in advance of implementing an insert
function...

Thanks-
Danielle

<asp:GridView ID="gdContacts" runat="server"
AutoGenerateColumns="False"
AutoGenerateEditButton="True"
AutoGenerateDeleteButton="True"
DataSourceID="sqlContactInfo"
DataKeyNames="Guid"
ShowFooter="True">
<Columns>
<asp:TemplateField HeaderText="Contact Name"
SortExpression="ContactName">
<EditItemTemplate>
<asp:TextBox ID="txtContactName"
runat="server" Text='<%# Bind("ContactName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblContactName" runat="server"
Text='<%# Bind("ContactName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Contact Email"
SortExpression="ContactEmail">
<EditItemTemplate>
<asp:TextBox ID="txtContactEmail"
runat="server" Text='<%# Bind("ContactEmail") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblContactEmail" runat="server"
Text='<%# Bind("ContactEmail") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Contact Phone"
SortExpression="ContactPhone">
<EditItemTemplate>
<asp:TextBox ID="txtContactPhone"
runat="server" Text='<%# Bind("ContactPhone") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblContactPhone" runat="server"
Text='<%# Bind("ContactPhone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID="btnInsert" runat="server"
CommandName="Insert" Text="Insert" />
</FooterTemplate>
<FooterStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Guid"
SortExpression="Guid" Visible="False">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server"
Text='<%# Bind("Guid") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<
%# Bind("Guid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="Black" ForeColor="White" />
<AlternatingRowStyle BackColor="#FFE16D" />
</asp:GridView>
<asp:SqlDataSource ID="sqlContactInfo" runat="server"
ConnectionString="<%$
ConnectionStrings:Altiris_IncidentsConnectionString %>"
SelectCommand="usp_sp"
SelectCommandType="StoredProcedure"
UpdateCommand="update contactTable Contacts SET Name =
@ContactName, Email = @ContactEmail, Phone = @ContactPhone WHERE
(CAST(_ResourceGuid AS varchar(36)) = @Guid)"
DeleteCommand="delete from contactTable where Name =
@ContactName and Email = @ContactEmail and Phone = @ContactPhone and
(CAST(_ResourceGuid AS varchar(36)) = @Guid)" >
<SelectParameters>
<asp:QueryStringParameter Name="agreementGuid"
QueryStringField="agreementGuid" Type="String" />
</SelectParameters>
<DeleteParameters>
<asp:parameter Name="Contactame" Type="string" />
<asp:parameter Name="ContactEmail" Type="string" />
<asp:parameter Name="ContactPhone" Type="string" />
<asp:parameter Name="Guid" Type="string" />
</DeleteParameters>

</asp:SqlDataSource>
 
C

Cor Ligthert[MVP]

Daniele,

DeleteCommand="delete from contactTable where Name = @ContactName and
Email = @ContactEmail and Phone = @ContactPhone and
(CAST(_ResourceGuid AS varchar(36)) = @Guid)"

Where X=X is forever equal, have a look at your code, in my idea you oversee
something.

I would concatinate the paramters to one string in your place. You get
always errors doing it like that, but that is not the main problem.

Cor
 
M

murtaza gandhi

use the names as DeleteCommand="delete from contactTable where Name = @Name
and
Email = @Email and Phone = @Phone "

i mean map the names correctly to the field names.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top