C# Reporting via query - c#

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

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.

How can I get top 3 value with selection of combobox in C# from database

I want to select combobox items and want to show top 3 possibilities on datagridview according to my selections in c# 2010 express. I created database using C#.
private void add_button_Click(object sender, EventArgs e)
{
SqlConnection sqlcon = new SqlConnection();
sqlcon.ConnectionString = #"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Mystudies\C#\SQL Database\MakerSelectionDatabase.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
// insert Logic
sqlcon.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from makerinfo Where [Equipment Type] = '" + equipmenttype_combobox.Text.Trim() + "' and Plant='" + plant_combobox.Text.Trim() + "'", sqlcon);
// I could not combine this code to combobox selection.
// SqlDataAdapter eval = new SqlDataAdapter("select top 3 safety from makerinfo order by Safety desc", sqlcon);
DataTable dt = new DataTable();
SqlDataAdapter db = new SqlDataAdapter();
sda.Fill(dt);
datagridview.DataSource = dt;
sqlcon.Close();
}

Search Box not working

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;

How add dataset into report 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

How to display SQL search results in a datagrid using WPF

private void Button_Click(object sender, RoutedEventArgs e)
{
SqlConnection sc = new SqlConnection();
SqlCommand com = new SqlCommand();
sc.Open();
com.Connection = sc;
string sql;
{
sql = "SELECT FROM WolfAcademyForm WHERE [Forename] == 'txtSearch.Text';";
{
grdSearch.ItemsSource = sql;
sc.Close();
}
This is the code that I have, When I press the search button nothing shows up... Can someone please help me with this problem, I don't get any errors
Problems:
SQL query is not right:
It should be like SELECT * FROM TABLENAME.
In WHERE clause [Forename] == 'txtSearch.Text', == should = and Textbox value should be concatenated using +.
Fixed Code:
private void Button_Click(object sender, RoutedEventArgs e)
{
string sConn = #"Data Source=MYDS;Initial Catalog=MyCat;
User ID=MyUser;Password=MyPass;";
using(SqlConnection sc = new SqlConnection(sConn))
{
sc.Open();
string sql = "SELECT * FROM WolfAcademyForm WHERE [Forename]= #Forename";
SqlCommand com = new SqlCommand(sql, sc);
com.Parameters.AddWithValue("#Forename", txtSearch.Text);
using(SqlDataAdapter adapter = new SqlDataAdapter(com))
{
DataTable dt = new DataTable();
adapter.Fill(dt);
grdSearch.ItemsSource = dt.DefaultView;
}
}
}
Use this
using (SqlConnection con = new SqlConnection(ConString))
{
CmdString = "SELECT FROM WolfAcademyForm WHERE [Forename] == " + txtSearch.Text + ";"
SqlCommand cmd = new SqlCommand(CmdString, con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable("Employee");
sda.Fill(dt);
grdSearch.ItemsSource = dt.DefaultView;
}

Categories