I am fully new in the programming section so don't try bad help pls.
Actually I am fail focus data from phpmyadmin to c# windows form textbox1.Text area.
My database name: business, Table Name: life, columns Name : Email_id
I need to show email id 10 number row in my textbox1.Text area.
See my code
string connString = "datasource=x5x.1x1.13x.xxx;Database=business;username=sumon;password=root";
MySqlConnection connect = new MySqlConnection(connString);
MySqlCommand myCommand = connect.CreateCommand();
string input = textBox1.Text;
myCommand.CommandText = "SELECT * FROM life WHERE id = #input";
connect.Open();
MySqlDataReader reader = myCommand.ExecuteReader();
if (reader.Read())
textBox1.Text = reader["*"].ToString();
connect.Close();
you've missed to set the value for #input. I edited you code (without testing). Give it a try.
string connString = "datasource=x5x.1x1.13x.xxx;Database=business;username=sumon;password=root";
MySqlConnection connect = new MySqlConnection(connString);
MySqlCommand myCommand = connect.CreateCommand();
myCommand.Parameters.Add(new SqlParameter("input", 10));
//string input = textBox1.Text;
myCommand.CommandText = "SELECT * FROM life WHERE id = #input";
connect.Open();
MySqlDataReader reader = myCommand.ExecuteReader();
if (reader.Read())
textBox1.Text = reader["Email_id"].ToString();
connect.Close();
Related
I have below data in SQL query, and am showing the current user "Domain" logged in I want to show the full name in label based on each user using the Windows form,
I got stuck here, how I can continue to find the value
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
label1.Text = userName.ToUpper();
SqlConnection con = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=db;User ID=AAA;password=******");
con.Open();
string command7 = "SELECT distinct [fullname] ,[Group] ,[Domain] FROM [tableA] where fullname is not null and [group] is not null and [Domian] = '"+ label1.Text + "'";
SqlCommand da7 = new SqlCommand(command7, con);
Full name
Group
Domain
Alex Sam J
A GROUP
test\Alex
Jon Pete F
B GROUP
test\Jon
You can get the value of full name this way :
...
SqlCommand cmd = new SqlCommand(command7, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
labelFullName.Text = da.Tables[0].Rows[0]["fullname"].ToString();
I solve it like this, thanks
SqlCommand cmd = new SqlCommand(da7, con);
cmd.CommandType = CommandType.Text;
//con.Open();
SqlDataReader reader = cmd.ExecuteReader();
//DataTable dt7 = new DataTable();
while (reader.Read())
{
label21.Text = reader["Technician"].ToString();
}
con.Close();
I'm trying to INSERT data into my SQL database but its not showing anything at all.
This is a for an online role-playing game . There is no error but when I refresh my browser for phpmyadmin using XAMP, no data is being shown.
MySqlConnection connection = new MySqlConnection(connectionString);
connection.Open();
string checkDatabase = "select * from players where username = #playerName";
MySqlCommand command = new MySqlCommand(checkDatabase, connection);
command.Parameters.AddWithValue("#playerName", player.SocialClubName);
MySqlDataReader reader = command.ExecuteReader();
if(reader.Read())
{
player.SendChatMessage("There is an account with the assiociated Social Club Profile!");
}
else
{
MySqlConnection connection1 = new MySqlConnection(connectionString);
connection1.Open();
string playerInsert = "insert into players(username,password) VALUES (#user,#password)";
MySqlCommand command1 = new MySqlCommand(playerInsert, connection1);
command1.Parameters.AddWithValue("#user", player.SocialClubName);
command1.Parameters.AddWithValue("#password", password);
connection1.Close();
}
connection.Close();
You need to execute the query. Try:
...
command1.ExecuteNonQuery();
That's cause you are not executing the query at all as can be seen in below posted code
string playerInsert = "insert into players(username,password) VALUES (#user,#password)";
MySqlCommand command1 = new MySqlCommand(playerInsert, connection1);
command1.Parameters.AddWithValue("#user", player.SocialClubName);
command1.Parameters.AddWithValue("#password", password);
command1.ExecuteNonQuery(); //execute the query
connection1.Close();
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());
}
Hi I am submitting a form to access but want to get the access assigned auto number to display in a textbox after I submit. Below it was i have, any suggestions would be great!
string cmdstr = "Insert into TaskPerformed(TaskType,OtherType,Analyst,DateCompleted)Values(#b,#c,#d,#e)";
string query2 = "Select ##IDENTITY";
OleDbConnection con1 = new OleDbConnection(constr);
OleDbCommand com = new OleDbCommand(cmdstr, con1);
OleDbCommand cmdNewID = new OleDbCommand("SELECT ##IDENTITY", con1);//
con1.Open();
cmd.CommandText = query2;
com.ExecuteNonQuery();
con1.Close();
label16.Text = cmdNewID.ToString();
It looks like the problem you're having is because you're not executing the second command... and you're closing the connection before using it
using(OleDbCommand cmdNewID = new OleDbCommand("SELECT ##IDENTITY", con1))
{
con1.Open();
cmd.CommandText = query2;
com.ExecuteNonQuery();
label16.Text = cmdNewID.ExecuteScalar();
}
I've seen this question asked a couple times but I couldn't find a good answer. I've been stuck for hours on this.
Basically I have usernames saved in a database and when a new user registers I want to check if his username is available - and if it is available add him to the database. And they register through a textbox called FName. The table is called Users.
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT FName FROM Users WHERE FName = ????? usernames????? ", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader["text"].ToString());
}
How can I fix this code?
"SELECT FName FROM Users WHERE FName = #paramUsername"
and then you insert the parameter into the cmd like so:
cmd.Parameters.Add("paramUsername", System.Data.SqlDbType.VarChar);
cmd.Parameters["paramUsername"].Value = "Theusernameyouarelookingfor";
Check this out:
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
string validationQuery = "SELECT * FROM Users WHERE FName = #name";
SqlCommand validationCommand = new SqlCommand(validationQuery, connection);
validationCommand.Parameters.Add("#name", SqlDbType.VarChar).Value = loginUserSelected;
connection.Open();
SqlDataReader validationReader = validationCommand.ExecuteReader(CommandBehavior.CloseConnection);
if (!validationReader.Read())
{
string insertQuery = "INSERT INTO Users (FName) VALUES (#name)";
SqlCommand insertCommand = new SqlCommand(insertQuery, connection);
insertCommand.Parameters.Add("#name", SqlDbType.VarChar).Value = loginUserSelected;
connection.Open();
insertCommand.ExecuteNonQuery();
insertCommand.Dispose();
connection.Close();
}
else
{
//Uh oh, username already taken
}
validationReader.Close();
validationCommand.Dispose();
Things to note:
Use parameters, avoid concatenating strings because it's a security vulnerability
Always Close and Dispose your ADO objects