C# .NET - Combobox binding

Asked By saif
10-Feb-12 12:50 AM
Kindly Help me in  the following issue

I have two table .. 1>author master
                  c_code---- primary key   value  01
                  c_author                  K .Ghosh
               2> book master
                  c_code--- primary key              01
                  c_name                  c# .net
                 c_author    (foreign key)     Combox List


I need to show  K.Ghosh[01]  in combox of book master while clicking on drop down

and after retrieving that i need to save the data of book master...here i need to insert  01 insted inserting K.ghosh[01] in the database...



Now how i can perform
 
  Somesh Yadav replied to saif
10-Feb-12 01:08 AM
Binding to a C# ComboBox
It is possible to bind http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=1862 http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=86 to http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=1862 http://atakala.com/browser/Item.aspx?user_id=amos&key=c# http://atakala.com/browser/Item.aspx?user_id=amos&key=combobox such that the http://atakala.com/browser/Item.aspx?user_id=amos&key=user can http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=1848 the http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2106 as well as choose http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=1862 http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=1849 one from http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=1862 list.
http://atakala.com/browser/Item.aspx?user_id=amos&key=set DropDownType to DropDown to http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=1992 http://atakala.com/browser/Item.aspx?user_id=amos&key=user http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=1992.
Populate your http://atakala.com/browser/Item.aspx?user_id=amos&key=combobox by http://atakala.com/browser/Item.aspx?user_id=amos&key=binding your DataTable of choices to it (or else http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=62 http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2058s manually):

myComboBox.DataSource = myDataChoicesTable;
myComboBox.DisplayMember = "field_text";
myComboBox.ValueMember = "field_text";

Then, bind http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=1862 DataTable containing one http://atakala.com/browser/Item.aspx?user_id=amos&key=row of http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2058 to the http://atakala.com/browser/Item.aspx?user_id=amos&key=combobox:
myComboBox.DataBindings.Add("Text", myDataTable, "my_text");

An important note: Your myDataChoicesTable MUST contain as one of the options the http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2106 in myDataTable that you are initially http://atakala.com/browser/Item.aspx?user_id=amos&key=using. Otherwise, the my_text http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2106 will be displayed, but the .http://atakala.com/browser/Item.aspx?user_id=amos&key=text http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2106 will not be correct. You will need to modify the http://atakala.com/browser/Item.aspx?user_id=amos&key=sql that generates the myDataChoicesTable table and ensure the http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=1632 http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2106 is encluded. Don't know why this is necessary... seems like http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=1862 bug to me- probably http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=1862 'feature' or else we're not supposed to be doing this.

Now, when the Combo's http://atakala.com/browser/Item.aspx?user_id=amos&key=text is changed, the datasource needs to be updated manually, as it is not done by .http://atakala.com/browser/Item.aspx?user_id=amos&key=net. So, capture BOTH the TextChanged & SelectedValueChanged http://atakala.com/browser/Item.aspx?user_id=amos&key=events, and http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=62 something like:


http://atakala.com/browser/Item.aspx?user_id=amos&key=combobox cb = (ComboBox)sender;
http://atakala.com/browser/Item.aspx?user_id=amos&key=string s = cb.DataBindings[0].BindingMemberInfo.BindingField.ToString();
DataTable dTable = (DataTable)cb.DataBindings[0].DataSource;
dTable.Rows[0][s] = cb.Text;

Finally, to http://atakala.com/browser/Item.aspx?user_id=amos&key=get the http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2106 from the http://atakala.com/browser/Item.aspx?user_id=amos&key=combobox, just call myComboBox.Text. Note that the SelectedValue and SelectedText properties will not "work" in this case, as you are http://atakala.com/browser/Item.aspx?user_id=amos&key=binding to the http://atakala.com/browser/Item.aspx?user_id=amos&key=text http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2017, not those other properties!

If you need to do some processing when the http://atakala.com/browser/Item.aspx?user_id=amos&key=user http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=69s the http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2106 of the http://atakala.com/browser/Item.aspx?user_id=amos&key=combobox, you will need to put the action in BOTH the TextChanged and the SelectedValueChanged http://atakala.com/browser/Item.aspx?user_id=amos&key=events of the http://atakala.com/browser/Item.aspx?user_id=amos&key=combobox. This is because if the http://atakala.com/browser/Item.aspx?user_id=amos&key=user http://atakala.com/browser/Item.aspx?user_id=amos&key=types in http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=1862 http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=1849 http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2106, the first http://atakala.com/browser/Item.aspx?user_id=amos&key=event is fired. If they select http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=1862 http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=1849 http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2106, the latter is fired. (Alternatively, put your logic in the LostFocus http://atakala.com/browser/Item.aspx?user_id=amos&key=event, and just use the .http://atakala.com/browser/Item.aspx?user_id=amos&key=text http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2017 to http://atakala.com/browser/Item.aspx?user_id=amos&key=get the http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2106 that was changed. (Though this will be called every time the http://atakala.com/browser/Item.aspx?user_id=amos&key=user tabs through, even if they didn't http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=69 the http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2106).

Lastly, in some rare cases, I needed to http://atakala.com/browser/Item.aspx?user_id=amos&key=set BOTH the .http://atakala.com/browser/Item.aspx?user_id=amos&key=text http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2017 and .SelectedText http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2017 when http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=91 the http://atakala.com/browser/Item.aspx?user_id=amos&key=text automatically from http://atakala.com/browser/Item.aspx?user_id=amos&key=code to http://atakala.com/browser/Item.aspx?user_id=amos&key=get it to keep its http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2106.

More http://atakala.com/browser/Item.aspx?user_id=amos&key=problems: When the FIRST http://atakala.com/browser/Item.aspx?user_id=amos&key=key is typed into the http://atakala.com/browser/Item.aspx?user_id=amos&key=combobox, the TextChanged http://atakala.com/browser/Item.aspx?user_id=amos&key=event http://atakala.com/browser/Item.aspx?user_id=amos&key=fires, but the .http://atakala.com/browser/Item.aspx?user_id=amos&key=text http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2017 is NOT updated (http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2066s old http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2106). http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=1858 would be to capture the Leave http://atakala.com/browser/Item.aspx?user_id=amos&key=event and double check your http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2058 if you are doing processing on TextChanged.
  Sreekumar P replied to saif
10-Feb-12 01:40 AM
Hi,

This is a simple one.
That is u just have to bind the Code and AuthorName to the Combo box.

Here is the code for u

public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    SqlConnection myConnection = new SqlConnection("user id=username;" +
                         "password=password;server=serverurl;" +
                         "Trusted_Connection=yes;" +
                         "database=database; " +
                         "connection timeout=30");
 
    private void Form1_Load(object sender, EventArgs e)
    {
      BindAuthors();
    }
    void BindAuthors()
    {
      try
      {
        myConnection.Open();
      }
      catch (Exception e)
      {
        Console.WriteLine(e.ToString());
      }
 
      SqlCommand myCommand = new SqlCommand("SELECT * FROM [author_master]", myConnection);
      SqlDataAdapter ta = new SqlDataAdapter(myCommand);
      DataTable dt = new DataTable();
      ta.Fill(dt);
      cmbAuthor.ValueMember = "c_code";
      cmbAuthor.DisplayMember = "c_author";
      cmbAuthor.DataSource = dt;
 
      myConnection.Close();
    }
 
    private void btnSave_Click(object sender, EventArgs e)
    {
 
      try
      {
        myConnection.Open();
      }
      catch (Exception ex)
      {
        Console.WriteLine(e.ToString());
      }
 
      SqlCommand myCommand = new SqlCommand("INSERT INTO [book_master] (c_name,c_author) VALUES ('" + txtBookName.Text + "','" + cmbAuthor.SelectedValue + "')", myConnection);
      myCommand.ExecuteNonQuery();
      myConnection.Close();
    }
 
  }
  dipa ahuja replied to saif
10-Feb-12 06:05 AM
Use nested combobox , 1st combo bind on form load and second bind on selectedIndexChanged event of 1st combo

private void Form1_Load(object sender, System.EventArgs e)
{
  string conn = "ConnectionString";
  SqlDataAdapter da = new SqlDataAdapter("Select * from tb1", conn);
 
  DataSet ds = new DataSet();
 
  da.Fill(ds);
  comboBox1.DataSource = ds.Tables[0];
  comboBox1.DisplayMember = "id";
  comboBox1.ValueMember = "id";
   
}
 
private void ComboBox1_SelectedIndexChanged(System.Object sender, System.EventArgs e)
{   
  string a = comboBox1.SelectedValue.ToString();
  string conn = "ConnectionString";
  SqlDataAdapter da = new SqlDataAdapter("select * from tb1 where id=" + a, conn);
 
  DataSet ds = new DataSet();
  da.Fill(ds);
 
  comboBox2.DataSource = ds.Tables[0];
  comboBox2.DisplayMember = "name";
  comboBox2.ValueMember = "name";
   
}
Create New Account
help
near ' = '. Hi!Help fixing this error<< Incorrect syntax near ' = '. > > It is pointin to the sqlcomm.ExecuteNonQuery(); This is the code I am using: string sconStr = System.Configuration.ConfigurationManager.ConnectionStrings["EticketingconnectionString"].ToString(); SqlConnection sqlconnection = new SqlConnection(sconStr); sqlconnection.Close(); sqlconnection.Open(); string u = "select username, password from dbo.Registration where username = '" + UsernameTextBox.Text + "'&password = '"+PasswordTextBox Text+"'"; SqlCommand sqlcomm = new SqlCommand(u, sqlconnection); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = sqlcomm; sqlcomm.CommandType = CommandType.Text; sqlcomm.ExecuteNonQuery(); DataTable dt = new
sender, EventArgs e) { if (!IsPostBack) { BindData(); } } private void BindData() { string connStr = ConfigurationManager.ConnectionStrings["MyDbConn1"].ToString(); SqlConnection conn = new SqlConnection(connStr); SqlDataAdapter ad = new SqlDataAdapter("select * from [mnames]", conn); DataSet dSet = new DataSet(); ad.Fill(dSet, "PagesData"); datagrid1.DataSource = dSet datagrid1.DataBind(); } protected void Button1_Click(object sender, EventArgs e) { string connStr = ConfigurationManager.ConnectionStrings["MyConn77"].ToString(); SqlConnection conn = new SqlConnection(connStr); string query = "INSERT INTO [tasssk] (name, gender, qualification) VALUES('" + name1.Text + "', '" + gender.ToString() + "''" + qualification1.ToString() + "')"; SqlCommand dCmd = new SqlCommand(query, conn); conn.Open(); SqlDataAdapter da = new SqlDataAdapter(dCmd); DataSet dt = new DataSet (); da.Fill(dt); dCmd.CommandText = "insert_task"; dCmd.CommandType = CommandType.StoredProcedure
dtTable public static DataTable dtTable; / / Get the connectionstring from the webconfig and declare a global SqlConnection "SqlConnection" public static string connectionString = ConfigurationManager.AppSettings["ConnectionString"]; public SqlConnection SqlConnection = new SqlConnection(connectionString); / / Declare a global SqlDataAdapter SqlDataAdapter public SqlDataAdapter SqlDataAdapter = new SqlDataAdapter(); / / Declare a global SqlCommand SqlCommand public SqlCommand SqlCommand = new SqlCommand(); protected void
in gridview protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { string connStr = ConfigurationManager.ConnectionStrings["MyConn77"].ToString(); SqlConnection conn = new SqlConnection(connStr); SqlDataAdapter ad = new SqlDataAdapter("select name from [form] where autoid = @autoid" , conn); DataTable dt = new DataTable (); ad.Fill(dt the code protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { string connStr = ConfigurationManager.ConnectionStrings["MyConn77"].ToString(); SqlConnection conn = new SqlConnection(connStr); SqlDataAdapter ad = new SqlDataAdapter("select * from [form] where autoid = @autoid" , conn); DataTable dt = new DataTable (); ad.Fill(dt); gridview1
can yu plz modify diz code if (!IsPostBack) { BindData(); string connStr = ConfigurationManager.ConnectionStrings["MyConn77"].ToString(); SqlConnection conn = new SqlConnection(connStr); SqlDataAdapter ad = new SqlDataAdapter("select * from [form]", conn); SqlDataAdapter da = new SqlDataAdapter("select autoid from [form]", conn); DataSet dSet = new DataSet(); ad.Fill(dSet, "PagesData"); GridView1.DataSource load : protected void Page_Load( object sender, EventArgs e) { if (!IsPostBack) { autoid.Text = getId(); } } string getId() { SqlConnection sqlconn = new SqlConnection( "Your Connection string" ); string value = "" ; try { if (sqlconn.State = = ConnectionState.Closed) { sqlconn.Open(); } string Query