Hello. I have to do a form to add meetings, I have userid saved as cookie and put in a label and i want to add contacts name to a dropdown list, select the contact name but retrieve de contactid to use it as foreign key.
I tried to add
cmd.Parameters.AddWithValue("@FC", DDL.DataValueField);
but i get
Conversion failed when converting the nvarchar value 'Id' to data type int.
Sorry for my bad english, hope you understand and thank you for your help!
What I have tried:
<asp:DropDownList ID="DDL" runat="server" DataSourceID="SqlDataSource2" DataTextField="nume" DataValueField="Id" OnSelectedIndexChanged="DDL_SelectedIndexChanged" OnTextChanged="DDL_Change">
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:BazadedateConnectionString1 %>" SelectCommand="SELECT [nume], [FKuserID], [Id] FROM [Contacte] WHERE ([FKuserID] = @FKuserID)">
<SelectParameters>
<asp:ControlParameter ControlID="user" Name="FKuserID" PropertyName="Text" Type="Int32" />
</SelectParameters>
protected void MetodaAdaugareIntalnire(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO Intalniri (Data, Ora, Locatia,FKcontactID,FKuserID) VALUES (@DT,@OR,@LO,@FC,@FU)", con);
cmd.Parameters.AddWithValue("@DT", txtData.Text);
cmd.Parameters.AddWithValue("@OR", txtOra.Text);
cmd.Parameters.AddWithValue("@LO", txtLocatia.Text);
cmd.Parameters.AddWithValue("@FC", DDL.DataValueField);
cmd.Parameters.AddWithValue("@FU", user.Text);
cmd.ExecuteNonQuery();
con.Close();