I try create a pdf with a subreport and a chart into subreport, when i create pdf, this error show:
The subreport 'Subreport' could not be found at the specified location
~\ReciboCargoConceptos.rdlc. Please verify that the subreport has been
published and that the name is correct.
.... I add subProcessingEvent
var appDomain = AppDomain.CurrentDomain;
var basePath = appDomain.RelativeSearchPath ?? appDomain.BaseDirectory;
var path = Path.Combine(basePath, "Vistas", "ReciboTres" + ".rdlc");
var reporteLocal = new ReportViewer();
reporteLocal.Reset();
reporteLocal.LocalReport.DataSources.Clear();
if (File.Exists(path))
reporteLocal.LocalReport.ReportPath = path;
var utils = new Utils();
DataTable dt = GetResult();
reporteLocal.LocalReport.DataSources.Add(new ReportDataSource("DSRecibo", dt));
reporteLocal.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(CargoConceptoSubReport);
Method SubreportProcessingReport
void CargoConceptoSubReport(object sender, SubreportProcessingEventArgs e)
{
int idContrato = int.Parse(e.Parameters["IdContrato"].Values[0]);
var dtGrafica= GetGrafica(idContrato);
ReportDataSource datasource= new ReportDataSource("Grafica", dtGrafica);
e.DataSources.Add(datasource);
}
Method GetGrafica
private DataTable GetGrafica(int id)
{
DataTable resultTable = new DataTable();
SqlConnection conn = new SqlConnection("ConnectionString");
try
{
using (conn)
{
string query = #"select top 24 Consumo.ConsumoAgua, PeriodoFacturacion.FechaFinal, PeriodoFacturacion.Id from Consumo inner join PeriodoFacturacion on " +
"PeriodoFacturacion.Id = Consumo.IdPeriodoFacturacion where Consumo.IdContrato = " + id + " order by PeriodoFacturacion.FechaFinal desc";
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
resultTable.Load(reader);
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
return resultTable;
}
PDF With Error SubReport With Chart:
Related
When printing the receipt through the reportviewer memory leakage is arising. Even though I have tried other solutions mentioned non of it didn't give me any positive outcome.
I have run the garbage collector but still did not get any outcome properly. Please give me a proper solution to sort out the matter.
static string myconn = ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;
SqlConnection conn = new SqlConnection(myconn);
readonly SalesF sf;
public RecieptForm(SalesF frm)
{
InitializeComponent();
this.sf = frm;
}
private void RecieptForm_Load(object sender, EventArgs e)
{
this.reportViewer1.RefreshReport();
}
public void LoadReceipt(string cashTendered, string cashBalance)
{
ReportDataSource receiptds;
try
{
this.reportViewer1.LocalReport.ReportPath = Application.StartupPath + #"\Reports\CashPaymentReciept.rdlc";
this.reportViewer1.LocalReport.DataSources.Clear();
DataSet1 dc = new DataSet1();
SqlDataAdapter sda = new SqlDataAdapter();
string query = "select c.id,c.transno,c.productid,c.price,c.quantity,c.total,c.transactiondate,p.description from tbl_cart as c inner join product as p on p.productID=c.productid where transno like '" + sf.lblInvoiceNumber.Text + "'";
conn.Open();
sda.SelectCommand = new SqlCommand(query, conn);
sda.Fill(dc.Tables["dtSold"]);
sda.Dispose();
dc.Dispose();
ReportParameter cartTotal = new ReportParameter("cartTotal", sf.lblAmount.Text);
ReportParameter cashTend = new ReportParameter("cashTendered", cashTendered);
ReportParameter cashBal = new ReportParameter("cashBalance", cashBalance);
ReportParameter storeName = new ReportParameter("storeName", store.ToUpper());
ReportParameter cashier = new ReportParameter("cashier", "Cashier: " + sf.lblName.Text);
reportViewer1.LocalReport.SetParameters(cartTotal);
reportViewer1.LocalReport.SetParameters(cashTend);
reportViewer1.LocalReport.SetParameters(cashBal);
reportViewer1.LocalReport.SetParameters(storeName);
reportViewer1.LocalReport.SetParameters(cashier);
receiptds = new ReportDataSource("DataSet1", dc.Tables["dtSold"]);
reportViewer1.LocalReport.DataSources.Add(receiptds);
reportViewer1.LocalReport.PrintToPrinter();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sf.Refresh();
reportViewer1.Dispose();
GC.SuppressFinalize(reportViewer1);
conn.Close();
conn.Dispose();
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
Ontrip _ontrip = new Ontrip(_FNAME);
string _query2 = "select CContactno from CustomerTbl where CUsername = #USERNAME";
string _query3 = "select Price from TransactionTypeTble T join PendingTransTbl P ON P.TransType = T.TransType ";
string _query4 = "select VehicleDescription from DriverTbl D join VehicleSpecTbl V ON D.VehicleType = V.VehicleType";
SqlConnection _sqlcnn = new SqlConnection("Data Source=MELIODAS;Initial Catalog=WeGo;Integrated Security=True");
_sqlcnn.Open();
try
{
SqlDataReader _reader = null;
SqlCommand _cmd = new SqlCommand("Select CFName+' '+CLName from CustomerTbl where CUsername=#USERNAME", _sqlcnn);
SqlParameter _param = new SqlParameter();
_param.ParameterName = "#USERNAME";
_param.Value = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
_cmd.Parameters.Add(_param);
_reader = _cmd.ExecuteReader(); //for displaying users name in the label
while (_reader.Read())
{
_ontrip._txtboxUsername.Text = _reader.GetString(0);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
using (SqlCommand _sqlcmd = new SqlCommand(_query2, _sqlcnn))
{
try
{
SqlDataReader _reader = null;
SqlParameter _param = new SqlParameter();
_param.ParameterName = "#USERNAME";
_param.Value = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
_sqlcmd.Parameters.Add(_param);
_reader = _sqlcmd.ExecuteReader(); //for displaying users name in the label
while (_reader.Read())
{
_ontrip._txtboxContact.Text = _reader.GetString(0);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Is their a way for me to read the query and display the output, when i run this code their is an error saying that their is already an open data reader associated with the command. I should be displaying multiple data in a textbox
try Call Close when done reading.
_reader.Close();
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
Ontrip _ontrip = new Ontrip(_FNAME);
string _query2 = "select CContactno from CustomerTbl where CUsername = #USERNAME";
string _query3 = "select Price from TransactionTypeTble T join PendingTransTbl P ON P.TransType = T.TransType ";
string _query4 = "select VehicleDescription from DriverTbl D join VehicleSpecTbl V ON D.VehicleType = V.VehicleType";
SqlConnection _sqlcnn = new SqlConnection("Data Source=MELIODAS;Initial Catalog=WeGo;Integrated Security=True;MultipleActiveResultSets=True ");
_sqlcnn.Open();
I added MultipleActiveResultSet or MARS
I need to find a record in C# but I get an InvalidOperationException:
public static Auto findAuto(int kfznr)
{
Auto retAuto = new Auto();
try
{
myOleDbConnection.Open();
string query = "SELECT * FROM Auto WHERE Auto.KFZNR = " + kfznr;
OleDbCommand select = new OleDbCommand();
select.Connection = myOleDbConnection;
select.CommandText = query;
OleDbDataReader reader = select.ExecuteReader();
while (reader.Read())
{
Auto at = new Auto(Convert.ToInt32(reader[0]), Convert.ToString(reader[1]), Convert.ToString(reader[2]));
retAuto = at;
}
}
catch (OleDbException e)
{
Console.WriteLine(e.ToString());
}
return retAuto;
}
I get the error in the while loop at the creation of the new Auto.
When I run the same query in the SQLDeveloper I get one record (take a look at the first screenshot) but in my C# program I get there is no data for my row/cell.
When I hover the reader I get the following image. It says that the reader has rows:
Hope that you can help me fix this problem.
You need to use reader.GetValue(0)
public static Auto findAuto(int kfznr)
{
Auto retAuto = new Auto();
try
{
myOleDbConnection.Open();
string query = "SELECT * FROM Auto WHERE Auto.KFZNR = " + kfznr;
OleDbCommand select = new OleDbCommand();
select.Connection = myOleDbConnection;
select.CommandText = query;
OleDbDataReader reader = select.ExecuteReader();
while (reader.Read())
{
Auto at = new Auto(Convert.ToInt32(reader.GetValue(0)), Convert.ToString(reader.GetValue(1)), Convert.ToString(reader.GetValue(2)));
retAuto = at;
}
}
catch (OleDbException e)
{
Console.WriteLine(e.ToString());
}
return retAuto;
}
Also wanted to add...you can access the reader values by column name as well using:
reader["ColumnName"]...don't forget the "" around the name of the column ;)
public static Auto findAuto(int kfznr)
{
Auto retAuto = new Auto();
try
{
myOleDbConnection.Open();
string query = "SELECT * FROM Auto WHERE Auto.KFZNR = " + kfznr;
OleDbCommand select = new OleDbCommand();
select.Connection = myOleDbConnection;
select.CommandText = query;
OleDbDataReader reader = select.ExecuteReader();
while (reader.Read())
{
Auto at = new Auto(Convert.ToInt32(reader["col1"]), Convert.ToString(reader["col2"]), Convert.ToString(reader["col3"]));
retAuto = at;
}
}
catch (OleDbException e)
{
Console.WriteLine(e.ToString());
}
return retAuto;
}
I need some help to complete this. I have searched and tried several ways but is not enetring in my head yet. It is not homework! I am a self learner.
I managed to populate a grid selecting the table from a dropdown:
private void radDropDownList1_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
{
if (radDropDownList1.SelectedIndex > 0)
{
radGridView1.Visible = true;
label8.Text = radDropDownList1.SelectedValue.ToString();
DataTable dt = new DataTable();
var selectedTable = radDropDownList1.SelectedValue; //Pass in the table name
string query = #"SELECT * FROM " + selectedTable;
using (var cn = new SqlConnection(connString))
{
cn.Open();
try
{
SqlCommand cmd = new SqlCommand(query, cn);
using (var da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
}
}
catch (SqlException ex)
{
//MessageBox.Show(ex.StackTrace);
}
}
radGridView1.DataSource = dt;
}
Now I am trying to write the event to update the sql table using the grid as datasource:
private void radGridView1_CellEndEdit(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
{
DataTable dt = (DataTable)radGridView1.DataSource;
DataSet ds = new DataSet();
ds.Tables[0] = dt;
try
{
//**I am lost here!**
}
catch
{
}
}
Could you please help? How can I now update the sql table?
I don't really like the way you are managing the updates but... this code here will send an update statement:
using (var cn = new SqlConnection(connString))
{
cn.Open();
try
{
SqlCommand cmd = new SqlCommand("UPDATE YourTable SET Column = #Parm1 WHERE Col = #Parm2", cn);
var parm1 = cmd.CreateParameter("Parm1");
parm1.Value = "SomeValue";
var parm2 = cmd.CreateParameter("Parm2");
parm2.Value = "SomeOtherValue";
cmd.Parameters.Add(parm1);
cmd.Parameters.Add(parm2);
int rowsAffected = cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
//MessageBox.Show(ex.StackTrace);
}
}
I am trying to a get the Row values from a excel sheet, based on the column Value.
e.g. I have CutsomerID as lets say 5 , so I want First name 5, last Name 5 and Address 5
I am converting whole excel sheet into DataTable and then trying to read on each DataRow, when I get CustomerID as 5, I copy all the values and break from the loop
Here is my code and it is working fine as well, but I was wondering is there any way to optimise it.
Here is my Code.
public ExcelData GetDataByCustomerID(String excelFilePath, String customerID)
{
OleDbConnectionStringBuilder connectionStringBuilder = new OleDbConnectionStringBuilder();
connectionStringBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
connectionStringBuilder.DataSource = excelFilePath;
connectionStringBuilder.Add("Mode", "Read");
const string extendedProperties = "Excel 12.0;IMEX=1;HDR=YES";
connectionStringBuilder.Add("Extended Properties", extendedProperties);
String connectionString = connectionStringBuilder.ToString();
// Create connection object by using the preceding connection string.
using( var objConn = new OleDbConnection(connectionString))
{
objConn.Open();
// Get the data table contaning the schema guid.
DataTable excelSheetsDataTable = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (excelSheetsDataTable == null)
return null;
// get all the tables in the Sheet
List<String> excelSheets = (from DataRow row in excelSheetsDataTable.Rows select row["TABLE_NAME"].ToString()).ToList();
// Our data is on First sheet only
OleDbCommand _oleCmdSelect = new OleDbCommand(#"SELECT * FROM [" + excelSheets[0] + "]", objConn);
OleDbDataAdapter oleAdapter = new OleDbDataAdapter();
oleAdapter.SelectCommand = _oleCmdSelect;
DataTable newDataTable = new DataTable();
oleAdapter.FillSchema(newDataTable, SchemaType.Source);
oleAdapter.Fill(newDataTable);
if (newDataTable.Columns.Contains("CustomerID"))
{
foreach (DataRow rowValue in newTB.Rows)
{
if ((string) rowValue["CustomerID"] == customerID)
{
var data = new ExcelData
{
customerFirstName = rowValue["Customer_First_ Name"].ToString(),
customerLastName = rowValue["Customer_Last_Name"].ToString(),
customerAddress = rowValue["Customer_Address"].ToString(),
};
return data;
}
}
String message = String.Format("The CustomerID {0} not found in Excel file {1}", customerID, excelFilePath);
MessageBox.Show(message);
}
else
{
String message = String.Format("The Column CustomerID not found in Excel file {0}", excelFilePath);
MessageBox.Show(message);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return null;
}
public class ExcelData
{
public String customerID;
public String customerFirstName;
public String customerLastName;
public String customerAddress;
}
Change your select query to like below -
#"SELECT * FROM [" + excelSheets[0] + "] WHERE CustomerID=<your_value>"
Courtesy msdn web site:
Link: MSDN
DataTable dt;
private void button1_Click(object sender, EventArgs e)
{
try
{
openFileDialog1.ShowDialog();
string connectionString = string.Format("Provider = Microsoft.Jet.OLEDB.4.0;Data Source ={0};Extended Properties = Excel 8.0;", this.openFileDialog1.FileName);
var con = new OleDbConnection(connectionString);
var cmd = new OleDbCommand("select * from [sheet1$] where [MRN#]=#c", con);
cmd.Parameters.Add("#c", "33264");
con.Open();
var dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dt = new DataTable();
dt.Load(dr);
}
dr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DataGridView dv = new DataGridView();
this.Controls.Add(dv);
dv.DataSource = dt;
}
}
EDIT:
As per your code you should try the below lines of code:
OleDbCommand _oleCmdSelect = new OleDbCommand(#"SELECT * FROM [" + excelSheets[0] + "]" + " Where [CustomerID#] = #custID" , objConn);
_oleCmdSelect.Parameters.Add("#custID", customerID);
OleDbDataAdapter oleAdapter = new OleDbDataAdapter();
oleAdapter.SelectCommand = _oleCmdSelect;