create search button in c# & retrieve the image by button - c#

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)
{
}
}
}

Related

Problems listing States on ComboBox (C#)

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.

Reading tables from SQL Server Express database

I am trying to read my tables that are contained within my database in SQL Server Express, but it keeps coming up empty. What is it that am doing wrong?
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string connectionString = "Data Source=LPMSW09000012JD\\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
string query = "SELECT * FROM INFORMATION_SCHEMA.TABLES";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(query, connection);
command.Connection.Open();
command.ExecuteNonQuery();
}
}
Updated but still empty:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try {
string connectionString = "Data Source=LPMSW09000012JD\\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
SqlConnection con2 = new SqlConnection(connectionString);
con2.Open();
string query = "SELECT * FROM INFORMATION_SCHEMA.TABLES";
SqlCommand cmd2 = new SqlCommand(query, con2);
SqlDataReader dr2 = cmd2.ExecuteReader();
while (dr2.Read())
{
comboBox1.Items.Add((string)dr2[0]);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}}
Latest Updated code. Think I figured it but still showing empty:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try {
string connectionString = "Data Source=LPMSW09000012JD\\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
SqlConnection con2 = new SqlConnection(connectionString);
con2.Open();
string query = "SELECT * FROM INFORMATION_SCHEMA.TABLES ";
SqlCommand cmd2 = new SqlCommand(query, con2);
SqlDataReader dr2 = cmd2.ExecuteReader();
while (dr2.Read())
{
string Dtables = dr2.GetString(dr2.GetOrdinal("TABLE_NAME"));
comboBox1.Items.Add(Dtables);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}}
Full class:
namespace unique_database{ public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
string connectionString = "Data Source=LPMSW09000012JD\\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
string query = "CREATE TABLE [dbo].[" + textBox1.Text + "](" + "[Code] [varchar] (13) NOT NULL," +
"[Description] [varchar] (50) NOT NULL," + "[NDC] [varchar] (50) NULL," +
"[Supplier Code] [varchar] (38) NULL," + "[UOM] [varchar] (8) NULL," + "[Size] [varchar] (8) NULL,)";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(query, connection);
command.Connection.Open();
command.ExecuteNonQuery();
}
SqlConnection con = new SqlConnection("Data Source=LPMSW09000012JD\\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True");
string filepath = textBox2.Text; //"C:\\Users\\jdavis\\Desktop\\CRF_105402_New Port Maria Rx.csv";
StreamReader sr = new StreamReader(filepath);
string line = sr.ReadLine();
string[] value = line.Split(',');
DataTable dt = new DataTable();
DataRow row;
foreach (string dc in value)
{
dt.Columns.Add(new DataColumn(dc));
}
while (!sr.EndOfStream)
{
value = sr.ReadLine().Split(',');
if (value.Length == dt.Columns.Count)
{
row = dt.NewRow();
row.ItemArray = value;
dt.Rows.Add(row);
}
}
SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
bc.DestinationTableName = textBox1.Text;
bc.BatchSize = dt.Rows.Count;
con.Open();
bc.WriteToServer(dt);
bc.Close();
con.Close();
}
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "CSV files (*.csv)|*.csv|XML files (*.xml)|*.xml";
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.textBox2.Text = openFileDialog.FileName;
}
}
private void FillCombo()
{
try
{
string connectionString = "Data Source=LPMSW09000012JD\\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
using (SqlConnection con2 = new SqlConnection(connectionString))
{
con2.Open();
string query = "SELECT * FROM INFORMATION_SCHEMA.TABLES ";
SqlCommand cmd2 = new SqlCommand(query, con2);
SqlDataReader dr2 = cmd2.ExecuteReader();
while (dr2.Read())
{
int col = dr2.GetOrdinal("TABLE_NAME");
comboBox1.Items.Add(dr2[col].ToString());
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Ok, first, don't use comboBox1_SelectedIndexChanged to fill comboBox1.
use YourForm.OnLoad event, the form constructor, or click a button, but don't use SelectedIndexChanged to fill itself, because you're executing this procedure every time you select one item of comboBox1.
To get column index simply use: dr2.GetOrdinal("TABLE_NAME");
private void FillCombo()
{
try
{
string connectionString = "Data Source=LPMSW09000012JD\\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
using (SqlConnection con2 = new SqlConnection(connectionString))
{
con2.Open();
string query = "SELECT * FROM INFORMATION_SCHEMA.TABLES";
SqlCommand cmd2 = new SqlCommand(query, con2);
SqlDataReader dr2 = cmd2.ExecuteReader();
while (dr2.Read())
{
int col = dr2.GetOrdinal("TABLE_NAME");
comboBox1.Items.Add(dr2[col].ToString());
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Use ExecuteReader() instead of ExecuteNonQuery().then Use SqlDataReader() for get result.

Refresh the WPF application window

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);
}
}
}
}

Windows Form Application update query not save data

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);
}
}

update not working

protected void Page_Load(object sender, EventArgs e)
{
txtHidden.Text = Request.QueryString["YKcode"];
Display();
}
private void Display()
{
SqlDataReader reader;
SqlConnection con = new SqlConnection("Data Source=Localhost;Initial Catalog=MLC000022;User ID=sa;Password=Adama6DaY; Integrated Security=True");
SqlCommand cmd = new SqlCommand("SELECT " + " dbo.GMYAKU.NAME, " +"FROM " +
" dbo.GMYAKU " + " WHERE " +
" (dbo.GMYAKU.YKCODE = ('" + txtHidden.Text + "'))",con) ;
con.Open();
reader = cmd.ExecuteReader();
if (reader.Read())
{
this.TextBox1.Text = reader["NAME"].ToString();
}
else
{
// 読めないので画面を初期化する
}
cmd.Connection.Close();
cmd.Dispose();
con.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
string connetionString = null;
SqlConnection cnn;
SqlCommand cmd;
connetionString = ("Data Source=Localhost;Initial Catalog=MLC000022;User ID=sa;Password=redacted; Integrated Security=True");
string strSQL ;
strSQL = "UPDATE GMYAKU SET";
strSQL += " NAME = '" + (TextBox1.Text) + "'";
strSQL += " WHERE";
strSQL += " YKCODE= '" + txtHidden.Text + "'";
cnn = new SqlConnection(connetionString);
try
{
cnn.Open();
cmd = new SqlCommand(strSQL, cnn);
cmd.ExecuteNonQuery();
cmd.Dispose();
// cnn.Close();
//MessageBox.Show(" ExecuteNonQuery in SqlCommand executed !!");
}
catch (Exception ex)
{
// MessageBox.Show("Can not open connection ! ");
}
Response.Redirect("Default.aspx");
}
It may be something as simple as not having the control in an update panel to refresh with the new data, but without more information/context it is impossible to tell
I think you need to check the Request.QueryString["YKcode"]; before assigning value to txthidden.text like:
protected void Page_Load(object sender, EventArgs e)
{
if(!Request.QueryString["YKcode"].equals("") && Request.QueryString["YKcode"]!=null)
{
txtHidden.Text = Request.QueryString["YKcode"];
Display();
}
}

Categories