I have basically a combobox and a text box.
The combobox consists of subject code, whereas the textbox consists of a subject name which is stored in the database.
Is there any way that when an item from combobox is selected, it automatically displays the subject name based on the selected subject code, which will be from the database?
Below is my code, but it is not working.
private void Form1_Load(object sender, EventArgs e)
{
con.Open();
OleDbDataAdapter oda1 = new OleDbDataAdapter("select subject_code from subjectinfo", con);
DataTable dt1 = new DataTable();
oda1.Fill(dt1);
comboBoxSubjectCodeUpdate.DataSource = dt1;
comboBoxSubjectCodeUpdate.DisplayMember = "subject_code";
comboBoxSubjectCodeUpdate.SelectedIndex = -1;
con.Close();
}
private void comboBoxSubjectCodeUpdate_SelectedIndexChanged(object sender, EventArgs e)
{
string str = "select subject_abbreviation from subjectinfo where subject_code ='" + comboBoxSubjectCodeUpdate.Text + "'";
OleDbCommand cmd = new OleDbCommand(str, con);
OleDbDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
textBox1.Text = dr["subject_abbreviation"].ToString();//column name should be that you want to show on textbox
}
}
Related
private void Purchases_Load(object sender, EventArgs e)
{
com.Connection = con;
con.Open();
com.CommandText = "select * from Items";
com.ExecuteNonQuery();
DataTable dt7 = new DataTable();
SqlDataAdapter da7 = new SqlDataAdapter(com);
da7.Fill(dt7);
ComboColumn.DataSource = dt7;
ComboColumn.DisplayMember = "Name";
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
com.Connection = con;
com.CommandText = "select ID from Items where Name = '" + ComboColumn + "' ";
con.Open();
com.ExecuteNonQuery();
SqlDataReader dr;
dr = com.ExecuteReader();
while (dr.Read())
{
string id = (string)dr["ID"].ToString();
}
con.Close();
}
I have sql db table contains ID and Items.
also, I have winform and I'm using DataGrid type "DataGridViewComboBoxColumn" to select the items from db table and it's working perfectly
now, The problem is I need to show the following "ID column" to the second column in DGV while I selecting the items from "DataGridViewComboBoxColumn".
So, I need help to link a specific column in database which is (ID) with a specific column on datagrid column type "DataGridViewTextBoxColumn" when user select items from the gdv ComboBoxColumn.
note: 1st scop of code working and giving me items from db.
2nd scop of code not working and I can't handle it for retrieve ID from db table!!
The answer is here!
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewComboBoxCell comboBoxCell = (row.Cells[2] as DataGridViewComboBoxCell);
comboBoxCell.Items.Add("Select Goods");
comboBoxCell.Value = "Select Goods";
DataTable dt_1 = this.GetData("SELECT ID,Name from Goods");
foreach (DataRow drow in dt_1.Rows)
{
string customerId = drow[0].ToString();
comboBoxCell.Items.Add(drow[0] + "-" + drow[1]);
if (customerId != "3")
{
if (row.Cells[2].Value.ToString() == customerId)
{
comboBoxCell.Value = drow[1];
}
}
}
I want to filter listbox data by using datagridview cell instead of using textbox.I am using the following code.
private void searchLedger(string value)
{
cn.Open();
SqlCommand cmd = new SqlCommand("Select * from LedgerCreation Where ledgerName like #LedgerName and AddClassification = #AddClassification order by LedgerName", cn);
cmd.Parameters.AddWithValue("#AddClassification", "Cash/Bank");
cmd.Parameters.AddWithValue("#LedgerName", "%" + value + "%");
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable table = new DataTable();
da.Fill(table);
LstReceipt.DataSource = table;
LstReceipt.DisplayMember = "LedgerName";
cn.Close();
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 1)
{
searchLedger(dataGridView1.CurrentRow.Cells[0].FormattedValue.ToString());
}
}
I have combobox in my form which display value from database. I want one default value so I use .text property also and on selected index changed event the label display the corresponding value of the database the code is not working
string cstr = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
private void AddStock_Load(object sender, EventArgs e)
{
bindcombo();
}
private void bindcombo()
{
SqlConnection con = new SqlConnection(cstr);
SqlDataAdapter da = new SqlDataAdapter("SELECT Dname FROM dealer", con);
DataTable dt = new DataTable();
da.Fill(dt);
cmbdealer.DataSource = dt;
cmbdealer.DisplayMember = "Dname";
cmbdealer.Text = "select-Dealer";
con.Close();
}
private void cmbdealer_SelectedIndexChanged(object sender, EventArgs e)
{
dealerdetails();
}
private void dealerdetails()
{
SqlConnection con = new SqlConnection(cstr);
SqlCommand cmd = new SqlCommand("select * from dealer where Dname='" + cmbdealer.Text + "'", con);
con.Open();
SqlDataReader re = cmd.ExecuteReader();
while (re.Read())
{
lbdl.Text = re["Ddlno"].ToString();
lbgst.Text = re["Dgstno"].ToString();
lbadd.Text = re["Daddress"].ToString();
}
}
the above code is working fine but when the form is load all the 3 label shows the database value of the first entry of my dealer table without selecting and value from combobox.
I try to develop a web based survey creation and filling app on ASP.NET and C# getting data from Sql Server. I use DataBind to bind my question names and answers. Below in the picture, I need to show only the question number (like 1, 2, 3 etc.) in RadioButtonList (not with the question name) and only the question name in a label lblQuestion as;
protected void rbtnQuestions_SelectedIndexChanged(object sender, EventArgs e)
{
General.myquestionid = System.Convert.ToInt32(rbtnQuestions.SelectedValue);
lblQuestion.Text = rbtnQuestions.SelectedItem.Text;
}
I also need the "id" of survey_question table to display the answers below the question since I use it in SelectedIndexChanged event.
But since I use DataTextField, both RadioButtonList text and lblQuestion.Text show the same thing. My way of binding the data is below:
string sqlStr = "SELECT id, question_no, question, survey_answer_type_id FROM survey_question WHERE survey_id = '" + General.mysurveyid + "'";
cmd = new SqlCommand(sqlStr, connection);
da.SelectCommand = cmd;
da.Fill(data);
rbtnQuestions.DataSource = data;
rbtnQuestions.DataTextField = "question";
rbtnQuestions.DataValueField = "id";
rbtnQuestions.DataBind();
cmd.Dispose();
How can I achieve this?
Thanks.
On Page load event Set
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string sqlStr = "SELECT id,question_no, CONVERT(varchar(10),(ROW_NUMBER() over (order by question desc))) AS QueNo,question, survey_answer_type_id FROM survey_question WHERE survey_id = '" + General.mysurveyid + "'";
cmd = new SqlCommand(sqlStr, connection);
da.SelectCommand = cmd;
da.Fill(data);
ViewState["data"] = data;
rbtnQuestions.DataSource = data;
rbtnQuestions.DataTextField = "QueNo";
rbtnQuestions.DataValueField = "id";
rbtnQuestions.DataBind();
cmd.Dispose();
}
}
And Question Set On SelectedIndexChanged Event
protected void rbtnQuestions_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet ds = (DataSet)ViewState["data"];
DataRow[] row = ds.Tables[0].Select("id = '" + rbtnQuestions.SelectedValue + "'");
lblQuestion.Text = row[0]["question"].ToString();
}
using query i have called two columns value from database into one column. the point is now i want to select a value form combobox and put one column value into textbox.
e.g
two column values from database into combobox below
10001 haider <------ when i select this index i want only haider to be viewed into the textbox
10002 fahad
10003 aitazaz
the snippet which i have used for calling the two colums value from database is:
public void account()
{
con.Open();
cmd.Connection = con;
cmd.CommandText = "SELECT acc_no, acc_name FROM accounts_record";
MySqlDataAdapter adpt = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
adpt.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
cbacc.Items.Add(ds.Tables[0].Rows[i][0] + " " + ds.Tables[0].Rows[i][1]);
}
con.Close();
}
You should be adding values and text to the combobox separately.
Here's an example ComboBox: Adding Text and Value to an Item (no Binding Source).
If you have to display the id in the text you have to do some parsing before putting the selected text into the textbox.
Use ComboBox.SelectedIndexChanged event of the combobox to populate the textbox accordingly. use http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged(v=vs.110).aspx
If you are able to get the 2 value text of the combo box on selection change then you can split it with space (" ") character and take the 2nd string and put it in textbox.
For example
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string[] splitedStr = comboBox1.SelectedText.Split(' ');
textBox1.Text = splitedStr[1];
}
You should use the code as below
private void Form1_Load(object sender, EventArgs e)
{
string conString = "Data Source=\\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True";
SqlConnection con = new SqlConnection(conString);
SqlCommand cmd = new SqlCommand();
con.Open();
cmd.Connection = con;
cmd.CommandText = "SELECT acc_no +'-' + acc_name as AccNoWithName , acc_no as ActNo FROM accounts_record";
SqlDataAdapter adpt = new SqlDataAdapter(cmd);
DataSet dsn = new DataSet();
adpt.Fill(dsn);
con.Close();
comboBox1.DisplayMember = "AccNoWithName";
comboBox1.ValueMember = "ActNo";
comboBox1.DataSource = dsn.Tables[0];
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = comboBox1.SelectedValue.ToString();
}