i just followed a tutorial and now is stucked with a "MySql.Data.MySqlClient.MySqlException" - Exception and "Unable to connect to any of the specified MySQL hosts".
What is causing those? The Credentials are all right.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
MySqlConnection con = new MySqlConnection(#"Data Source=appm52;port=3306;Initial Catalog=test;User Id=skaratekedb;password=***");
int i;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
i = 0;
con.Open();
MySqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from login where username = '" + textBox1.Text + "' and password= '" + textBox2.Text + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dt);
i = Convert.ToInt32(dt.Rows.Count.ToString());
if (i == 0)
{
label3.Text = "you have entered invalid username pr password";
}
else
{
this.Hide();
Form2 fm = new Form2();
fm.Show();
}
con.Close();
}
}
}
Related
I have login issue authentication on windows form C# application. Once I register user it send user data to a SQL Server database. When I am trying to log in. Even if credentials match to data in data base message box showing up. Please see the code below.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using travel_booking.UserControlers;
using System.Data.SqlClient;
namespace travel_booking
{
public partial class UserContrLogin : UserControl
{
internal Action<object, EventArgs> OnUserLogin;
UserContrRegister userContrRegister;
public UserContrLogin()
{
InitializeComponent();
}
public void setUserContrRegister(UserContrRegister userContrRegister)
{
this.userContrRegister = userContrRegister;
}
private void Exit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void LoginButton_Click(object sender, EventArgs e)
{
SqlConnection sqlConnection = new SqlConnection(#"//Removed by me as it is sensitive data");
sqlConnection.Open();
string query = "Select * from tblUser Where Email = ' " + txtEmail.Text.Trim() + "' and Password = '" + txtPassword.Text.Trim() + "'";
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(query, sqlConnection);
DataTable dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
if (dataTable.Rows.Count > 0)
this.Hide();
else
MessageBox.Show("Email or/and Password is/are invalid. Please try again");
sqlConnection.Close();
}
}
}
You can use this code to work much better
public void Login()
{
SqlConnection sqlConnection = new SqlConnection(#"//Removed by me as it is sensitive data");
sqlConnection.Open();
string query = "Select * from tblUser Where Email = #Email and Password = #Password";
SqlCommand command = new SqlCommand();
command.Connection = sqlConnection;
command.CommandType = CommandType.Text;
command.Text = query;
command.Parameters.AddWithValue("#Email", txtEmail.Text.Trim());
command.Parameters.AddWithValue("#Password", txtPassword.Text.Trim());
SqlDataReader reader = command.ExecuteReader();
if(reader.Read() == true)
{
this.Hide();
}
else
{
MessageBox.Show("Email or/and Password is/are invalid. Please try again");
}
}
I use the command.Parameters.AddWithValue() to avoid the concatenation of the string of your query that can cause an SQL INJECTION
I facing error when want to pass my data which insert at Form1(frmLogin) to Form2(frmMain) SQL Query.
Form1(frmLogin)coding
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace SytelineAutoGenerateDocNum
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
SqlConnection sqlcon = new SqlConnection(#"Data Source=P13L3CFN756\SQLEXPRESS;Initial Catalog=Syteline_Misc_DocNum;Persist Security Info=True;User ID=sa;Password=1234;");
string query = "SELECT *FROM [Syteline_Misc_DocNum].[dbo].[users] Where username = '" + txtUsername.Text.Trim() + "' and password = '" + txtPassword.Text.Trim() + "'";
SqlDataAdapter sda = new SqlDataAdapter(query, sqlcon);
DataTable dtbl = new DataTable();
sda.Fill(dtbl);
if (dtbl.Rows.Count == 1)
{
frmMain objFrmMain = new frmMain();
//objFrmMain.Value = txtUsername.Text;
//objFrmMain.ShowDialog();
objFrmMain.Show();
this.Hide();
}
else
{
MessageBox.Show("Check your username and password");
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Form2(frmMain)Coding
private void buttonGDN_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("Insert into DocNum (Date,UserID,BusinessUnit,RunningNumber,DocumentNumber)values('" + txtUsername.Text + "')");
}
My Error is (The Name "txtUsername" does not exist in the current context).
I have provided an image of the error I am getting Error screenshot
Any help is much appreciated, Thanks
What I have tried:
I have no idea on the Error.
I have tried to enable the System Configuration on the reference, this did not solve the issue.
I am trying to show records in a DataGridView using Microsoft Access 2010. But the problem is each time i click on the button1 i get an error saying "The ConnectionString property has not been initialized"
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Attendance_Generation_System
{
public partial class Take_attendance : Form
{
OleDbConnection conn = new OleDbConnection();
public Take_attendance()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
conn.ConnectionString=#"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = F:\UNI WORK\7th Semester\Visual Programming\Database31.accdb; " + "User id = admin; " + "Password = "; ;
OleDbCommand cmd =new OleDbCommand("SELECT * FROM Attendancerecord");
OleDbDataAdapter add = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
add.Fill(dt);
dataGridView1.DataSource=dt;
cmd.ExecuteNonQuery();
}
}
}
The error is clear that tells you the connection string is not assigned. So, you should assign the connection string for OleDbDataAdapter
var connectionString = #"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = F:\UNI WORK\7th Semester\Visual Programming\Database31.accdb; " + "User id = admin; " + "Password = ";
using (var oledbCnn = new OleDbConnection(connectionString))
{
oledbCnn.Open();
var cmd = new OleDbCommand("SELECT * FROM Attendancerecord", oledbCnn);
OleDbDataAdapter add = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
add.Fill(dt);
dataGridView1.DataSource = dt;
oledbCnn.Close();
}
I'm trying to writing a basic c# program that read datas from SQL and writes results on 3 textboxes and a label. Here is my code;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace PLAKA
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=TESTDB;Integrated Security=True");
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Project where ID = '" + textBox1.Text + "'", con);
DataTable dt = new DataTable();
sda.Fill(dt);
textBox1.Text = dt.Rows[0][0].ToString();
textBox2.Text = dt.Rows[0][1].ToString();
textBox3.Text = dt.Rows[0][2].ToString();
textBox4.Text = dt.Rows[0][3].ToString();
SqlDataAdapter oks = new SqlDataAdapter("SELECT * FROM Project where Status = 'YES'", con);
if (oks)
{
label1.Text = "POSITIVE";
}
else
{
label1.Text = "NEGATIVE";
}
}
}
}
I'm writing ID number and see the informations of this ID on text boxes in my first part of my code and this works perfectly
All i need that when value in the 'Status' raw is "YES", my program writes "POSITIVE", other else writes 'NEGATIVE' on label1
Meanwhile Status information writes on Textbox3.
For this code i got this error message: "Error 1 Cannot implicitly convert type 'System.Data.SqlClient.SqlDataAdapter' to 'bool'
How can i solve this problem?
Your If condition is wrong.
use this.
SqlDataAdapter oks = new SqlDataAdapter("SELECT * FROM Project where Status = 'YES'", con);
oks.fill(dataSet)
if (dataSet.Tables["Table"].Rows.Count > 1)
{
label1.Text = "POSITIVE";
}
else
{
label1.Text = "NEGATIVE";
}
I am currently building an application in C#. In this application, people give values in a label and that values will be saved in a database. This also works at the moment. What I want is the following:
The people can see their own values back in a datagridview. If I do this with the wizard of the datagridview I can't select label2.Text(this is the customerID).
The query I used below is the following:
SELECT * FROM prestaties WHERE gebruikerid = '" + label1.Text + "'";
But it doesn't work. I don't see anything in my datagrid.
Sorry for my bad English. I hope you can understand me.
The code below I already have, but it doesn't work
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Eindopdracht
{
public partial class resultaten : Form
{
public resultaten()
{
InitializeComponent();
dataGridView1.Show();
}
private string username;
public void displaydata(String ddata)
{
OleDbConnection conn = new OleDbConnection(#"provider= microsoft.jet.oledb.4.0;data source=C:\\Users\\Jeffrey\\Desktop\\Eindopdracht WOE\\Eindopdracht\\sample.mdb");
string select = "SELECT * FROM prestaties WHERE gebruikerid = '" + label2.Text + "'";
conn.Open();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(select, conn);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
dataGridView1.DataSource = ds.tables[0];
conn.Close();
}
public void setUsername(String name)
{
username = name;
label1.Text = username;
setID();
}
public void setID()
{
OleDbConnection vcon = new OleDbConnection(#"provider= microsoft.jet.oledb.4.0;data source=C:\\Users\\Jeffrey\\Desktop\\Eindopdracht WOE\\Eindopdracht\\sample.mdb");
string selectie = "SELECT id FROM inlog WHERE Username='" + label1.Text + "'";
OleDbCommand vcom1 = new OleDbCommand(selectie, vcon);
vcon.Open();
vcom1.ExecuteNonQuery();
OleDbDataReader dr = null;
dr = vcom1.ExecuteReader();
while (dr.Read())
{
var waarde = dr.GetValue(0);
label2.Text = waarde.ToString();
}
I don't know if my code is 100% correct. I've build this by myself. If I use the class displayData in the begin next to InitializeComponent(); I will get an error.