How add dataset into report c# - c#

Hello I have problem on create report with crystal report c#,
I should insert the result of this SqlAdapter in a c # report , but do not know how to do
String Query = "SELECT Utente.LogoAzienda,Preventivo.DataInserimento,Preventivo.RiferimentoInterno,Preventivo.Testata,Preventivo.Chiusura,Cliente.Titolo,Cliente.RagioneSociale,Cliente.Indirizzo,Cliente.Cap,Cliente.Citta,Cliente.Provincia FROM Preventivo inner join Cliente on Cliente.IdCliente = Preventivo.IdCliente inner join Utente on Preventivo.UtenteCreazione = Utente.Username";
SqlConnection conn = db.apriconnessione();
DataStampaPreventivoCompleto d = new DataStampaPreventivoCompleto();
SqlDataAdapter da = new SqlDataAdapter(Query, conn);
da.Fill(d, d.Tables[0].TableName);

Here is an example to bind dataset to crystal report:
private void CrystalFormView_Load(object sender, EventArgs e)
{
string connection = ConfigurationManager.ConnectionStrings["sqlbill"].ConnectionString;
string provider = ConfigurationManager.ConnectionStrings["sqlbill"].ProviderName;
SqlConnection con = new SqlConnection(connection);
SqlDataAdapter sda = new SqlDataAdapter("select product as Product,productid as ProductId,quantity as Quantity from productdata", con);
DataSet ds = new DataSet();
sda.Fill(ds);
ds.Tables[0].TableName = "BILLTEST";
BillCrystalReport bill = new BillCrystalReport();
bill.SetDataSource(ds);
bill.VerifyDatabase();
crystalReportViewer1.ReportSource = bill;
crystalReportViewer1.RefreshReport();
}
For more, please check this link: http://www.codeproject.com/Tips/754037/Bind-Crystal-Reports-with-Dataset-or-Datatable

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.

Fill combobox from SQL database with specific parameter

I have problem to get specific value from sql server with parameter can anybody explain me why it works on winfom but not on wpf and how i can fix it
my code:
private void UpdateItems()
{
COMBOBOX1.Items.Clear();
SqlConnection conn = new SqlConnection(Properties.Settings.Default.constring.ToString());
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM CLIENT where cod_cli='some_specific_string'", conn);
DataSet ds = new DataSet();
da.Fill(ds, "CLIENT");
COMBOBOX1.ItemsSource = ds.Tables[0].DefaultView;
COMBOBOX1.DisplayMemberPath = ds.Tables[0].Columns["FR"].ToString();
COMBOBOX1.SelectedValuePath = ds.Tables[0].Columns["FC"].ToString();
}
The program when execute this function crash with error:
System.Data.SqlClient.SqlException: 'Invalid column name
'some_specific_string'.'
the solution is
SqlConnection sqlConnection = new SqlConnection(Properties.Settings.Default.constring.ToString());
{
SqlCommand sqlCmd = new SqlCommand("SELECT * FROM CLIENTS where cod_cli=#cod", sqlConnection);
sqlCmd.Parameters.AddWithValue("#cod", cod_cli.Text);
sqlConnection.Open();
SqlDataReader sqlReader = sqlCmd.ExecuteReader();
while (sqlReader.Read())
{
COMBOBOX1.Items.Add(sqlReader["FR"].ToString());
}
sqlReader.Close();
}
The query doesn't recognize string as parameter but adding as SQL parameter it works.
SqlConnection conn = new SqlConnection(Properties.Settings.Default.constring.ToString());
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM CLIENT where cod_cli='some_specific_string'", conn);
DataSet ds = new DataSet();
da.Fill(ds, "CLIENT");
//Populate the combobox
COMBOBOX1.ItemsSource = ds.Tables[0].DefaultView;
COMBOBOX1.DisplayMemberPath = "FR";
COMBOBOX1.SelectedValuePath = "FC";
where "FR" and "FC" are existing columns in your SELECT Query.

Filter by typing in textbox

I'm using the code below in order to bind my datagridview and the select statement in order to get the records I need.
Everything works fine in these lines of code, but I would like to automatically filter the records when the user types something into textbox and when deletes the data, the records return to the initial state, when the form is loading. Could anyone give me some tips?
Here is my code:
public nir()
{
InitializeComponent();
nir_load();
}
void nir_load()
{
string cs = "Data Source=IS020114\\CODRINMA;Initial Catalog=gcOnesti;Integrated Security=True";
string select = "select p.cod as Numar, p.data as Data, p.doc_cod as NrDocFurnizor, g.nume as Gestiune, c.nume as Furnizor, p.validat as Validat, p.facturat as Contat from primar p inner join gestiuni g on p.part2=g.gest_id inner join cf c on p.part1=c.cf_id where p.tip=2";
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand(select, con);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
BindingSource bs = new BindingSource();
bs.DataSource = dt;
dataGridView1.DataSource = bs;
}
}
private void btnSearch_Click(object sender, EventArgs e)
{
string cs = "Data Source=IS020114\\CODRINMA;Initial Catalog=gcOnesti;Integrated Security=True";
string select = "select p.cod as Numar, p.data as Data, p.doc_cod as NrDocFurnizor, g.nume as Gestiune, c.nume as Furnizor, p.validat as Validat, p.facturat as Contat from primar p inner join gestiuni g on p.part2=g.gest_id inner join cf c on p.part1=c.cf_id where cod='" +txtnr.Text+"' and data='" + dtpData.Value.ToString("yyyy-MM-dd") +"' and p.tip=2";
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand(select, con);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
BindingSource bs = new BindingSource();
bs.DataSource = dt;
dataGridView1.DataSource = bs;
}
}
I used private void txtNumber_KeyUp(object sender, KeyEventArgs e) event and it worked fine.

Generate Report Using Report Viewer in asp.net

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]);

C# Reporting via query

hi everyone i have this code here
SqlConnection con = new SqlConnection(Properties.Settings.Default.StoreDBConnection);
SqlDataAdapter da = new SqlDataAdapter();
private void Form1_Load(object sender, EventArgs e)
{
String test = DateTime.Now.ToString("yyyy-MM-dd");
var dept = Properties.Settings.Default.GiftDepartment;
MessageBox.Show(""+test+"");
SqlCommand cmd = new SqlCommand("SELECT DeptNo,DeptSales,DeptCustomers FROM StoreSalesDetail where DeptNo="+dept+" and ProcDate>="+test+"", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet1 ds = new DataSet1();
da.Fill(ds);
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", ds.Tables[1]));
this.reportViewer1.RefreshReport();
}
Sample data in my table
2013-05-03 00:00:00.000,2,120.0000,1,1
2013-05-04 00:00:00.000,2,50.0000,1,1
test returns the date 2013-05-04 so why does my report return both date in the >= query?
try using datediff for more precision. Do wrap input value [test] in single quotes.
SqlCommand cmd = new SqlCommand("SELECT DeptNo,DeptSales,DeptCustomers FROM StoreSalesDetail where DeptNo="+dept+" and datediff(d, '" + test + "', ProcDate) >=0", con);

Categories