locked
Problem with inserting to ProfileTable RRS feed

  • Question

  • User2114530758 posted

    Hi guys! I'm getting this error on DataSource.Insert(); The variable name '@Fornavn' has already been declared. Variable names must be unique within a query batch or stored procedure. Must declare the scalar variable "@Landsdel".

    I don't know how to solve it, it's driving me crazy! Can anyone see the problem?

    This is what my DataSource looks like:

    <

    asp:SqlDataSource ID="InsertExtraInfo" runat="server"

    ConnectionString="<%$ ConnectionStrings:AvrilwebConnectionstring %>"

    InsertCommand="INSERT INTO [ProfileTable] ([UserId], [Fornavn], [Efternavn], [Alder], [Landsdel], [imgURL]) VALUES (@UserId, @Fornavn, @Efternavn, @Alder, @Landsdel, @imgURL)"

    SelectCommand

    ="SELECT [UserId], [Fornavn], [Efternavn], [Alder], [Landsdel], [imgURL] FROM [ProfileTable]">

    <InsertParameters

    >

    <asp:ControlParameter Name="Fornavn" Type="String" ControlID="txtFornavn" PropertyName="Text"

    />

    <asp:ControlParameter Name="Efternavn" Type="String" ControlID="txtEfternavn" PropertyName="Text"

    />

    <asp:ControlParameter Name="imgURL" Type="String" ControlID="ImgURL" PropertyName="Text"

    />

    <asp:ControlParameter Name="Landsedel" Type="String" ControlID="txtLandsdel" PropertyName="Text"

    />

    <asp:ControlParameter Name="Alder" Type="DateTime" ControlID="txtAlder" PropertyName="Text"

    />

    </InsertParameters

    >

    </asp:SqlDataSource

    >

    C# CodeBehind:

    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
            {
    
                TextBox UserNameTextBox = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("UserName");
                SqlDataSource DataSource = (SqlDataSource)CreateUserWizardStep1.ContentTemplateContainer.FindControl("InsertExtraInfo");
    
                MembershipUser User = Membership.GetUser(UserNameTextBox.Text);
                object UserGUID = User.ProviderUserKey;
    
    
                DropDownList DropDownList1 = (DropDownList)CreateUserWizardStep1.ContentTemplateContainer.FindControl("DropDownList1");
                DropDownList DropDownList2 = (DropDownList)CreateUserWizardStep1.ContentTemplateContainer.FindControl("DropDownList2");
                DropDownList DropDownList3 = (DropDownList)CreateUserWizardStep1.ContentTemplateContainer.FindControl("DropDownList3");
    
                string day = DropDownList1.SelectedValue.ToString();
                string month = DropDownList2.SelectedValue.ToString();
                string year = DropDownList3.SelectedValue.ToString();
    
                DateTime birthdate = Convert.ToDateTime(day + "/" + month + "/" + year);
    
                TextBox txtFornavn = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("txtFornavn");
                TextBox ImgURL = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("ImgURL");
                TextBox txtEfternavn = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("txtEfternavn");
                DropDownList txtLandsdel = (DropDownList)CreateUserWizardStep1.ContentTemplateContainer.FindControl("txtLandsdel");
    
                DataSource.InsertParameters.Add("UserId", UserGUID.ToString());
                DataSource.InsertParameters.Add("Fornavn", txtFornavn.ToString());
                DataSource.InsertParameters.Add("Efternavn", txtEfternavn.ToString());
                DataSource.InsertParameters.Add("Alder", birthdate.ToString());
                DataSource.InsertParameters.Add("Landsdel", txtLandsdel.ToString());
                DataSource.InsertParameters.Add("imgURL", txtLandsdel.ToString());
                DataSource.Insert();
           }


     

    Wednesday, February 9, 2011 10:16 AM

Answers

  • User-1995538749 posted

    You already have your ControlParameters declaratively set-up for the Insert, and you are then trying to add those same parameters in your code-behind. You need to decide which of the two you want to keep. If you are doing all via code, then simply remove your ControlParmaters from the InsertParameters collection.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 9, 2011 10:25 AM
  • User-1995538749 posted

    Yeah, change from this:

    DataSource.InsertParameters.Add("Fornavn", txtFornavn.ToString()); 

    to this:

    DataSource.InsertParameters.Add("Fornavn", txtFornavn.Text); 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 9, 2011 10:48 AM

All replies

  • User-1995538749 posted

    You already have your ControlParameters declaratively set-up for the Insert, and you are then trying to add those same parameters in your code-behind. You need to decide which of the two you want to keep. If you are doing all via code, then simply remove your ControlParmaters from the InsertParameters collection.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 9, 2011 10:25 AM
  • User2114530758 posted

    Oh your right, thanks a lot! Now theres a different problem, instead of getting the text from the TextBox it inserts System.Web.UI.WebControls.TextBox to the database, can you see why?

    Wednesday, February 9, 2011 10:43 AM
  • User-1995538749 posted

    Yeah, change from this:

    DataSource.InsertParameters.Add("Fornavn", txtFornavn.ToString()); 

    to this:

    DataSource.InsertParameters.Add("Fornavn", txtFornavn.Text); 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 9, 2011 10:48 AM
  • User2114530758 posted

    Your awesome, thanks so much for your help! 

    Wednesday, February 9, 2011 10:57 AM