Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
cmd.Parameters.AddWithValue("@FC", DDL.DataValueField);
but i get
C#
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();
Posted
Updated 13-May-16 21:43pm

1 solution

C#
cmd.Parameters.AddWithValue("@FC", DDL.DataValueField);

use
C#
cmd.Parameters.AddWithValue("@FC", DDL.SelectedValue);

DataValueField will gives you the string 'ID', it wont return the selected Id

if the above doesnt work, try DDL.SelectedItem.Value
 
Share this answer
 
Comments
Member 12502847 14-May-16 3:44am    
Thank you very much for your help. Have a great day!
Karthik_Mahalingam 14-May-16 3:46am    
welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900