Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HELLO Guys, i'm totally new to this_
searched a lot in internet_ but have'nt got the statisfied answer_

actually m trying to send the selected item of comboBox To SQL DB using sql query_
but hv'nt got the success yet_
Here is my code_

C#
private void button1_Click(object sender, EventArgs e)
        {


            string SQL = "INSERT INTO bca(course,scheme,subcode,subname)VALUES('" + comboBox1.SelectedItem.ToString() + "','" + comboBox2.SelectedItem.ToString() + "','" + textBox1.Text + "','" + textBox2.Text + "')";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = SQL;
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();

            MessageBox.Show("success");
        }


The texBox data's are saving exactly in the DB, but the comboBox values are saving like System.Data.DataRowView

plzzzz help me to overcome from this__

THanK UUUU
Posted
Comments
Nandakishore G N 11-Mar-13 0:23am    
how are you popualting combobox? the problem lies there while populating..paste the code..
abhijit hbk 12-Mar-13 2:57am    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;

namespace My_Work
{
public partial class Form1 : Form
{
SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\gcis\Documents\Visual Studio 2008\Projects\My Work\My Work\Database1.mdf;Integrated Security=True;User Instance=True");


public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
fillcombo();
fillcombo2();
}
private void fillcombo()
{
cn.Open();

SqlDataAdapter d = new SqlDataAdapter("select course from bca", cn);

DataSet dt = new DataSet();

d.Fill(dt);

comboBox1.DataSource = dt.Tables[0]; /// assing the first table of dataset

comboBox1.DisplayMember = "course";
cn.Close();
}

private void fillcombo2()
{
cn.Open();

SqlDataAdapter d = new SqlDataAdapter("select scheme from bca", cn);

DataSet dt = new DataSet();

d.Fill(dt);

comboBox2.DataSource = dt.Tables[0];

comboBox2.DisplayMember = "scheme";
cn.Close();

}

private void button1_Click(object sender, EventArgs e)
{


string SQL = "INSERT INTO bca(course,scheme,subcode,subname)VALUES('" + comboBox1.SelectedText.ToString() + "','" + comboBox2.SelectedText.ToString() + "','" + textBox1.Text + "','" + textBox2.Text + "')";
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = SQL;
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();

MessageBox.Show("success");
}




by using a private function i use to load the data of DB to combobox_
and with that i have to send textBox data also to a new DB_

here, THE actual problem is_ the selected data of combBox is not saving to a DB while the textBox data's are saving,while the comboBox values are saving like System.Data.DataRowView

1 solution

Maybe you should use SelectedText instead of using SelectedItem, or if you are using SelectedItem you should get the right property and its value to save to database.
At the end, who knows what type of object is SelectedItem, right. :)

Happy coding.
 
Share this answer
 
Comments
abhijit hbk 12-Mar-13 3:12am    
ThankS for yor regards_ ,sorry but it didn't worked_
I used SelectedText instead of SelectedItem,the empty data's are saving to the DB_

Here is the Brief description_
After selecting an item in the course(course,just the name of the comboBox) combobox by a click, after that i'l add subjectCode in the textBox. After all i'l save the selected item of comboBox data and textBox data in the new DB by a button click_
plzz Help me_
above i pasted my whole code_
Sharp Robin 12-Mar-13 10:55am    
Try comboBox.Text instead of comboBox.SelectedText. Peace.
Member 11478531 28-Feb-15 6:19am    
Awesome .. thanks i was facing the same problem
Member 13191952 18-Dec-17 11:15am    
your solution is very good
abhijit hbk 12-Mar-13 13:54pm    
thanks a lot_ i have got it_ :)

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