I want to use a combobox to retrieve data from an access database, but it doesn't work with numerical values.
This part of the code is the connection string and object:
public partial class StockControl : Form
{
OleDbConnection connection = new OleDbConnection();
public StockControl()
{
InitializeComponent();
connection.ConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Folder\NewStockControl.mdb;Persist Security Info=False;";
}
Here is the code I used to retrieve the field ProductName in the Access database (StockControl is the name of the form I created):
private void StockControl_Load(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select ProductName from Products";
command.CommandText = query;
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
cbProductId.Items.Add(reader["ProductName"].ToString());
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex);
}
}
And here is the code I added to the combo box (cbProductId)
private void cbProductId_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.CommandType = CommandType.Text;
command.Connection = connection;
string query = "SELECT * from Products WHERE ProductName='"+cbProductId.Text+"'";
command.CommandText = query;
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
txtStockName.Text = reader["ProductName"].ToString();
txtStockQty.Text= reader["SupplyLeft"].ToString();
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex);
}
}
There are two textboxes in the StockControl form: txtStockName and txtStockQty, and I want the fields ProductName and SupplyLeft to be displayed in those textboxes respectively. However whenever I run the code the only value that shows is ProductName, but SupplyLeft doesn't. I assume it has to do with the fact that it is an integer rather than a string, but I don't know how to convert the text box to be able to display the value SupplyLeft.
i'm having troubles to list Brazillian States (The only States for now) on ComboBox2
I have one Database with 3 Tables , Paises(Contries), Estados(States) and Cidades(Cities), i'm trying to get the States using the Country Number and list it in the ComboBox2 but it isnt working.
My Code
private void Account_Create_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(#"Data Source=WINDOWS-PC\SQLSERVER;Initial Catalog=World;Integrated Security=True");
string sql = "Select * from Paises";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr;
try
{
con.Open();
sdr = cmd.ExecuteReader();
while (sdr.Read())
{
string Pais = sdr.GetString(1);
comboBox1.Items.Add(Pais);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(#"Data Source=WINDOWS-PC\SQLSERVER;Initial Catalog=World;Integrated Security=True");
string sql = "Select Cod from Paises where NomePT = '" + comboBox1.Text + "'";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr;
try
{
con.Open();
sdr = cmd.ExecuteReader();
while (sdr.Read())
{
string Cod = sdr.GetValue(0).ToString();
lbl1.Text = Cod;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(#"Data Source=WINDOWS-PC\SQLSERVER;Initial Catalog=World;Integrated Security=True");
string sql = "select Cod , Estados from Estados where PaisCod = " + lbl1.Text + "";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr;
try
{
con.Open();
sdr = cmd.ExecuteReader();
while (sdr.Read())
{
string Cod = sdr.GetValue(0).ToString();
lbl2.Text = Cod;
string Estado = sdr.GetString(1);
comboBox2.Items.Add(Estado);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(#"Data Source=WINDOWS-PC\SQLSERVER;Initial Catalog=World;Integrated Security=True");
string sql = "Select * from Cidades where Ci_Cod = " + lbl2.Text + "";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr;
try
{
con.Open();
sdr = cmd.ExecuteReader();
while (sdr.Read())
{
string Cidade = sdr.GetString(1);
comboBox3.Items.Add(Cidade);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
I'm kinda new with C# and Windows Forms, it's something on my code?
I have figured out how to make it work.
I can't explain exactly how i make it but that's the code :
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(#"Data Source=WINDOWS-PC\SQLSERVER;Initial Catalog=World;Integrated Security=True");
string sql = "Select * from Paises";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr;
try
{
con.Open();
sdr = cmd.ExecuteReader();
while (sdr.Read())
{
string Pais = sdr.GetString(4);
comboBox1.Items.Add(Pais);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(#"Data Source=WINDOWS-PC\SQLSERVER;Initial Catalog=World;Integrated Security=True");
string sql = "Select Cod from Paises where Nome = '" + comboBox1.SelectedItem + "'";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr;
try
{
con.Open();
sdr = cmd.ExecuteReader();
while (sdr.Read())
{
string PaisCod = sdr.GetValue(0).ToString();
Cod.Text = PaisCod;
}
sdr.Close();
string a = "Select Estados from Estados where PaisCod = " + Cod.Text + "";
SqlCommand cm1 = new SqlCommand(a , con);
SqlDataReader sd1;
sd1 = cm1.ExecuteReader();
while(sd1.Read())
{
string aqw = sd1.GetString(0);
comboBox2.Items.Add(aqw);
}
sd1.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(#"Data Source=WINDOWS-PC\SQLSERVER;Initial Catalog=World;Integrated Security=True");
string b = "Select Cod from Estados where Estados = '" + comboBox2.Text + "'";
SqlCommand cmd = new SqlCommand(b , con);
SqlDataReader sdr;
try
{
con.Open();
sdr = cmd.ExecuteReader();
while(sdr.Read())
{
string Cod = sdr.GetValue(0).ToString();
Ci_Cod.Text = Cod;
}
sdr.Close();
string c = "Select Cidade from Cidades where Ci_Cod = " + Ci_Cod.Text + "";
SqlCommand cm1 = new SqlCommand(c , con);
SqlDataReader sd1;
sd1 = cm1.ExecuteReader();
while(sd1.Read())
{
string Cidades = sd1.GetString(0);
comboBox3.Items.Add(Cidades);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
(Yeh, some of my variables are a mess)
So, basicaly, when the form loads, the Countries table load on CheckBox1.
When the user select a country on CheckBox the Label Cod recieves the Country Code, after that, the CheckBox2 automaticaly select the States where the "PaisCod" is equal to the Country Code, when the State is selected the Label "Ci_Cod" recieves the State Code, and finally, the CheckBox3 select the Cities that the Code is equal to the City code. Sorry if this is confusing, comment if you don't understand it.
I'm new to C#. I trying to develop WPF application. In here when I press Save button I want to refresh the all the text box after insert values to database. How can I do that? Thanks..
addnewcategory.xaml.cs
public addnewcategory()
{
InitializeComponent();
}
private void save_Click(object sender, RoutedEventArgs e)
{
string dbConn = "datasource=localhost;port=3306;username=root";
string Query = "insert into tenderprocess.mstcategory (category) values('" + this.txtcategory.Text + "');";
MySqlConnection mysqlConn = new MySqlConnection(dbConn);
MySqlCommand cmdTenderprocess = new MySqlCommand(Query, mysqlConn);
MySqlDataReader myReader;
try
{
mysqlConn.Open();
myReader = cmdTenderprocess.ExecuteReader();
MessageBox.Show("New Category Successfully Saved");
while (myReader.Read())
{
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
I try to use an update query in my C# windows form application. I do not get any errors, it just seems not to save the data I try to update. Take a look at the code:
private void button2_Click(object sender, EventArgs e)
{
try
{
string myConnection = connection;
MySqlConnection myConn = new MySqlConnection(myConnection);
myConn.Open();
MySqlDataAdapter myDataAdapter = new MySqlDataAdapter();
myDataAdapter.UpdateCommand = new MySqlCommand(" update users set username=" + textBox1.Text + " where username=" + username + " ", myConn);
MySqlCommandBuilder cb = new MySqlCommandBuilder(myDataAdapter);
DataSet ds = new DataSet();
myConn.Close();
MessageBox.Show("Changes has been saved.");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
The username is an string passed from another form (gridview) ..
I found an solution for this on the internet. What I cam up with is:
private void button2_Click(object sender, EventArgs e)
{
string myConnection = connection;
MySqlConnection myConn = new MySqlConnection(myConnection);
MySqlCommand cmdDataBase = new MySqlCommand("UPDATE `users` SET username='Test' WHERE username='CurrentName' ", myConn);
MySqlDataReader myReader;
try
{
myConn.Open();
myReader = cmdDataBase.ExecuteReader();
myConn.Close();
MessageBox.Show("Changes has been saved!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
I have created comboBox and filled with one column, after I choose item from the combobox I would like to show other column in the textboxs so I wrote code to make it happen but what if I want to choose column from another table I mean I would like to show couple of columns from two different table in the textbox when I hit the combobox
Here is my code:
private void comboLname_SelectedIndexChanged(object sender, EventArgs e)
{
string conn = "Data Source=srv-db-02;Initial Catalog=rmsmasterdbtest;Persist Security Info=True;User ID=test;Password=*******";
string Query = "select * from rmsmasterdbtest.dbo.customer where LastName= '" + comboLname.Text + "' ;";
SqlConnection Myconn = new SqlConnection(conn);
SqlCommand cmdDataBase = new SqlCommand(Query, Myconn);
SqlDataReader Reader;
try
{
Myconn.Open();
Reader = cmdDataBase.ExecuteReader();
while (Reader.Read())
{
string ID = Reader.GetInt32(Reader.GetOrdinal("ID")).ToString();
string AccountNuber = Reader.GetString(Reader.GetOrdinal("AccountNumber"));
//string Time = Reader.GetString(Reader.GetOrdinal("Time"));
// string Deposit = Reader.GetString(Reader.GetOrdinal("Deposit"));
string sstatus = Reader.GetString(Reader.GetOrdinal("status"));
string slastname = Reader.GetString(Reader.GetOrdinal("lastname"));
txtid.Text = ID;
txtacnum.Text = AccountNuber;
//txttime.Text = Time;
//txtdeposit.Text = Deposit;
txtstatus.Text = sstatus;
txtlname.Text = slastname;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
Myconn.Close();
}
}