Hi,
I am designing a web form and one of its pages is divided into two parts. The first section is stored in the data base and returns to me a number that I store in a session to use later by adding new data to the same record..and when writing the code and executing the program, the following message appears:
(System.ArgumentException: 'No mapping exists from object type System.Web.UI.WebControls.ListItem to a known managed provider native type.'
)
protected void AddVisitor_Click(object sender, EventArgs e)
{
using (SqlConnection connection = new SqlConnection(connoctionString))
{
using (SqlCommand command = new SqlCommand("AddVisitor", connection))
{
command.CommandType = CommandType.StoredProcedure;
connection.Open();
//command.Parameters["@VisitID"].Direction = ParameterDirection.Output;
command.Parameters.AddWithValue("@VisitID", (string)Session["VisitID"]);
command.Parameters.AddWithValue("@Visitor_name", VisitorName.Text);
command.Parameters.AddWithValue("@Nationality_ID", NationalDD.SelectedItem);
command.Parameters.AddWithValue("@org_id", Org.SelectedValue);
command.ExecuteNonQuery();
connection.Close();
can any one help me?

Sachin SinghRank #66 · 26.2K reputationPosted Jan 18, 2023, 5:58 AM EST
use below parameters
command.Parameters.AddWithValue("@VisitID", Convert.ToInt32(Session["VisitID"])); command.Parameters.AddWithValue("@Visitor_name", VisitorName.Text); command.Parameters.AddWithValue("@Nationality_ID", Convert.Toint32(NationalDD.SelectedValue)); command.Parameters.AddWithValue("@org_id", Convert.Toint32(Org.SelectedValue));Make sure, VisitID, Nationality_ID and Org_Id are if int type in your table, Otherwise type cast accordingly.
Jignesh KumarRank #91 · 22.9K reputationPosted Jan 18, 2023, 3:22 AM EST
Hello,
You are passing dropdown.SelectedItem, you should pass selectedItem.Value
using (SqlConnection connection = new SqlConnection(connoctionString)) { using (SqlCommand command = new SqlCommand("AddVisitor", connection)) { command.CommandType = CommandType.StoredProcedure; connection.Open(); //command.Parameters["@VisitID"].Direction = ParameterDirection.Output; command.Parameters.AddWithValue("@VisitID", Convert.Tostring(Session["VisitID"])); command.Parameters.AddWithValue("@Visitor_name", VisitorName.Text); command.Parameters.AddWithValue("@Nationality_ID", NationalDD.SelectedItem.Value); command.Parameters.AddWithValue("@org_id", Org.SelectedItem.Value); command.ExecuteNonQuery(); connection.Close();Name Mame164 reputationPosted Jan 18, 2023, 3:48 AM EST
Thanks sir
when running the code after the changing , it give me this error
(System.Data.SqlClient.SqlException: 'Error converting data type nvarchar to int.'
) for the (command.ExecuteNonQuery();)
Jignesh KumarRank #91 · 22.9K reputationPosted Jan 18, 2023, 4:04 AM EST
change your session to Convert.ToInt() then it should work
command.Parameters.AddWithValue("@VisitID", Convert.ToInt32(Session["VisitID"]));Rijwan AnsariRank #82 · 24.2K reputationPosted Jan 18, 2023, 6:10 AM EST
Hi,
Can you please share your code using session or session conversion to list?
Name Mame164 reputationPosted Jan 18, 2023, 6:21 AM EST
Many thanks for your help ...
it's work :) ...
Now , in 2nd part ,i wrote this code which i bring the session Visit_ID to this part
using (SqlCommand command1 = new SqlCommand("strtVisit", con))
{
command1.CommandType = CommandType.StoredProcedure;
con.Open();
command1.Parameters.AddWithValue("@VisitID", Convert.ToInt32(Session["VisitID"]));
command1.Parameters.AddWithValue("@Visitor_name", VisitorName.Text);
command1.Parameters.AddWithValue("@Nationality_ID", NationalDD.SelectedIndex);
command1.Parameters.AddWithValue("@org_id", Org.SelectedItem.Value);
command1.ExecuteNonQuery();
con.Close();
}
and the message is appear
{Procedure or function StrtVisit expects parameter @Visit_ID, which was not supplied.} System.Exception {System.Data.SqlClient.SqlException}
and the save not complete :(
Note: " i need procedure which save a list of names and their details and then store it in the database after it split "
Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 18, 2023, 6:38 AM EST
Hello Name,
Your procedure parameter is @Visit_ID and you are passing @VisitID. So change
command1.Parameters.AddWithValue("@VisitID", Convert.ToInt32(Session["VisitID"]));To
command1.Parameters.AddWithValue("@Visit_ID", Convert.ToInt32(Session["VisitID"]));Thanks
Naimish Makwana
Jignesh KumarRank #91 · 22.9K reputationPosted Jan 18, 2023, 7:34 AM EST
You need to change just line with "@VisitID" to "@Visit_ID"
and your filnal code should look like,
using (SqlCommand command1 = new SqlCommand("strtVisit", con)) { command1.CommandType = CommandType.StoredProcedure; con.Open(); command1.Parameters.AddWithValue("@Visit_ID", Convert.ToInt32(Session["VisitID"])); command1.Parameters.AddWithValue("@Visitor_name", VisitorName.Text); command1.Parameters.AddWithValue("@Nationality_ID", NationalDD.SelectedIndex); command1.Parameters.AddWithValue("@org_id", Org.SelectedItem.Value); command1.ExecuteNonQuery(); con.Close(); }Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 18, 2023, 4:15 AM EST
Hello Name,
The problem may be
command.Parameters.AddWithValue("@Nationality_ID", NationalDD.SelectedItem);Try one of the below.
command.Parameters.AddWithValue("@Nationality_ID", NationalDD.SelectedIndex); OR command.Parameters.AddWithValue("@Nationality_ID", NationalDD.SelectedValue); OR command.Parameters.AddWithValue("@Nationality_ID", NationalDD.SelectedItem.ToString());Thanks.