Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
System.ArgumentException: No mapping exists from object type System.Windows.Forms.ListBox+ObjectCollection to a known managed provider native type.

namespace Finalnew
{
    public partial class Form1 : Form
    {
        private SqlConnection con;
        private SqlCommand command;
        private SqlDataAdapter adapter;
        private DataSet dataset;

        public Form1()
        {
            InitializeComponent();

            con = new SqlConnection();
            command = con.CreateCommand();
            con.ConnectionString = "Data Source=CASSINI-003-PC;Initial Catalog=studentdb;Integrated Security=True";
            adapter = new SqlDataAdapter(command);
            dataset = new DataSet();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            command.Parameters.Clear();
            command.Parameters.AddWithValue("@StudentID", textBox1.Text);
            command.Parameters.AddWithValue("@Name", textBox2.Text);
            command.Parameters.AddWithValue("@Age", textBox3.Text);
            command.Parameters.AddWithValue("@Gender", textBox4.Text);
            command.Parameters.AddWithValue("@Courseno",listBox1.Items);
            command.CommandText = "INSERT into details" + "(StudentID,Name,Age,Gender,Courseno)VALUES" + "(@StudentID,@Name,@Age,@Gender,@Courseno)";

            try
            {
                con.Open();

                int result = command.ExecuteNonQuery();

                if (result > 0)
                    MessageBox.Show("student successfully updated");
                else
                    MessageBox.Show("failed to update");
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }

            ClearFields();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            command.Parameters.Clear();
            command.Parameters.AddWithValue("@StudentID", textBox1.Text);
            command.CommandText = "SELECT * FROM details WHERE StudentID=@StudentID";


            dataset.Tables.Clear();

            int result = adapter.Fill(dataset, "details");

            if (result > 0)
            {
                DataRow srow = dataset.Tables["details"].Rows[0];
                textBox1.Text = srow["StudentID"].ToString();
                textBox2.Text = srow["Name"].ToString();
                textBox3.Text = srow["Age"].ToString();
                textBox4.Text = srow["Gender"].ToString();
                listBox1.SelectedItem = srow["Courseno"].ToString();
            }
            else
            {
                MessageBox.Show("Student does not exist");
            }
        }

        void ClearFields()
        {
            textBox1.Text = String.Empty;
            textBox2.Text = String.Empty;
            textBox3.Text = String.Empty;
            textBox4.Text = String.Empty;
            //textBox5.Text = String.Empty;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string currentitem = listBox1.SelectedItem.ToString();


        }
    }
}
Posted

command.Parameters.AddWithValue("@Courseno",listBox1.Items);
Items[^] is a collection property, you can not pass it as parameter! You probably need SelectedValue[^].
 
Share this answer
 
v3
Comments
codingisok101 24-Aug-12 3:21am    
how do i pass the course no as a parameter in this case?
Zoltán Zörgő 24-Aug-12 3:22am    
listBox1.SelectedValue, but this will work only if values are also provided to the listbox. It depend where your listbox items are coming from.
codingisok101 24-Aug-12 3:37am    
my listbox items are selected by an end user and those values are updated in the database.
Zoltán Zörgő 24-Aug-12 4:17am    
I imagined this, but how is the listbox populated?
Oshtri Deka 24-Aug-12 3:24am    
5.
just got it! thanks for the effort!
 
Share this answer
 
Comments
BillW33 24-Aug-12 8:50am    
You should not post a comment to a solution or an addition to your question as a "Solution". Either add a comment to a previous solution by pressing the "Have a Question or Comment?" button or update your question by using the "Improve question" button. I did not vote, but I thought you should know why this is being downvoted.

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