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);
}
}
}
}
Related
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 am starting out in C# and am making a tool to update a field in a table when the box_id is in the textbox and the button is clicked. I have worked my way through many errors and now everything runs error free, but no data is being changed. Any help with the issue would be a big help.
try
{
using (SqlConnection conn = new SqlConnection(connString))
{
string query = #"UPDATE Shipment_SCK
SET printed = '1'
WHERE box_id IN (#textBox1);";
SqlCommand cmd = new SqlCommand(query, conn);
Form1 a = new Form1();
cmd.Parameters.Add("#textBox1", SqlDbType.VarChar, 7).Value = a.textBox1.Text;
conn.Open();
int rowsaffected = cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show(rowsaffected.ToString() + " Shipments updated");
}
}
catch (Exception ex)
{
MessageBox.Show("Unable to update:" + ex.Message);
}
At this lines:
Form1 a = new Form1();
cmd.Parameters.Add("#textBox1", SqlDbType.VarChar, 7).Value = a.textBox1.Text;
You're instantiating a new Form1 instance, maybe you can't update nothin' due to empty values on your IN statement. I don't know your logic and how data comes to the method, but maybe you can do something like this:
try
{
using (SqlConnection conn = new SqlConnection(connString))
{
string boxIds = "134, 593, 333, 666";
string query = $#"UPDATE Shipment_SCK
SET printed = '1'
WHERE box_id IN ({boxIds});";
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
int rowsaffected = cmd.ExecuteQuery();
conn.Close();
MessageBox.Show(rowsaffected.ToString() + " Shipments updated");
}
}
catch (Exception ex)
{
MessageBox.Show("Unable to update:" + ex.Message);
}
Where boxIds its a string containing all the IDs to be used in your query.
Kind regards
According to your description,you want to transfer values between forms when you press a
button and the box_id in database will change.
You can try the following code to solve this problem.
Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string text1
{
get
{
return textBox1.Text;
}
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.Show();
}
}
Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
try
{
using (SqlConnection conn = new SqlConnection(connString))
{
string query = #"UPDATE Shipment_SCK
SET printed = '1'
WHERE box_id IN (#textBox1);";
SqlCommand cmd = new SqlCommand(query, conn);
Form1 a = (Form1)Application.OpenForms["Form1"];
textBox1.Text = a.text1;
cmd.Parameters.Add("#textBox1", SqlDbType.VarChar, 7).Value = a.text1;
conn.Open();
int rowsaffected = cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show(rowsaffected.ToString() + " Shipments updated");
}
}
catch (Exception ex)
{
MessageBox.Show("Unable to update:" + ex.Message);
}
}
}
i have this source , i want create a search button to find the id & name & date & very important ( retrieve the image)
&
another search button to find the name recorded between to date
such as 21/2/2012 & 1/3/2012
find all record between to date .
namespace _2015
{
public partial class import : Form
{
public import()
{
InitializeComponent();
}
private void bt_save_Click(object sender, EventArgs e)
{
byte[] imagebt = null;
FileStream fstream = new FileStream(this.txt_ipath.Text, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fstream);
imagebt = br.ReadBytes((int)fstream.Length);
string constring = "Data Source=SKYNET-PC;Initial Catalog=2015;Integrated Security=True";
string Query = "insert into t_import (id,name,barwar,imgc)" + "values('" + this.t_id.Text + "','" + this.t_name.Text + "','"+this.dt1.Text+"',#IMG); ";
SqlConnection condatabase = new SqlConnection(constring);
SqlCommand cmddatabase = new SqlCommand(Query, condatabase);
SqlDataReader myreader;
try
{
condatabase.Open();
{
cmddatabase.Parameters.Add(new SqlParameter("#IMG",imagebt));
myreader = cmddatabase.ExecuteReader();
MessageBox.Show("پاشەکەوت کرا");
while (myreader.Read())
{
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void update_Click(object sender, EventArgs e)
{
string constring = "Data Source=SKYNET-PC;Initial Catalog=2015;Integrated Security=True";
string Query = "update t_import set id='" + this.t_id.Text + "',name='" + this.t_name.Text + "' where id='"+this.t_id.Text+"' ; ";
SqlConnection condatabase = new SqlConnection(constring);
SqlCommand cmddatabase = new SqlCommand(Query, condatabase);
SqlDataReader myreader;
try
{
condatabase.Open();
{
myreader = cmddatabase.ExecuteReader();
MessageBox.Show("بە سەرکەوتوویی نوێ کرایەوە");
while (myreader.Read())
{
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void bt_delete_Click(object sender, EventArgs e)
{
string constring = "Data Source=SKYNET-PC;Initial Catalog=2015;Integrated Security=True";
string Query = "delete from t_import where id='" + this.t_id.Text + "' ; ";
SqlConnection condatabase = new SqlConnection(constring);
SqlCommand cmddatabase = new SqlCommand(Query, condatabase);
SqlDataReader myreader;
try
{
condatabase.Open();
{
myreader = cmddatabase.ExecuteReader();
MessageBox.Show("بە سەرکەوتوویی سردرایەوە");
while (myreader.Read())
{
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void browse_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "JPG Files(*.jpg)|*.jpg|PNG Files(*.PNG)|*.png|All Files (*.*)|(*.*)";
if (dlg.ShowDialog() == DialogResult.OK)
{
string picpath = dlg.FileName.ToString();
txt_ipath.Text = picpath;
pictureBox1.ImageLocation = picpath;
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void dt1_ValueChanged(object sender, EventArgs e)
{
}
private void comboBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
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 need to populate a combobox in page1 with the address details of a selected name contained in another combobox held in the MainWindow page. I have tried the code below, but combobox name in MainWindow is not recognised.
MainWindow:
private void displayParts()
{
try
{
sc.Open();
string Query = "select * from Parts";
SqlCommand createCommand = new SqlCommand(Query, sc);
SqlDataReader dr = createCommand.ExecuteReader();
while (dr.Read())
{
string Name = dr.GetString(1);
cbParts.Items.Add(Name);//Displaying a list in the Combo Box
}
sc.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Page1:
private void ComboBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
string constring = "Data Source=.;Initial Catalog=**.MDF;Integrated Security=True";
DataContext=MainWindow.
string Query = "select * from Partners where Name='" + cbParts.SelectedItem.ToString() + "' ;";
SqlConnection conDataBase = new SqlConnection(constring);
SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
SqlDataReader myReader;
try
{
sc.Open();
myReader = cmdDataBase.ExecuteReader();
if (myReader.Read())
{
txtPartner.Text = myReader["Name"].ToString();
}
myReader.Close();
sc.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
// may need to cast this to your MainWindow's type
var mainWindow = Application.Current.MainWindow;
//...
mainWindow.cbParts.Items.Add(Name);
Additionally, you could bind your combobox to an ObservableCollection property, and add the items to the collection.