When I type in my search box my Data grid view doesn't show the data that i typed in even though its in the database. This is my codes for the search box
MySqlConnection con = new MySqlConnection(MyConnectionString);
con.Open();
MySqlDataAdapter adapt = new MySqlDataAdapter();
con = new MySqlConnection(MyConnectionString);
adapt = new MySqlDataAdapter("SELECT * from tblregister where FirstName like '" + textBox1.Text + "%'", con);
DataTable ds = new DataTable();
DataSet dt = new DataSet();
adapt.Fill(dt);
dataGridView1.DataSource =ds;
Related
I'm trying to make a button to load and filter my ReportViewer by name but the output is data source instance not supplied. Here is my code:
private void btnReport_Click(object sender, EventArgs e)
{
String sql = "Select * from practise Where name ='" + textBox1.Text + "'";
SqlConnection con = new SqlConnection(ConnectionString);
SqlDataAdapter adp = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
adp.Fill(ds);
ReportDataSource rds = new ReportDataSource("practise", ds.Tables[0]);
reportViewer2.ProcessingMode = ProcessingMode.Local;
reportViewer2.LocalReport.ReportPath = "Report1.rdlc";
if (ds.Tables[0].Rows.Count > 0)
{
reportViewer2.LocalReport.DataSources.Clear();
reportViewer2.LocalReport.DataSources.Add(rds);
reportViewer2.RefreshReport();
}
This could be simple. Please check that your dataset name in the report is not "DataSet1" by default. Also, make sure that the path for report is correct. Here is a working example:
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DemoDB"].ConnectionString))
{
String sql = "Select * from practise Where name ='" + textBox1.Text + "'";
SqlDataAdapter adp = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
adp.Fill(ds);
ReportDataSource rds = new ReportDataSource("DataSet1", ds.Tables[0]);
reportViewer2.ProcessingMode = ProcessingMode.Local;
reportViewer2.LocalReport.ReportPath = "Report1.rdlc";
if (ds.Tables[0].Rows.Count > 0)
{
reportViewer2.LocalReport.DataSources.Clear();
reportViewer2.LocalReport.DataSources.Add(rds);
reportViewer2.RefreshReport();
}
}
I hope that would help someone.
Hi i want to save edits in DataGriView to datatable , i tried that code but an error shows 'System.ArgumentOutOfRangeException: 'The index was out of range. It must not be negative and must be smaller than the size of the collection.
Parameter name: index '' help
private void button11_Click(object sender, EventArgs e)
{
con.Open();
String query = "SELECT * FROM Taom";
SqlDataAdapter SDA = new SqlDataAdapter(query, con);
DataTable dt1 = new DataTable();
DataSet ds1 = new DataSet();
DataTable dt = new DataTable();
dataGridView1.DataSource = dt1;
SDA.Fill(dt);
dataGridView1.Rows[2].Cells[2].Value = Math.Atan((Convert.ToDouble(dataGridView1.Rows[5].Cells[2].Value)) / (Convert.ToDouble(dataGridView1.Rows[6].Cells[2].Value)));
dataGridView1.Rows[3].Cells[2].Value = Math.Atan((Convert.ToDouble(dataGridView1.Rows[7].Cells[2].Value)) / (Convert.ToDouble(dataGridView1.Rows[8].Cells[2].Value)));
ds1.Tables.Add(dt);
con.Close();}
I changed my code to that code no errors showed after i run values on datagridview change but no changes in datatable !!!
string query = "SELECT * FROM [dbo].[Taom]";
SqlConnection conn = new SqlConnection(#"Data Source=STE-P0024818PW;Initial Catalog=test;Integrated Security=True");
conn.Open();
SqlDataAdapter SDA = new SqlDataAdapter(query, conn);
DataSet ds1 = new DataSet();
DataTable dt = new DataTable();
SDA.Fill(dt);
dt.Rows[0]["Contents"] = "98"; //Before it was 10
dt.Rows[1]["Contents"] = "99"; //Before it was 11
ds1.Tables.Add(dt);
conn.Close();
Fill the right value inside your table and after pass the table rightly filled in your datasource, don't change this directly in the gridview like:
I try myself and it works:
private void Run()
{
string query = "SELECT * FROM dbo.[Anrufthema]";
SqlConnection conn = new SqlConnection("MyConnectionString");
conn.Open();
SqlDataAdapter SDA = new SqlDataAdapter(query, conn);
DataSet ds1 = new DataSet();
DataTable dt = new DataTable();
SDA.Fill(dt);
dt.Rows[0]["Anrufthema"] = "98"; //Before it was 10
dt.Rows[1]["Anrufthema"] = "99"; //Before it was 11
ds1.Tables.Add(dt);
conn.Close();
}
My Result, it works !
This is my Code to View Data from DataBase into Grid View. Can Anyone help me how to update data into database through updating data in Same GridView
Heres My Code
string myConnection = "datasource=localhost;port=3306;username=root;password=2905";
MySqlConnection myConn = new MySqlConnection(myConnection);
MySqlCommand cmdDataBase = new MySqlCommand("select * from mydb.stud_info where stud_id = '" + this.studentid_txt.Text + "'; ", myConn);
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDataBase;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
grid1.DataSource = bSource;
sda.Update(dbdataset);
this is a faster method just one line and it will update everything in your datagridview
this.vesselTableTableAdapter.Update(this.dockMasterDbDataSet.VesselTable);
I am using the following code:
string getinputs = "SELECT ir.plu_code PLU_Code,ir.barcode Barcode,ir.product_name Product_Name FROM inventory_register ir,inventory_value iv WHERE ir.dept_id='" + textBox1.Text + "' AND ir.barcode = iv.barcode";
connection.Open();
MySqlDataAdapter adapter = new MySqlDataAdapter(getinputs, connection);
MySqlCommandBuilder cmdbuilder = new MySqlCommandBuilder(adapter);
DataTable dt = new DataTable();
adapter.Fill(dt);
BindingSource bindsrc = new BindingSource();
DataGridView datagridv = new DataGridView();
bindsrc.DataSource = dt;
datagridv.DataSource = bindsrc;
connection.Close();
I have three predefined columns in datagridview1 say 'PLU_Code', 'Barcode' and 'Product_Name'.
I want to display the result (more than one row) of the select query in the datagridview. But I am not getting any.
How do I achieve this?
Try this code:
string getinputs = "SELECT ir.plu_code PLU_Code,ir.barcode Barcode,ir.product_name Product_Name FROM inventory_register ir,inventory_value iv WHERE ir.dept_id='" + textBox1.Text + "' AND ir.barcode = iv.barcode";
connection.Open();
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(getinputs, connection);
MySqlCommandBuilder commandBuilder = new MySqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
dataAdapter.Fill(table);
datagridview1.DataSource = table;
connection.Close();
I got the result. I achieved two empty rows on using #user4340666 code. when i scrolled the grid i found the set of data's been added as new columns leaving predefined column cells empty. so i just cleared the columns.
dataGridView1.Columns.Clear();
string getinputs = "SELECT ir.plu_code PLU_Code,ir.barcode Barcode,ir.product_name Product_Name FROM inventory_register ir,inventory_value iv WHERE ir.dept_id='" + textBox1.Text + "' AND ir.barcode = iv.barcode";
connection.Open();
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(getinputs, connection);
MySqlCommandBuilder commandBuilder = new MySqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
dataAdapter.Fill(table);
datagridview1.DataSource = table;
connection.Close();
Try this code:
string getinputs = "Select Query";
connection.Open();
MySqlCommandBuilder cmdbuilder = new MySqlCommandBuilder(getinputs,connection);
MySqlDataAdapter adapter = new MySqlDataAdapter(cmdbuilder);
DataTable dt = new DataTable();
adapter.Fill(dt);
DataGridView datagridv = new DataGridView();
datagridv.DataSource = dt;
datagridv.DataBind();
connection.Close();
i am having Report page in my windows project .In that i included microsoftReportViewer . In that page two combobox is there .items in 1st combobox are:
FirmDetails
ChittyHolding
LoanDetails.
Corresponding to selection this items in combobox1, items in combobox will change .I need report According to this values in 2 combbox.
My code is like
if (cbReprt.Text == "FirmDetails")
{
if (cbGeneral.Text == "AllFirmDetails")
{
reportViewer1.RefreshReport();
SqlConnection con = new SqlConnection("Data Source=202.88.231.102;Initial Catalog=dbs_Merchant;Persist Security Info=True;User ID=sa;Password=abc123*");
con.Open();
allfirmdetails ds = new allfirmdetails();
string str = "Select * from View_2";
SqlCommand cmd = new SqlCommand(str, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
DataTable dt = new DataTable();
da.Fill(dt);
reportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
reportViewer1.LocalReport.ReportPath = "F:\\MerchantAssociation\\MerchantAssociation\\Report7.rdlc";
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("allfirmdetails_View_2", dt));
reportViewer1.RefreshReport();
}
else
{
reportViewer1.RefreshReport();
SqlConnection con = new SqlConnection("Data Source=202.88.231.102;Initial Catalog=dbs_Merchant;Persist Security Info=True;User ID=sa;Password=abc123*");
con.Open();
allfirmdetails ds1 = new allfirmdetails();
string str1 = "Select * from View_2 where FirmName='" + cbGeneral.Text + "'";
SqlCommand cmd1 = new SqlCommand(str1, con);
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
da1.Fill(ds1);
DataTable dt1 = new DataTable();
da1.Fill(dt1);
reportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
reportViewer1.LocalReport.ReportPath = "F:\\MerchantAssociation\\MerchantAssociation\\firmwise.rdlc";
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("allfirmdetails_View_2", dt1));
reportViewer1.RefreshReport();
}
}
else if (cbReprt.Text == "ChittyDetails")
{
if (cbGeneral.Text == "AllChittyDetails")
{
reportViewer1.RefreshReport();
SqlConnection con = new SqlConnection("Data Source=202.88.231.102;Initial Catalog=dbs_Merchant;Persist Security Info=True;User ID=sa;Password=abc123*");
con.Open();
ChittyDetails ds2 = new ChittyDetails();
string str2 = "Select * from View_1";
SqlCommand cmd2 = new SqlCommand(str2, con);
SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
da2.Fill(ds2);
DataTable dt2 = new DataTable();
da2.Fill(dt2);
reportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
reportViewer1.LocalReport.ReportPath = "F:\\MerchantAssociation\\MerchantAssociation\\allchitty.rdlc";
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("ChittyDetails_View_1", dt2));
reportViewer1.RefreshReport();
}
First I select chittyholding details. Then I got the report. Then I select chitty details report, I got the error like
**An error occured during local report processing .A data sourcr instance has not been supplied for the data source "ChittyHolding_View_7"**. If I close and run the project again ,then that firmdetails selection will work.But another will not work.It means Only for one selection I am getting the report. Why? Please solve this error
Try to call reportViewer1.Reset(); instead of reportViewer1.Refresh(); at the beginning of every condition