Error: The operation not method is not implemented - c#

I wish to load the datatable from database to data grid view,but I get the error saying "The operation & method is not implemented". I do review the video over here: http://www.youtube.com/watch?v=Sm5mxkytfWk; This is my very first time to do this, hope to get to solve it.
Below is my code:
private void button1_Click(object sender, EventArgs e)
{
string constring = #"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\oo\Documents\Data.mdf;Integrated Security=True;Connect Timeout=30";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand("SELECT* from Data.itemInfo;", conDataBase);
try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDataBase;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
dataGridView1.DataSource = bSource;
sda.Update(dbdataset);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

MySqlCommand cmdDataBase = new MySqlCommand("SELECT* from Data.itemInfo;", conDataBase);
yours Sql query contain extra semicolen
from
SELECT* from Data.itemInfo;
to
SELECT* from Data.itemInfo
How to: Bind Data to the Windows Forms DataGridView Control

Related

c# Select sql database data using datetimepicker set with date range

I am trying to make c# program, where I have to make a database report to be previewed at the datagridview. Data will be selected using the datetimepicker. I have written the code, it works but then if the date selected is of different months. No records appear
void FilterDBbtnClick(object sender, EventArgs e)
{
MySqlConnection conn = new MySqlConnection();
conn = new MySqlConnection(cs);
string data = "SELECT `Date`, `Process`, `Actual`, `Target` FROM `database` WHERE `Date` BETWEEN '"+this.fromDatePicker.Value+"' AND '"+this.toDatePicker.Value+"' order by `Date` desc";
MySqlCommand cmd = new MySqlCommand(data, conn);
cmd.Connection.Open();
try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmd;
DataSet dt = new DataSet();
sda.Fill(dt);
BindingSource bsource = new BindingSource();
bsource.DataSource = dt;
mondeDataTable.DataSource = dt.Tables[0];
sda.Update(dt);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
cmd.Connection.Close();
}
please help me check my code and tell me what might be wrong or missing.
Use profiler to check the query that hits the DB. I suspect it's a date formatting issue.
Maybe this question can help you with logging the queries that hit the database:
Hope this helps:
Select * from [Table] where StartDate between '06/13/2016' and '10/13/2016'
The above query fetches records between months 06 and 10. Make sure that string in the data variable is in the above format. Also the column type in the database is date.
Check and remove special characters, if any.
Mark this as answer if you find this useful.
Try this for your select query. I have changed one of your select variable because it ambiguous for the Date datatype.
void FilterDBbtnClick(object sender, EventArgs e)
{
MySqlConnection conn = new MySqlConnection();
conn = new MySqlConnection(cs);
//string data = "SELECT `Date`, `Process`, `Actual`, `Target` FROM `database` WHERE `Date` BETWEEN '"+this.fromDatePicker.Value+"' AND '"+this.toDatePicker.Value+"' order by `Date` desc";
//Changed query for getting data from DB according to the date
string data = "SELECT CreatedDate, Process, Actual, Target FROM database WHERE DATE_FORMAT(CreatedDate,'%Y-%m-%d') BETWEEN '"+this.fromDatePicker.Value.ToString("yyyy-MM-dd")+"' AND '"+this.toDatePicker.Value.ToString("yyyy-MM-dd")+"' order by CreatedDate desc";
MySqlCommand cmd = new MySqlCommand(data, conn);
cmd.Connection.Open();
try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmd;
DataSet dt = new DataSet();
sda.Fill(dt);
BindingSource bsource = new BindingSource();
bsource.DataSource = dt;
mondeDataTable.DataSource = dt.Tables[0];
sda.Update(dt);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
cmd.Connection.Close();
}

Use SQL queries virtual console to do any thing on a SQL Server database

How can I use a rich textbox and some pre-connected string to execute direct Transact-SQL queries and get results into a datagridview?
My GUI form is like this:
I want to use any SQL queries in this rich textbox and get result in data grid view
I using this code for query and fill data gridview
SqlConnection c = new SqlConnection(#"Data Source=.;Initial Catalog=db3;Integrated Security=True");
private void button1_Click(object sender, EventArgs e)
{
SqlDataAdapter a = new SqlDataAdapter("select * from phone", c);
DataTable t = new DataTable();
a.Fill(t);
dataGridView1.DataSource = t;
}
So how do I modify it to this?
SqlConnection c = new SqlConnection(#"Data Source=.;Initial Catalog=db3;Integrated Security=True");
private void button1_Click(object sender, EventArgs e)
{
SqlDataAdapter a = new SqlDataAdapter(Textbox1.Text, c);
DataTable t = new DataTable();
a.Fill(t);
dataGridView1.DataSource = t;
}
I also used Pravin Deshmukh code in another button and get error:
Update 3: used suggested code and got a reference error
private void button3_Click(object sender, EventArgs e)
{
string SqlString = textBox2.Text; // here you can have your user query from textbox
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Data Source=.;Initial Catalog=db3;Integrated Security=True"].ConnectionString.ToString());
conn.Open();
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(SqlString, conn);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
conn.Close();
cmd.Dispose();
}
Error:
Try this
string SqlString= "your sql query";
SqlConnection conn = new SqlConnection(#"Data Source=.;Initial Catalog=db3;Integrated Security=True");
conn.Open();
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(SqlString, conn);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
conn.Close();
cmd.Dispose();
Set datagridview generate columns automatic
EDIT : or
string SqlString= textbox1.Text; // here you can have your user query from textbox

There's always an error message saying "The SelectCommand property has not been initialized before calling 'Fill'."

private void toolStripButton1_Click(object sender, EventArgs e)
{
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString =
"Dsn=mdc;" +
"Uid=root;" +
"Pwd=;";
OdbcCommand cmd = new OdbcCommand("select * from tbl_delivery");
cmd.CommandType = CommandType.Text;
DataSet dt = new DataSet();
OdbcDataAdapter ds = new OdbcDataAdapter();
ds.Fill(dt);
tbl_deliveryDataGridView.DataSource = dt;
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
there is always an error everytime i am going to run the program, i don't know where is the error. i tried editing the code yet still the error keeps on popping out.
You getting error because you haven't assign OdbcCommand to OdbcDataAdapter and trying to execute Fill method. you should assign OdbcCommand to OdbcDataAdapter like this
OdbcDataAdapter ds = new OdbcDataAdapter(cmd,conn);
And then try to Fill DataTable
conn.Open();
DataSet dt = new DataSet();
OdbcDataAdapter ds = new OdbcDataAdapter();
ds.Fill(dt);
tbl_deliveryDataGridView.DataSource = dt;

fill datagridview with content from .mdf

I am trying to fill a datagridview with content from a .mdf SQL Server database file (C# Windows Forms app)...
private void Companies_Load(object sender, EventArgs e)
{
load_table();
}
void load_table()
{
String DATA = Application.StartupPath + #"\data.mdf";
string constring = #"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + DATA + ";Integrated Security=True";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand("select * from Companies ;", conDataBase);
try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDataBase;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
dataGridView1.DataSource = bSource;
sda.Update(dbdataset);
}
catch (Exception uu)
{
MessageBox.Show(uu.Message);
}
}
I get nothing. DataGridView is empty. No errors...
Table name: Companies with 4 rows and 1 column...
I tried SQL statements like
select * from dbo.Companies ;
... still nothing
I changed data.mdf connection to full path c:/etc/etc ...
No luck.
Any simple solution is welcome :)
.mdf is a SQL Server data file, therefore you need to use the SQL Server client library, e.g. SqlConnection, SqlCommand and SqlDataAdapter.
What you're using now (MySqlConnection, MySqlCommand, MySqlDataAdapter) is for MySQL and won't work on a (Microsoft) SQL Server data file.

Fill: SelectCommand.Connection property has not been initialized error

I am trying to display the data in a datagridview in my c# windows forms project. I keep getting this error
"Fill: SelectCommand.Connection property has not been initialized"
is there anything i'm doing wrong here:
private void Form1_Load(object sender, EventArgs e)
{
try
{
SqlCommand db = new SqlCommand("select * from Tbls");
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = db;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bsource = new BindingSource();
bsource.DataSource = dbdataset;
dataGridView1.DataSource = bsource;
sda.Update(dbdataset);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
You have to assign an SqlConnection object to your SqlCommand object.
db.Connection = conn;
Where conn is your SqlConnection object.
Initialize your SqlConnection object like so:
var conn = new SqlConnection(/*Connection String*/);
You create a new command:
SqlCommand db = new SqlCommand("select * from Tbls");
But, what SqlConnection are you using? You're not. You need to create a command with a connection. Generally you do this FROM a SqlConnection See: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.createcommand(v=vs.110).aspx

Categories