Question

I am using ASP.net 3.5 and i am getting this error.

Must declare the scalar variable "@Ad1".

What am i doing wrong?

My Submit button

Protected Sub btnUpdate_Click

    Try
        AddressSRC.Update()
        lblResult.Visible = True

    Catch ex As Exception

        lblResult.Visible = True
        lblResult.Text = ex.Message

    End Try

    LoadData()

End Sub

My ASPX page where my SQLDatasource is sitting - I have the parramators though.

      <asp:SqlDataSource ID="AddressSRC" runat="server" 
            ConnectionString="<%$ ConnectionStrings:SLConn %>" 
            ProviderName="<%$ ConnectionStrings:SLConn.ProviderName %>" 

            UpdateCommand="UPDATE ADDRESS 
                           SET Address1 = @Ad1, Address2 = @Ad2
                           WHERE entityID = 'CRB'
                           AND addressID = @AdID ">
            <UpdateParameters>        
                <asp:ControlParameter ControlID="txtPaddress1" Name="Ad1" PropertyName="Text"  Type="String"/>
                <asp:ControlParameter ControlID="txtPaddress2" Name="Ad2" PropertyName="Text" Type="String" />
                <asp:ControlParameter ControlID="txtAddressID" Name="AdID" PropertyName="Text" Type="String" />
            </UpdateParameters>


        </asp:SqlDataSource>
Was it helpful?

Solution 3

AI AI AI.........i was stupid.

I was suppose to place a IF NOT ISPOSTBACK statement on my PAGE LOAD event because i re-loaded my records from the DB when i clicked the Submit button and it updated the records with the old records!

OTHER TIPS

It looks ok to me. Can you breakpoint it and check what parameters are present just before the update statement? I can't see anything obvious wrong, so it could be a silly mistake elsewhere.

If this source is databound, make sure the databound control (i.e. GridView, ListView, etc) has their DataKeyNames property set. Something like:

GridView1.DataKeyNames = new string[]{"KeyFieldName"};

EDIT: If all else fails, you can manually add the parameters:

Protected Sub btnUpdate_Click

    Try
        AddressSRC.UpdateParameters.Clear();
        AddressSRC.UpdateParameters.Add("Ad1",textPaddress1.Text);
        AddressSRC.UpdateParameters.Add("Ad2",textPaddress2.Text);
        AddressSRC.UpdateParameters.Add("AdID",textAddressID.Text);
        AddressSRC.Update()
        lblResult.Visible = True

    Catch ex As Exception

        lblResult.Visible = True
        lblResult.Text = ex.Message

    End Try

    LoadData()

End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top