Using C# & MySQL
When i select a particular value from the combobox then i clicked the button, the corresponding value should display in the other textbox
code
protected void Button1_Click(object sender, EventArgs e)
{
cmd2 = new OdbcCommand("Select name from users where username = '" + combobox1.Text + "' ", con);
dr1 = cmd2.ExecuteReader();
while (dr1.Read())
{
textbox1.Text = dr1.GetString(0);
}
dr1.Close();
}
The Above Code is working, but nothing displaying in Textbox1, there is the problem only in the query, I changed the query like Select name from users where username = '005'
Output is displaying in the textbox1, but when i used combobox value it is not displaying.
I also tried:
combobox1.text, combobox1.selectedvalue, combobox1.selecteditem, combobox1.selectedindex
Combobox Filling Code
cmd2 = new OdbcCommand("Select username from users, con);
ada2 = new OdbcDataAdapter(cmd2);
ada2.Fill(data2);
combobox1.DataValueField = "username";
combobox1.DataSource = data2;
combobox1.DataBind();
Username values like 201, 202, 203....,
Why is the query executing while using the combobox value....
Need Query Help
if you are using winform controls, please use below code
comboBox1.DataSource = list;
comboBox1.ValueMember = "Name";
to get value from combobox use comboBox1.SelectedValue[It will return object type]
comboBox1.Items[comboBox1.SelectedIndex].Text should give you what you want.
wrap your combo box's filling code in
if (!Page.IsPostBack) {}
It should be filled only when the page is loaded the first time and not on subsequent postbacks otherwise it will reset the binding everytime the page is posted back.
you should use combobox1.SelectedItem.Text for the desired result.
Related
Edit: I am using window forms
So, I want to change a value of NumericUpDown if the selected value in combo box changes.
I placed a data table items with the columns ID, itemName, itemPrice and Stock and set the DisplayMember property to itemName.
I used this code:
cmb.DisplayMember = "itemName";
cmb.DataSource = items;
Then to get the whole row of the selected item I used
DataRow dataRow = ((DataRowView)cmbItems.SelectedItem).Row;
The problem is that in the UI, the combo box's selected item does not changes no matter what I do but the value of the selected item changes.
Like this.
I first thought that my unit is just lagging but its not. How do I fix this?
You can try this code to check if the combobox will get the selected item when you make your option.
dbConn.Open();// this allows you to edit the database
string sql = "Select * from database1";
SqlCommand dbComm = new SqlCommand(sql, dbConn);
SqlDataAdapter dbAdapter = new SqlDataAdapter(dbComm);
DataTable dt = new DataTable();
dbAdapter.Fill(dt);
cmbDescription.DataSource = dt;
cmbDescription.DisplayMember = "itemName";
cmbDescription.ValueMember = "Enter the column name here";
cmbDescription.Text = "";
cmbDescription.Items.Add(dt);
cdbConn.Close(); //close connection to save all your inputs,calculations to the database
I found out that my code is very confusing and the system seem to be having problems while executing, I've rewritten the whole code for this window form and it is working now.
I have a 3 Layer asp.net c# code. My BL:
public DataTable ddl()
{
base.Link();
string Query = "SELECT [nam], [idZone] FROM [zones] ORDER BY idZone";
DataTable Output_Q = base.SelectDataText(Query);
base.UnLink();
return Output_Q;
}
public void insert()
{
base.Link();
string Query = "INSERT INTO students (fnam, lnam, cod, idZone) VALUES ( '{0}', '{1}', '{2}', {3} )";
Query = string.Format(Query, fnam, lnam, cod, idZone);
base.commanddatatext(Query);
base.UnLink();
My Code:
page_load:
BL_students_new F = new BL_students_new();
DropDownList1.Items.Clear();
DropDownList1.DataSource = F.ddl();
DropDownList1.DataTextField = "nam";
DropDownList1.DataValueField = "idZone";
DropDownList1.DataBind();
btn_insert:
BL_students_new F = new BL_students_new();
F.fnam = TextBox1.Text.Trim();
F.lnam = TextBox2.Text.Trim();
F.cod = TextBox3.Text.Trim();
F.idZone = Convert.ToInt32(DropDownList1.SelectedItem.Value);
F.insert();
it saves every things but dropdownlist value. note that my ddl has text and int value and i need the value to be saved. but it fails. (My DA is OK too.)
From your comment above:
populating ddl is in page_load and inserting is in btn_insert
Page_Load happens before btn_Insert. This happens every time the page is requested, regardless of whether it's a "post back" or not. (After all, the page has to load into a usable state before it can even know the nature of the HTTP request.)
So what's happening is the following:
Bind the DropDownList options
Show the page to the user
User selects an option and submits
Re-bind the DropDownList options, losing whatever the user selected
Insert to database
A simple way to address this in WebForms is to wrap your DropDownList binding logic in a condition on IsPostBack. Something like this:
// in Page_Load:
if (!IsPostBack)
{
BL_students_new F = new BL_students_new();
DropDownList1.Items.Clear();
DropDownList1.DataSource = F.ddl();
DropDownList1.DataTextField = "nam";
DropDownList1.DataValueField = "idZone";
DropDownList1.DataBind();
}
That way you're only populating the DropDownList when the page is initially requested, not when the user is submitting the form.
I know that it sounds wrong. Is It possible to get the SQL value on the dropdown list anyway, so that one of the ListItem in dropdownlist is similar to SQL.
here is my code:
private void ReviewButtonPinsDetails()
{
con.Open();
cmd = new SqlCommand(#"SELECT quo_ProdCategory,quo_ProdType
,quo_JobDesc,quo_PrintProcess
,quo_File,quo_Finishing
,quo_Quantity
FROM JobQuotations1
WHERE TransactionID = #id
AND TransactionNum = #Num", con);
cmd.Parameters.AddWithValue("#id", GridView1.SelectedRow.Cells[2].Text);
cmd.Parameters.AddWithValue("#Num", GridView1.SelectedRow.Cells[4].Text);
rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
while (rdr.Read())
{
ddlProducts.Text = rdr["quo_ProductCategory"].ToString();
ddlProdName.Text = rdr["quo_ProdType"].ToString();
txtJobDesc.Text = rdr["quo_JobDesc"].ToString();
ddlPrintProc.Text = rdr["quo_PrintProcess"].ToString();
lblFileName.Text = rdr["quo_File"].ToString();
txtFinishing.Text = rdr["quo_Finishing"].ToString();
txtQty.Text = rdr["quo_Quantity"].ToString();
}
}
con.Close();
lblFileStatus.Text = "Previous File";
}
When the customer wanted to review His/Her order and select the row on grid view It suppose to restore the value of The textboxes and dropdownlist.
I don't quite understand the framework that you are working in but have you considered using the functionality offered by the INotifyPropertyChangedinterface.
Which will allow you to use the PropertyChangedEventHandler object.
Bind the textbox in the view to a property in your model.
You could then create some properties in the class that will hold the data for the text boxes and dropdowns and ensure that when a new property is set the
PropertyChangedEventHandler object you have created will handle updating the property in the view.
First bind your dropdownlist before Sql Value assign to it.
For example:
bindProducts(ddlProducts);
ddlProducts.SelectedValue = rdr["quo_ProductCategory"].ToString();
bindProducts(ddl as DropDownList) is the method for bind the product list to dropdownlist.
I have been working on asp.net. I have a registration form with grid view.
The grid view contains the id of dropdownlist(ddl).
when i select the grid view ,all values have to be shown in appropriate fields in registration form.
For ddl, from the ddl id value in grid view ,appropriate dropdown list text is shown in dropdownlist.
THE PROBLEM COMES HERE, dropdown list showing only the appropriate value and AGAIN IT CANNOT BE CLICKED AND POPULATED FOR UPDATE BUTTON
enter image description here.
cmd.CommandText = "SELECT * FROM COMPANY WHERE COMPANYID='" + dbCompany + "'";
txtTextBox1.Text = newcmpid;
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
ddlCompanyName.DataSource = dt;
ddlCompanyName.DataTextField = "COMPANYNAME";
ddlCompanyName.DataValueField = "COMPANYID";
ddlCompanyName.DataBind();
HOW TO POPULATE THE DROPDOWNLIST WITH ALL ELEMENTS BY THE SAME TIME HIGHLIGHTING THE APPROPRIATE VALUE
To achive selection just use:
ddlCompanyName.Items.FindByValue(dbCompany).Selected = true;
But You should also notice that you SQL query is dangorous. It allows to create SQL inject attack. Instead of concatenating it you should SQL params.
So the full code could be like this:
cmd.CommandText = "SELECT * FROM COMPANY WHERE COMPANYID=#ID;";
cmd.Parameters.Add("#ID", SqlDbType.Int);
cmd.Parameters["#ID"].Value = dbCompany;
txtTextBox1.Text = newcmpid;
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
ddlCompanyName.DataSource = dt;
ddlCompanyName.DataTextField = "COMPANYNAME";
ddlCompanyName.DataValueField = "COMPANYID";
ddlCompanyName.DataBind();
ddlCompanyName.Items.FindByValue(dbCompany).Selected = true;
I have Gridview (gridview1), some TextEdits in the Form and add some Data's to few rows and i stored the gridview1 Data's and textedits to Access Database. In another form i Bind some column to gridview1 and TextEdits to new Gridview (gridview2). Now if i click edit button on any row in the gridview2, I want to get the Data's from Access Database and shown in 1st Form gridview1 and textedits fill automatically recording to Focused Row cell unique value.
Using this code i get value to TextEdits Fields
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/Srihari/OrionSystem.accdb");
OleDbCommand da = new OleDbCommand("select * from invoice_top where invoice_number=" + textEdit5.Text, con);
con.Open();
OleDbDataReader reader = da.ExecuteReader();
while (reader.Read())
{
textEdit12.Text = reader.GetValue(1).ToString();
textEdit13.Text = reader.GetValue(2).ToString();
textEdit4.Text = reader.GetString(3);
dateEdit1.Text = reader.GetValue(8).ToString();
textEdit1.Text = reader.GetValue(5).ToString();
textEdit2.Text = reader.GetValue(6).ToString();
textEdit3.Text = reader.GetValue(7).ToString();
checkEdit1.Checked = reader.GetBoolean(4);
}
con.Close();
I also want to fill gridview Data's ? I tried this bus its not working
gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[2], reader1.GetString(2));
How to set Access Database values to grdiview ?? Please Help me ?
Thanks in Advance.
I think you need to read a tutorial on Data Binding.
The GridControl can be bound to a data source which implements IList<>, IBindingList<> or IQueryable<> simply by setting the GridControl.DataSource property. There is no need to loop through your recordset and set the value for each row/cell individually. Additionally, I would suggest you Data Bind your TextEdit controls (to the EditValue property, NOT the Text property) rather than manually setting them like you are doing above.
This code is VB.NET but the ideea is the same in C#
Dim SIRCON as string = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/Srihari/OrionSystem.accdb"
Using con As New OleDbConnection(SIRCON)
con.Open()
Dim strSQL As String = "select * from invoice_top where invoice_number=" + textEdit5.Text
dim dt as new datatable
dt.Load(New OleDbCommand(strSQL, conexiune).ExecuteReader())
conexiune.Close()
GridControl1.datasource = dt
GridControl1.ForceInitialise
End Using