concat two columns in combobox from database - c#

I am trying to concat two columns but something is going wrong....
my output is not displayed .
String Query = " SELECT pa_forename , pa_surname FROM [ICPS].[dbo].[parking_attendants] order by pa_forename ";
SqlConnection conDataBase = new SqlConnection(conString);
SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
SqlDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
string pa_forename = myReader["pa_forename " +"," + "pa_surname"].ToString();
comboBox1.Items.Add(pa_forename);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

replace
string pa_forename = myReader["pa_forename " +"," + "pa_surname"].ToString();
with
string pa_forename = myReader["pa_forename"] +"," + myReader["pa_surname"];

You are doing it wrong,
string pa_forename = myReader["pa_forename " +"," + "pa_surname"].ToString();
You cannot get two columns from DataReader at the same time. Replace your code with something like this. The above code will try to look for column that doesn't exists.
string pa_forename = myReader["pa_forename"] +"," + myReader["pa_surname"];

Related

distinguish the columns of a stacked column chart

I want to draw a stacked chart from mysql database.
I want to have 4 columns named "port1", "port2", "port3" and "port4".
My problem is when I import the data from my DB, I check the type a column in the table then I draw the chart . My DB contains 4 types of port consequently I would have 4 columns named port1, port2, port3 and port4, but my code generate all the data but on the same column which is port1.
How can I add an identical number of datapoints?
try
{
con.Open();
string query1 = "SELECT type,name,value FROM " + server + " WHERE type LIKE '%port1'" ;
string query4 = "SELECT type,name,value FROM " + server + " WHERE type LIKE '%port4'";
SqlCommand cmmd = new SqlCommand(query1, con);
SqlCommand cmmd4 = new SqlCommand(query4, con);
SqlDataReader dataReader = cmmd.ExecuteReader();
while (dataReader.Read())
{
chart1.Series.Add(dataReader["name"].ToString());
chart1.Series[dataReader["name"].ToString()].ChartType = SeriesChartType.StackedColumn;
chart1.Series[dataReader["name"].ToString()]["StackedGroupName"] = "Group1";
chart1.Series[dataReader["name"].ToString()].Points.AddXY((dataReader["type"].ToString()), dataReader["value"].ToString());
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
try
{
con.Open();
string query1 = "SELECT type,name,value FROM " + server + " WHERE type LIKE '%port1'" ;
SqlCommand cmmd = new SqlCommand(query1, con);
SqlDataReader dataReader = cmmd.ExecuteReader();
while (dataReader.Read())
{
chart1.Series.Add(dataReader["name"].ToString());
chart1.Series[dataReader["name"].ToString()].ChartType = SeriesChartType.StackedColumn;
chart1.Series[dataReader["name"].ToString()]["StackedGroupName"] = "Group1";
chart1.Series[dataReader["name"].ToString()].Points.AddXY((dataReader["type"].ToString()), dataReader["value"].ToString());
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
try
{
con.Open();
string query2 = "SELECT type,name,value FROM " + server + " WHERE type LIKE '%port2'";
SqlCommand cmmd2 = new SqlCommand(query2, con);
SqlDataReader dataReader2 = cmmd2.ExecuteReader();
while (dataReader2.Read())
{
chart1.Series.Add(dataReader2["name"].ToString());
chart1.Series[dataReader2["name"].ToString()].ChartType = SeriesChartType.StackedColumn;
chart1.Series[dataReader2["name"].ToString()]["StackedGroupName"] = "Group2";
// MessageBox.Show(dataReader2["type"].ToString());
chart1.DataBind();
chart1.Series[dataReader2["name"].ToString()].Points.AddXY("Port_2", dataReader2["value"].ToString());
chart1.Series[dataReader2["name"].ToString()]["PointWidth"] = "1";
}
con.Close();
}
catch(Exception ex)
{
MessageBox.Show("Error "+ ex);
}

How to send the All items of combo Box To SQL DB using MySQL query

These are the values i want to send to the data base
private void radButton1_Click(object sender, EventArgs e)
{
string constring = "datasource=localhost;port=3306;username=root;";
string Query = "insert into rhms.reservation(no_table)values('" + this.comboBox1.SelectAll() + "');";
MySqlConnection condatabase = new MySqlConnection(constring);
MySqlCommand cmd = new MySqlCommand(Query, condatabase);
MySqlDataReader myread;
try
{
condatabase.Open();
myread = cmd.ExecuteReader();
MessageBox.Show("saved");
this.Refresh();
while (myread.Read())
{
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
You need set correct info at query string. Now it`s incorrect. Data must separated by commas:
string Query = "insert into rhms.reservation(no_table) values " + this.comboBox1.Items
.Select(p=>string.Format("({0})",p.ToString()))
.Aggregate((p1,p2)=>p1 + "," + p2) + ";"

Displaying values from a database on different rows in a textbox C#

I've been working on this program where it will pull accounts out of the database and will display them in a textbox I got this working so far but it will only pull the account from my database on the first row. Here is my code :
string myConnection = "datasource=xxxx;port=3306;username=xxxx;password=xxxx!";
string Query = "select * from xxx.xxx;";
MySqlConnection conDataBase = new MySqlConnection(myConnection);
MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
MySqlDataReader myReader;
DataTable dt = new DataTable();
try
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
string sUsername = myReader.GetString("usernames");
string sPasswords = myReader.GetString("passwords");
nsTextBox1.Text = sUsername + ":" + sPasswords;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Also is there anyway so it will erase the old Username + Password and show the new one that is pulled?
Like this?:
nsTextBox1.Text += sUsername + ":" + sPasswords + Enviroment.NewLine;
Also, your TextBox needs to be multiline.
Must it be a TextBox? Why not use a ListBox?

How do I delete a row from a Microsoft Access table using c#

I've tried this code:
string sql = " DELETE FROM HotelCustomers WHERE [Room Number] =" + textBox1.Text;
OleDbConnection My_Connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= c:\\Users\\Documents\\HotelCustomersOld.mdb");
My_Connection.Open();
OleDbCommand My_Command = new OleDbCommand(sql, My_Connection);
My_Command.ExecuteNonQuery();
Error: Data type mismatch in criteria expression, at the line:
My_Command.ExecuteNonQuery();
Use parametrized query to avoid all kind of errors
string sql = " DELETE FROM HotelCustomers WHERE [Room Number] =?";
using(OleDbConnection My_Connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= c:\\Users\\Documents\\HotelCustomersOld.mdb"))
{
My_Connection.Open();
OleDbCommand My_Command = new OleDbCommand(sql, My_Connection);
My_Command.Parameters.Add("#p1", textBox1.Text);
My_Command.ExecuteNonQuery();
}
In your case the Room NUmber field is of Text type so, you need to enclose the value in single quotes, but this is really wrong. You expose your code to maliciuos text written by your user inside the text box. A very simple and funny example here
Which type is your [Room Number] column? If it is a string then you have to write the value with inverted comma or quotation mark (I'm not sure which of both is used in Access).
string sql = " DELETE FROM HotelCustomers WHERE [Room Number] = '" + textBox1.Text + "'";
To avoid SQL injektion you should use Parameters instead of the string operation.
public static void DeleteLine(string kv)
{
OleDbConnection myConnection = GetConnection();
string myQuery = "DELETE FROM Cloth WHERE [ClothName] = '" + kv + "'";
OleDbCommand myCommand = new OleDbCommand(myQuery, myConnection);
try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine("Exception in DBHandler", ex);
}
finally
{
myConnection.Close();
}
}
try
{
OleDbConnection con = new OleDbConnection("provider = microsoft.ace.oledb.12.0;data source = E:\\Sohkidatabase\\Sohki.accdb");
con.Open();
str = "select * from compny_info where id=" + comboBox1.Text.Trim() + "";
com = new OleDbCommand(str, con);
OleDbDataReader reader = com.ExecuteReader();
if (reader.Read())
{
textBox1.Text = reader["regis_no"].ToString();
textBox2.Text = reader["comp_oner"].ToString();
textBox3.Text = reader["comp_name"].ToString();
textBox4.Text = reader["comp_add"].ToString();
textBox5.Text = reader["tin_no"].ToString();
textBox6.Text = reader["email"].ToString();
}
con.Close();
reader.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
public static void DeleteLine(string kv) {
OleDbConnection myConnection = GetConnection();
string myQuery = "DELETE FROM Cloth WHERE [ClothName] = '" + kv + "'" ;
}

checking if record in database in mysql using c#

Newbie here, I am currently creating a tool that will parse a text file "namelist.txt" contains names. my tool will check if each name are already in my database. if not exist it will write to another file "new_name.log".
my code is something like this.
in my query command I used count(*) to return the count
string DBNAME= "SELECT count(*) FROM tbl_namelist WHERE name = '" + name + "'";
conn.Open();
MySqlCommand DBCmd = new MySqlCommand(DBNAME, conn);
MySqlDataReader reader = DBCmd.ExecuteReader();
while (reader.Read())
{
if (reader == 0)
try
{
//label2.Text = reader.GetString(0);
sds.WriteLine("New Name: " + name+ " " + reader.GetString(0));
}
catch (Exception dd)
{
sds.WriteLine("duplicate Name: " + name+ " " + reader.GetString(0));
}
I dont know if i read the correct info "reader.read" to give me an integer output "count".
please assist me thanks.
don't use MySQLDataReader but instead use command's ExecuteNonScalar to fetch single value.
string DBName = "SELECT count(*) FROM tbl_namelist WHERE name = #name";
using (MySqlConnection conn = new MySqlConnection("connectionString Here"))
{
using (MySqlCommand comm = new MySqlCommand())
{
comm.Connection = conn;
comm.CommandText = DBName;
comm.CommandType = CommandType.Text;
comm.Parameters.AddWithValue("#name", name);
try
{
conn.Open();
int totalCount = Convert.ToInt32(comm.ExecuteScalar());
if (totalCount == 0)
{
sds.WriteLine("New Name: " + name + " " + totalCount);
}
else
{
// when not zero
}
}
catch( MySqlException ex)
{
// error here
}
}
}
use USING-statement and parameterized your query.
count(*) return int and you should change reader.GetString(0) to reader.GetInt32(0) and in
if(reader==0) to if (reader.GetInt32(0)==0) and it will work

Categories