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?
Related
I am getting error:
Connection must be valid and Open c# Winform.
Here is my code, can anyone please help what is wrong in the below code?
public void NameSearch()
{
listBox1.Visible = true;
try
{
String constring = "datasource=localhost;port=3306;Initial Catalog = 'svms'; username = svms; password =svms2016CPU";
string query = "select * from studentinformation where StudLname='" + metroTextBox1.Text + "'";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(query, conDataBase);
MySqlDataReader myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
string Lname = myReader.GetString(myReader.GetOrdinal("StudLname"));
string Fname = myReader.GetString(myReader.GetOrdinal("StudFname"));
string Mname = myReader.GetString(myReader.GetOrdinal("StudMname"));
string nameResult = Lname + ", " + Fname + " " + Mname;
listBox1.Items.Add(nameResult);
}
conDataBase.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
You haven't open connection before use it, Open connection
conDataBase.Open();
Open the conn before read:
conDataBase.Open();
Like:
String constring = "datasource=localhost;port=3306;Initial Catalog = 'svms'; username = svms; password =svms2016CPU";
string query = "select * from studentinformation where StudLname='" + metroTextBox1.Text + "'";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(query, conDataBase);
//
conDataBase.Open();
//
MySqlDataReader myReader = cmdDataBase.ExecuteReader();
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) + ";"
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"];
I am making a student database which contain the course id, course name, credits of each course, pass fail statement and the grades. Now I want to get the sum of the grades in a button and make them appear on a textbox. this is the code I wrote, but it's giving me an error saying the sum function doesn't exist. What should I do?
private void button1_Click(object sender, EventArgs e)
{
string ConString = " datasource = localhost; port = 3306; username = root; password = 3306";
string Query = " Select sum (grade) form studentdata.semestre1";
MySqlConnection ConDatabase = new MySqlConnection(ConString);
MySqlCommand cmdDataBase = new MySqlCommand(Query, ConDatabase);
MySqlDataReader myReader;
ConDatabase.Open();
myReader = cmdDataBase.ExecuteReader() ;
while (myReader.Read())
{
textBox2.Text = myReader.GetString(0);
}
myReader.Close();
Two reasons:
SQL query should not have space between the function and the column name
Change your query from "Form" to "FROM"
string Query = "Select SUM(grade) FROM studentdata.semestre1";
Try:
SELECT SUM(grade) FROM studentdata.semestre1
The most important change is the from.
For a more complete fix; add using, and use ExecuteScalar here:
using(MySqlConnection ConDatabase = new MySqlConnection(ConString))
using(MySqlCommand cmdDataBase = new MySqlCommand(
"SELECT SUM(grade) FROM studentdata.semestre1", ConDatabase))
{
ConDatabase.Open();
textBox2.Text = Convert.ToString(cmdDataBase.ExecuteScalar());
}
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();
}
}