views:

262

answers:

3

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>
A: 

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
Matthew Jones
DataSource is not databound to any control
Etienne
A: 

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.

Steven Robbins
Per my understanding you cant put a break point in with a SQLdatasource
Etienne
Really? You can't put a breakpoint at the start of your btnUpdate_Click method?
Steven Robbins
I can, but what will that show me? How will i get to see what is passed through a SQLDataSource?
Etienne
Seriously?! It will let you see what is in the UpdateParameters collection just before it's executed for starters!
Steven Robbins
A: 

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!

Etienne