[RESOLVED]cannot update a table

Hello 

 question I have an asp.net page that has an sql data source control on it. for some reason it updating the table and yes I have included code saying update delete and insert is there a special way I have to do in order for it too work in visual studio 2012?
Have used this method before in visual studio 2010 and 2008 and it worked fine. 

Maybe you have not set DataKeyNames yet.

DataKeyNames = "ID"

You need this key, pass the value using update parameter

        <UpdateParameters>
            <asp:Parameter Name="ID" Type="Int32" />
        </UpdateParameters>

how would you implement this Im using visual basic asp.net as my code

 

It should work the same in VS 2012 as the previous versions.  Could you post your code, please?

what I got so far sql table in correct folder (App_Data) SQL datasource pointing said table an insert update and delete command. Just like I have done is vs 2010. What Im not getting is data being added to said table I’m using visual basic asp.net very puzzled
on why this is not working .    

Is your SQLDataSource passing in Parameters?  Are any of these parameters NULL?

hi,

if it works fine for you before,  now you maybe miss something.

you can refer the  next link:

Working with SqlDataSource Control in ASP.NET 3.5

http://www.c-sharpcorner.com/uploadfile/raj1979/working-with-sqldatasource-control-in-Asp-Net-3-5/

This my VB code once the button is clicked

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
If Page.IsValid Then
SqlDataSource1.InsertParameters("Name").DefaultValue = txtName.Text
SqlDataSource1.InsertParameters("Email").DefaultValue = txtEmail.Text
SqlDataSource1.InsertParameters("Comments").DefaultValue = txtComments.Text = ""

Try
SqlDataSource1.Insert()
txtName.Text = ""
txtEmail.Text = ""
txtComments.Text = ""

Catch ex As Exception
lblError.Text = "A database error has occured." & ex.Message

End Try

Response.Redirect("TY_Page.aspx")

End If

End Sub
End Class

this is the pages code:

<%@ Page Title="Guestbook" Language="vb" AutoEventWireup="false" MasterPageFile="~/Sand.Master" CodeBehind="GB.aspx.vb" Inherits="Sand.GB" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<style type="text/css">
.auto-style1 {
height: 36px;
}
.auto-style2 {
height: 37px;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<form id="Form1" runat="server">
<link href="Css/G_book.css" rel="stylesheet" type="text/css"/>

<asp:ScriptManager ID="sm1" runat="server">
<Scripts>
<asp:ScriptReference Name="jquery" />
</Scripts>
</asp:ScriptManager>
<table class="words">
<tr>
<td colspan="3" class ="style6">This is an example of an asp.net page. Signing of the guest book would greatly appreciated.</td>
</tr>
<tr>
<td rowspan="5" class ="style1">
&nbsp;</td>
<td class="style2">
<asp:Label ID="lblName" runat="server" Text="Name:"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtName" runat="server" Class ="style3"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="name" runat="server" ControlToValidate="txtName" CssClass="style4" ErrorMessage="Name Required" ValidationGroup="Error">*</asp:RequiredFieldValidator>
</td>

</tr>
<tr>
<td class="style2">
<asp:Label ID="lblEmail" runat="server" Text="Email:"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEmail" runat="server" Class ="style3"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="EmailValidator" runat="server" ControlToValidate="txtEmail" CssClass="style4" ErrorMessage="Email Required" ValidationGroup="Error">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="InvailEmailValidator" runat="server" ControlToValidate="txtEmail" CssClass="style4" ErrorMessage="Invalid Email Format" ValidationExpression="w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*" ValidationGroup="Error">*</asp:RegularExpressionValidator>
</td>
<td>&nbsp;</td>

</tr>
<tr>
<td class="style2" >
<asp:Label ID="lblComments" runat="server" Text="Comments:"></asp:Label>
</td>
<td rowspan="2">
<asp:TextBox ID="txtComments" runat="server" class ="style3" TextMode =" MultiLine"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="CommentValidator" runat="server" ControlToValidate="txtComments" CssClass="style4" ErrorMessage="Comment Required" ValidationGroup="Error">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style2"></td>
</tr>
</table>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="style7" ValidationGroup="Error" />
<table>
<tr>

<td class="auto-style1"><asp:Label ID="lblError" runat="server" class ="style8" Text=""></asp:Label>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" CssClass="style4" HeaderText="Please fix these errors" ValidationGroup="Error" />

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:TY_GuestConnectionString %>" DeleteCommand="DELETE FROM [Table] WHERE [Id] = @original_Id AND [Name] = @original_Name AND [Email]
= @original_Email AND [Comments] = @original_Comments" InsertCommand="INSERT INTO [Table] ([Id], [Name], [Email], [Comments]) VALUES (@Id, @Name, @Email, @Comments)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [Id], [Name], [Email],
[Comments] FROM [Table]" UpdateCommand="UPDATE [Table] SET [Name] = @Name, [Email] = @Email, [Comments] = @Comments WHERE [Id] = @original_Id AND [Name] = @original_Name AND [Email] = @original_Email AND [Comments] = @original_Comments">
<DeleteParameters>
<asp:Parameter Name="original_Id" Type="Int32" />
<asp:Parameter Name="original_Name" Type="String" />
<asp:Parameter Name="original_Email" Type="String" />
<asp:Parameter Name="original_Comments" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Id" Type="Int32" />
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="Comments" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="Comments" Type="String" />
<asp:Parameter Name="original_Id" Type="Int32" />
<asp:Parameter Name="original_Name" Type="String" />
<asp:Parameter Name="original_Email" Type="String" />
<asp:Parameter Name="original_Comments" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>

</td>
</tr>

</table>
</form></asp:Content>

I do not see your id parameter being set when you update. If it is an identity field, try removing this parameter. Try commenting out the TRY…CATCH block to see if it throws an error. Also, if a parameter is null the insert will cancel and not execute.

What is strange about the problem is this is the exact code I used in Visual Studio 2010 and it worked there. It is just not working in Visual Studio 2012

OK figured it out I needed to set the increment in the table . thank you all for pointing me in the right direction. 

Leave a Reply