Generate Report Using Report Viewer in asp.net - c#

I want to display report in my admin panel when I select the sales person from dropdown list in my page but nothing is displayed and display some error here.
Below is my code:
protected void BtnViewReport_Click(object sender, EventArgs e)
{
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/SalesPerson.rdlc");
DataSet ds = GetData("select * from customer_new where salesperson in (select +
email from Registration where name='" + ddsalesperson.SelectedValue.ToString() +
"')");
ReportDataSource datasource = new ReportDataSource("customer_new",ds.Tables[0]);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(datasource);
}
private DataSet GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds, "customer_new");
return ds;
}
}
}
}
and I get the following error :
A data source instance has not been supplied for the data source
'DataSet1'.

That error means that the dataset "DataSet1" has not recieved data. I'd take a guess that the dataset "customer_new" doesn't actually exist, or if it does you have 2 datasets, one of which you may or may not be using.
Try:
ReportDataSource datasource = new ReportDataSource("DataSet1",ds.Tables[0]);

Related

filter ReportViewer data?

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.

Updating Data GridView using the selected value inside the dropdown list

I have a dropdownlist and a gridview.. Dropdown list contains the list of the tables I have in my database. What I want is, when I select a particular table name from the dropdownlist, I want all the columns and data inside that particular table display inside the gridview.
This is my code...
Code for displaying the list of tables inside the dropdown is success.. But to bind the columns and data inside the gridview is not success..
Please Help me...
protected void Page_Load(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=Employee;Integrated Security=True"))
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT table_name FROM INFORMATION_SCHEMA.TABLES", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "table_name";
DropDownList1.DataValueField = "table_name";
DropDownList1.DataBind();
con.Close();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=Employee;Integrated Security=True"))
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM INFORMATION_SCHEMA.columns where table_name='+ DropDownList1.selecteditem.text +'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
//+DropDownList1.selecteditem.text +
GridView1.DataBind();
con.Close();
}
}
This is a bad idea on several levels. First you are wide open to SQL injection. Second, you are giving everyone full view access to every column and row of every table.
But the reason you are not getting data is because the select string does not make sense. It should look like this
"SELECT * FROM INFORMATION_SCHEMA.columns where table_name='" + DropDownList1.SelectedValue + "'"
But this is how a proper sql connection should look like.
//create a new datatable
DataTable dt = new DataTable();
//create the string that hold the query including token
string query = "SELECT * FROM INFORMATION_SCHEMA.columns where table_name = #TableName";
//create a new database connection
using (SqlConnection connection = new SqlConnection(ConnectionString))
using (SqlCommand command = new SqlCommand(query, connection))
{
command.CommandType = CommandType.Text;
//replace the token with the correct value
command.Parameters.Add("#TableName", SqlDbType.VarChar).Value = DropDownList1.SelectedValue;
//open the connection
connection.Open();
//load the data of the select into the datatable
dt.Load(command.ExecuteReader());
//bind the datatable to the gridview
GridView1.DataSource = dt;
GridView1.DataBind();
//but you can also skip the datatable and bind directly to the gridview
GridView1.DataSource = command.ExecuteReader();
GridView1.DataBind();
}

How to databind more than 1 table to a listview

Right now this is my code, it only databinds the last mentioned table.
I have 2 tables the "Products" and "Phone", and I would like to databind both of them to the Listview1.
if (!IsPostBack)
{
ListView1.DataSource = this.GetDatatable2();
ListView1.DataSource = this.GetData();
ListView1.DataBind();
}
private DataSet GetData()
{
string conString = ConfigurationManager.ConnectionStrings["CARP"].ConnectionString;
string query = "SELECT * FROM Produtos WHERE Id= '" + Request.QueryString["Id"] + "'";
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
private DataSet GetDatatable2()
{
string conString = ConfigurationManager.ConnectionStrings["CARP"].ConnectionString;
string query = "SELECT * FROM Phone WHERE Id= '" + Request.QueryString["Id"] + "'";
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
The reason why it only binds the second table is, that you don't add the tables but set them as data source.
To add something you use the operator += instead of =.
But in this situation this doesn't seem appropriate. I think you should merge both tables at first. With databinding the ListView would generate the columns automatically. Therefore you would need the same column headers in both tables. Anything else isn't meaningful at all.
You could use a SQL UNION and select both tables in one query, too.

How can i refresh my data in datagridview after in enter new data

how can i refresh my datagridview here my code to view my data in datagridview but every time i enter new data i have to reopen my program to see the new data
using (IDbConnection dbconnection = new SQLiteConnection(conn))
{
dbconnection.Open();
SQLiteDataAdapter dataadapter = new SQLiteDataAdapter("SELECT * FROM tbl_Sample",conn);
DataSet ds = new System.Data.DataSet();
dataadapter.Fill(ds,"Info");
dataGridView1.DataSource = ds.Tables[0];
}
If you are using a DataSet or a DataTable as a datasource for the DataGridView, you can try something like this:
private void LoadDataToDgv()
{
using (IDbConnection dbconnection = new SQLiteConnection(conn))
{
dbconnection.Open();
SQLiteDataAdapter dataadapter = new SQLiteDataAdapter("SELECT * FROM tbl_Sample",conn);
DataSet ds = new System.Data.DataSet();
dataadapter.Fill(ds,"Info");
dataGridView1.DataSource = ds.Tables[0];
}
}
And then after you have added the new data to your tables, fetch the new dataset and reload the grid with the new rows:
//Code to add new data to Sql Table
LoadDataToDgv();
I would however recommend that you create a Class for the type of item you are binding to the grid (if you haven't created one yet) and the use BindingList to do the same. This question describes well how to use it for binding it as a source for DataGridViews.
i put the refresh function every time i put new data in the database so the datagridview will refresh every new data i enter
public void AddData()
{
using (IDbConnection dbconnection = new SQLiteConnection(conn))
{
dbconnection.Open();
using (IDbCommand dbCmd = dbconnection.CreateCommand())
{
string sqlQuery = "INSERT INTO tbl_sample (Name,Age) VALUES('" + textBox1.Text + "','" + textBox2.Text + "')"; ;
dbCmd.CommandText = sqlQuery;
using (IDataReader reader = dbCmd.ExecuteReader())
{
SQLiteDataAdapter dataadapter = new SQLiteDataAdapter("SELECT * FROM tbl_Sample", conn);
DataSet ds = new System.Data.DataSet();
dataadapter.Fill(ds, "Info");
dataGridView1.DataSource = ds.Tables[0];
dbconnection.Close();
}
}
}
}

How to solve a datasource error in Microsoft report viewer?

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

Categories