ExecuteCommand on Entity - mysql

protected void Button2_Click(object sender, EventArgs e)
{
Response.Write(Execute("SELECT NOW()"));
}
public string Execute(string storedProcedureName)
{
using (EntityConnection connection = (EntityConnection)new EGModel.EGEntity().Connection)
{
using (EntityCommand command = connection.CreateCommand())
{
command.CommandType = CommandType.Text;
command.CommandText = storedProcedureName;
connection.Open();
return command.ExecuteScalar().ToString();
}
}
}
I am getting error "The query syntax is not valid. Near line 1, column 13."; I am very curious, why this error?
(Mysql Entity Connection)

var A = new EGModel.EGEntity().Connection;
var command = ((EntityConnection)(A)).StoreConnection.CreateCommand();
command.CommandType = System.Data.CommandType.Text;
command.CommandText = "SELECT NOW()";
((EntityConnection)(A)).StoreConnection.Open();
string id = (string)command.ExecuteScalar();
#GTSouza Thank you for your comments,

Related

Insert data from mysql to cli listbox

listbox is emptyI want to take data from my sql and insert it into my listbox form but i can't. By compiling listbox is empty. What do i wrong?
private: System::Void listBox1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e)
{
String^ SQLconnection = "Server = localehost; Uid = root; Password = pass123; Database = details";
MySqlConnection^ conn = gcnew MySqlConnection(SQLconnection);
MySqlCommand^ cmd = gcnew MySqlCommand("SELECT * FROM details.material;", conn);
MySqlDataReader^ reader;
try
{
conn->Open();
reader = cmd->ExecuteReader();
while (reader->Read())
{
listBox1->Text +=(reader->GetInt32(0));
}
}catch (Exception^ ex)
{
MessageBox::Show(ex->Message);
}
}

Could not load type 'Devart.Data.MySql.MySqlDependency' from assembly 'Devart.Data.MySql

When I run my code, there is an error as below:
Could not load type 'Devart.Data.MySql.MySqlDependency' from assembly
'Devart.Data.MySql, Version=8.10.1086.0, Culture=neutral,
PublicKeyToken=09af7300eec23701'.
Which part would cause this problem?
Here is my sample codes...
using Devart.Data.MySql;
public partial class Form1 : Form
{
public string Query = "Select * from tbltransaction";
public MySql.Data.MySqlClient.MySqlConnection cn;
public MySql.Data.MySqlClient.MySqlDataAdapter sqlAdapter;
public DataSet dsdatagrid;
public DataTable dt;
public DataGridView datagrid;
public Form1()
{
InitializeComponent();
Start();
}
string connectionString = "User Id=root;Password=123!##abcD;Host=localhost;Database=kcms1_test;";
void Start()
{
try
{
MySqlConnection connection = new MySqlConnection(connectionString);
connection.Open();
MySqlCommand commandDeptEmp = new MySqlCommand("select * from tbltransaction", connection);
//MySqlCommand commandPict = new MySqlCommand("select * from mysqlnet_pictures", connection);
MySqlDependency dependency = new MySqlDependency(commandDeptEmp, 1000);
dependency.AddCommandDependency(commandDeptEmp);
//dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
MySqlDependency.Stop(connectionString);
MySqlDependency.Start(connectionString);
OpenLocalDBConnection();
View_Data();
DataGridView_tbltransaction.DataSource = datagrid.DataSource;
}
catch
{
Stop();
}
finally
{
}
}
void Stop()
{
MySqlDependency.Stop(connectionString);
}
void dependency_OnChange(object sender, MySqlTableChangeEventArgs e)
{
// process changes
View_Data();
DataGridView_tbltransaction.DataSource = datagrid.DataSource;
}
void View_Data()
{
sqlAdapter = new MySql.Data.MySqlClient.MySqlDataAdapter(Query, cn);
dsdatagrid = new DataSet();
sqlAdapter.Fill(dsdatagrid);
dt = new DataTable();
datagrid.DataSource = dsdatagrid.Tables;
}
void OpenLocalDBConnection()
{
cn.ConnectionString = "server=locahost;userid=root;password=123!##abcD;database=kcms1_test";
cn.Open();
}
}

MySql.Data.MySqlClient.MySqlException : Incorrect datetime value

Hai I have to add details from one table to another which should be within to dates. These dates are read from text boxes.
But i'm getting Error:
"An exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll but was not handled in user code
Additional information: Incorrect datetime value: '11/25/2015 12:00:00 AM' for column 'debissuedate' at row 1"
The first table is t_bondridapp with fields : id,cancode,canname,debissuedate...etc
And I have to copy from this table to new one named as bondlocal with fields :
bondid,cancode,canname,bonddate.
I've used the code
public class DBConnection
{
private DBConnection()
{
}
private string dbname = string.Empty;
public string DBName
{
get { return dbname;}
set { dbname = value;}
}
public string Password { get; set; }
private MySqlConnection mycon = null;
public MySqlConnection Connection
{
get { return mycon; }
}
private static DBConnection _instance = null;
public static DBConnection Instance()
{
if(_instance==null)
_instance=new DBConnection();
return _instance;
}
public bool IsConnect()
{
bool result = true;
if(mycon==null)
{
if (String.IsNullOrEmpty(dbname))
result = false;
string constr = string.Format("server=localhost;user id=root;password=mysql;database=pnys;",dbname);
mycon = new MySqlConnection(constr);
mycon.Open();
result = true;
}
return result;
}
public void Close()
{
mycon.Close();
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
MySqlDateTime fdate =new MySqlDateTime(DateTime.Parse(TextBox3.Text));
MySqlDateTime sdate = new MySqlDateTime(DateTime.Parse(TextBox4.Text));
var dbCon = DBConnection.Instance();
dbCon.DBName = "pnys";
if (dbCon.IsConnect())
{
string query = "INSERT INTO bondlocal (cancode,canname,bonddate) SELECT t_bondridapp.cancode,t_bondridapp.canname,t_bondridapp.debissuedate FROM t_bondridapp WHERE debissuedate>='" + fdate + "'AND debissuedate<='" + sdate + "'";
MySqlCommand cmd = new MySqlCommand(query, dbCon.Connection);
cmd.ExecuteNonQuery();
}
Server.Transfer("ReportBonds.aspx");
}
Pls Help Me...
Basically, the problem is how you're passing parameters into the database. You shouldn't need to create a MySqlDateTime yourself - just use parameterized SQL and it should be fine:
// TODO: Use a date/time control instead of parsing text to start with
DateTime fdate = DateTime.Parse(TextBox3.Text);
DateTime sdate = DateTime.Parse(TextBox4.Text);
string query = #"INSERT INTO bondlocal (cancode,canname,bonddate)
SELECT t_bondridapp.cancode,t_bondridapp.canname,t_bondridapp.debissuedate
FROM t_bondridapp
WHERE debissuedate >= #fdate AND debissuedate <= #sdate";
using (var command = new MySqlCommand(query, dbCon))
{
command.Parameters.Add("#fdate", MySqlDbType.Datetime).Value = fdate;
command.Parameters.Add("#sdate", MySqlDbType.Datetime).Value = sdate;
command.ExecuteNonQuery();
}
Basically, you should never specific values within SQL by just using string concatenation. Parameterized SQL prevents SQL injection attacks and conversion issues, and improves code readability.
(As an aside, I would urge you to ditch your current connection sharing, and instead always create and open a new MySqlDbConnection and dispose of it at the end of your operation - rely on the connection pool to make it efficient.)

Logged in as : (your name) passed to another form using c# passing parameters

When I use Microsoft C# windows forms application. I tried to run the login program then when I clicked the login button. it displays "Logged in as : (name of user)". then in the next form show I didn't see my name on the other form even I use some codes for the passing parameters like
Form1(codes)
public partial class Form1 : Form
{
string namesl;
OleDbCommand cm;
OleDbConnection cn;
OleDbDataReader dr;
string connection = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source =|DataDirectory|\10watts.accdb";
public Form1( )
{
cn = new OleDbConnection(connection);
cn.Open();
InitializeComponent();
}
public void getName(string _getName)
{
_getName = dr.GetValue(2).ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox2.PasswordChar = '*';
cn = new OleDbConnection(connection);
cn.Open();
progressBar1.Visible = false;
}
private void btnLog_Click(object sender, EventArgs e)
{
if (textBox1.Text == String.Empty || textBox2.Text == String.Empty)
{
MessageBox.Show("Missing Requirement Field!", "Unable to Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
timer1.Start();
progressBar1.Visible = true;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
progressBar1.Value = progressBar1.Value + 5;
lblPercent.Text = progressBar1.Value + "%";
if (progressBar1.Value == 100)
{
timer1.Enabled = false;
string sql = #"Select * from tblUser where Username like '" + textBox1.Text + "'and Password like '" + textBox2.Text + "'";
cm = new OleDbCommand(sql, cn);
dr = cm.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
MessageBox.Show("Entered Logged In as : " + dr.GetValue(2).ToString(),"Successfully Logged in",MessageBoxButtons.OK,MessageBoxIcon.Information);
_getName = dr.getValue(2).toString();
Form2 frm2 = new Form2(this);
frm2.Show();
}
else
{
}
}
else
{
}
}
}
Form 2
public partial class Form2 : Form
{
OleDbCommand cmd;
OleDbConnection cn;
OleDbDataReader dr;
public string names;
string connection = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source =|DataDirectory|\10watts.accdb";
public Form2( )
{
cn = new OleDbConnection(connection);
cn.Open();
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
this.loggedinform.getName(names);//this thing i worried about :/
}
This whole code that I tried when I was using c# application.
Need your opinions or codes that may use for passing parameters :)

Xamarin SqlConnection throwing error Input string was not in correct format

Im trying to connect my mobile to PC database using this code:
string cString = #"Persist Security Info=False;Integrated Security=false;Initial Catalog=myDB;server=192.168.1.11,1433\SqlExpress";
SqlConnection connection = new SqlConnection(cString);
connection.Open();
using (SqlCommand cmd = new SqlCommand("SELECT name1 FROM Product", connection))
{
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
t.Text += rdr.GetString(rdr.GetOrdinal("name1")) + System.Environment.NewLine;
}
}
}
Unfortunately I'm getting error: "Input string was not in the correcr format", source: mscorlib.
I know that there's error in connectionString (throwin exception after connection.Open())
Any ideas? Thanks in advance!
your current Culture is not in correct format
protected override void OnResume()
{
base.OnResume();
//Here you would read it from where ever.
var userSelectedCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = userSelectedCulture;
}