错误:从对象类型System.Data.DataRowView到已知的托管提供程序本机类型不存在映射

时间:2013-09-21 06:01:23

标签: c#

嗨, 它显示了这样的错误......
错误: - 从对象类型System.Data.DataRowView到已知的托管提供程序本机类型不存在映射。我是错误部分的基础............. !!

    private void button1_Click(object sender, EventArgs e)
    {
        String connectionString = @"Connection String Here ";
        SqlConnection connection = new SqlConnection(connectionString);
        connection.Open();

        // INSERTION 
        string query = "INSERT INTO StudentMaster(RollNo,Name,Gender,FathersName,Course,Branch,Semester,Section,ContactNo1,ContactNo2,EmailId) VALUES ('" + textBox1.Text + "', '" + textBox2.Text + "', @Gender,'" + textBox3.Text + "', '" + comboBox1.Text + "','" + comboBox2.Text + "', '" + comboBox3.Text + "', '" + comboBox4.Text + "','" + textBox4.Text + "', '" + textBox5.Text + "', '" + textBox6.Text + "'";
        SqlCommand command = new SqlCommand(query, connection);

        // Radio button
        if (radioButton1.Checked)
        {
            command.Parameters.AddWithValue("@Gender", "Male");
        }
        else
        {
            command.Parameters.AddWithValue("@Gender", "Female");
        }

        // value store for combobox
        //command.Parameters.AddWithValue("@DOB", dateTimePicker1.Text);
        command.Parameters.AddWithValue("@Course", comboBox1.SelectedItem);
        command.Parameters.AddWithValue("@Branch", comboBox2.SelectedItem);
        command.Parameters.AddWithValue("@Semester", comboBox3.SelectedItem);
        command.Parameters.AddWithValue("@Section", comboBox4.SelectedItem);

        command.ExecuteNonQuery();
        connection.Close();

        if (MessageBox.Show("Do you really want to INSERT??", "Insert", MessageBoxButtons.OKCancel) == DialogResult.OK)
        {
            MessageBox.Show("INSERTED");
        }
    }

2 个答案:

答案 0 :(得分:1)

SelectedValue是类的一个对象,用作DataRow的DataRowView视图,应该以相同的方式使用,即: DataRowView drv =(DataRowView)comboBox1.SelectedValue;

答案 1 :(得分:0)

我自己解决..... !!

command.Parameters.AddWithValue("@Course", comboBox1.Text); command.Parameters.AddWithValue("@Branch", comboBox2.Text); command.Parameters.AddWithValue("@Semester", comboBox3.Text); command.Parameters.AddWithValue("@Section", comboBox4.Text);

现在正在工作:D 我可以轻松插入数据...... !!!

  
    
      
        

如果你没有犯错,那么你什么都不做