Display Crystal Report Viewer in ASP.NET - c#

I'm having some trouble in CrystalReport ASP.NET.
I'm creating a DataSet in ASP.NET and I want to export it to CrystalReport.rpt using ADO.NET(xml). But it fails. When I make a new connection and put an attribute column in the report, the Main Report Viewer in Crystal Report shows the wrong result. I compile the sql query in Oraclesqldevelop, the result is fine and doesn't have any problems. I want to click a button and show the report.
This is my code :
protected void btnsumbit_Click(object sender, EventArgs e)
{
lblerror.Text = "";
if (txtdari.Text == "" || txtdari.Text == null || txtsampai.Text == "" || txtsampai.Text == null)
{
lblerror.Text = "Tanggal Harus Diisi !!!";
}
else
{
DateTime dt1 = Convert.ToDateTime(txtdari.Text);
DateTime dt2 = Convert.ToDateTime(txtsampai.Text);
if (dt1.Date > dt2.Date)
{
lblerror.Text = "Format Tanggal yang Dimasukkan Salah !!!";
}
else
{
OracleConnection conn = new OracleConnection();
conn.ConnectionString = connectionstring;
conn.Open();
string sql = "SELECT c.SUB_DISTRIBUTOR , c.MID , c.REKNO , b.ID, b.TERMINAL_ID , b.TANGGAL , b.KETERANGAN , b.DEBIT , b.KREDIT , b.SALDO , b.REFF_NO,b.PRODUK,b.NO_PELANGGAN, b.SN_ID , b.STATUS from MERCHANT c JOIN DAILY b ON (b.REKENING_NO = c.REKNO) where TANGGAL between TO_DATE('"+txtdari.Text+"','mm-dd-yyyy') AND TO_DATE('"+txtsampai.Text+"','mm-dd-yyyy')";
OracleCommand cmd = new OracleCommand(sql, conn);
OracleDataAdapter da = new OracleDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
da.Dispose();
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.WriteXmlSchema("C:\\Users\\Henz\\Documents\\Visual Studio 2012\\Projects\\LPI\\LPI\\Files\\Sample.xml");
conn.Close();
Response.Redirect("plot.aspx");
}
}
}

As your description, I can understand that there is not any error happened. The main issue is your result is not like your expectation. So I think you should check again Parameter of your command. Please try with below code :
DateTime dt1 = Convert.ToDateTime(txtdari.Text);
DateTime dt2 = Convert.ToDateTime(txtsampai.Text);
////////ARI DATE/////////
OracleParameter fromDateParameter = new OracleParameter();
fromDateParameter.OracleDbType = OracleDbType.Date;
fromDateParameter.Value = dt1;
////////SAMPAI DATE/////////
OracleParameter toDateParameter = new OracleParameter();
toDateParameter.OracleDbType = OracleDbType.Date;
toDateParameter.Value = dt2;
this.oracleDataAdapter4.SelectCommand = new OracleCommand("SELECT c.SUB_DISTRIBUTOR , c.MID , c.REKNO , b.ID, b.TERMINAL_ID , b.TANGGAL, b.KETERANGAN , b.DEBIT , b.KREDIT , b.SALDO , b.REFF_NO,b.PRODUK,b.NO_PELANGGAN, b.SN_ID , b.STATUS from MERCHANT c JOIN DAILY b ON (b.REKENING_NO = c.REKNO) where TANGGAL BETWEEN :fromDateParameter AND :fromDateParameter)”, conn);
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.SelectCommand.Parameters.Add(fromDateParameter);
da.SelectCommand.Parameters.Add(toDateParameter);
DataTable dt = new DataTable();
da.Fill(dt);

Related

How to plug info from one stored procedure to another?

The way this works, a drop down list has Price Groups in it and I need to get that Price Group and plug it into the stored procedure (_getVendorDetails) to get the Vendor ID. Then I need to put the Vendor ID into another stored procedure (DIST_get_POShortagesaAndExtra_VendorIDNotes) to get notes about the Vendor ID.
protected void LoadExtraVendorIDNotes()
{
if (ddlVendorPriceGroup.SelectedValue == "NA")
return;
DataSet ds = getVendorIDNotesPONotifications(ddlVendorPriceGroup.SelectedValue);
if (ds.Tables[0].Rows.Count > 0)
{
DataTable dt = ds.Tables[0];
lblShipmentScrutinyLow.Visible = Convert.ToBoolean(dt.Rows[0]["ShipmentScrutiny"].ToString());
lblContainsKitsLow.Visible = Convert.ToBoolean(dt.Rows[0]["ContainsKits"].ToString());
lblShippingIssuesLow.Visible = Convert.ToBoolean(dt.Rows[0]["ShippingIssues"].ToString());
lblMeetingNotesLow.Text = Convert.ToString(dt.Rows[0]["MeetingNotes"].ToString());
lblMeetingNotesLow.Visible = Convert.ToString(dt.Rows[0]["MeetingNotes"].ToString()).Length > 0;
lblShipmentNotesLow.Text = Convert.ToString(dt.Rows[0]["ShippingNotes"].ToString());
lblShipmentNotesLow.Visible = Convert.ToString(dt.Rows[0]["ShippingNotes"].ToString()).Length > 0;
}
}
protected DataSet getVendorIDNotes(string VENDORID)
{
DataSet ds = new DataSet();
using (SqlConnection objConn = new SqlConnection(ConfigurationManager.AppSettings["MeyerConnectionString"]))
{
using (SqlDataAdapter dadapter = new SqlDataAdapter())
{
dadapter.SelectCommand = new SqlCommand("DIST_get_POShortagesaAndExtra_VendorIDNotes", objConn);
dadapter.SelectCommand.CommandType = CommandType.StoredProcedure;
SqlParameter vendorID = new SqlParameter("#VENDORID", VENDORID);
dadapter.SelectCommand.Parameters.Add(vendorID);
dadapter.Fill(ds);
}
}
return ds;
}
protected DataSet getVendorIDNotesPONotifications(string VENDORID)
{
DataSet ds = new DataSet();
using (SqlConnection objConn = new SqlConnection(ConfigurationManager.AppSettings["MeyerConnectionString"]))
{
using (SqlDataAdapter dadapter = new SqlDataAdapter())
{
dadapter.SelectCommand = new SqlCommand("_getVendorDetails", objConn);
dadapter.SelectCommand.CommandType = CommandType.StoredProcedure;
SqlParameter vendorID = new SqlParameter("#pricegroup", VENDORID);
dadapter.SelectCommand.Parameters.Add(vendorID);
dadapter.Fill(ds);
}
}
return ds;
}
Going by your example code it seems you are on the right track just that you need to invoke the getVendorIDNotes method from within the getVendorIDNotesPONotifications method and pass the vendorID to it.
The dataset returned from getVendorIDNotes is further returned from the calling method i.e. getVendorIDNotesPONotifications and hence the logic in LoadExtraVendorIDNotes should work as expected.

How to select a value from Dropdownlist and pass value to Gridview?

I am having trouble with my SQL statement to pass the value from my dropdownlist select to my gridview. I tested my SQL statement Serve Management Studio, with a specific date, and it works, but it isn't working with select value from my Dropdownlist. How would I pass the value to the Gridview? Thank you for help, I am new student to the asp.net world.
Image of web application:
Image of My DATABASE:
public void RefreshDay()
{
SqlConnection conn = new SqlConnection(#"data source =.\sqlexpress; integrated security = true; database = DBdentist");
SqlDataAdapter da = null;
DataSet ds = null;
DataTable dt = null;
string sqlsel = "SELECT Distinct patient.patientID, day from patient, patientreservation where patient.patientID = patientreservation.patientID";
try
{
da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sqlsel, conn);
ds = new DataSet();
da.Fill(ds, "myDay");
dt = ds.Tables["myDay"];
DropDownListDay.DataSource = dt;
DropDownListDay.DataTextField = "day";
DropDownListDay.DataValueField = "patientID";
DropDownListDay.DataBind();
DropDownListDay.Items.Insert(0, "Select Day");
}
catch (Exception ex)
{
LabelDay.Text = ex.Message;
}
finally
{
conn.Close();
}
}
protected void DropDownListDay_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownListDay.SelectedIndex != 0)
{
SqlConnection conn = new SqlConnection(#"data source =.\sqlexpress; integrated security = true; database = DbDentist");
SqlDataAdapter da = null;
DataSet ds = null;
DataTable dt = null;
string sqlsel = "SELECT patientreservation.patientID, patient.firstname, patient.lastname, patientreservation.day, patientreservation.hour, treatment.treatment FROM((patientreservation INNER JOIN patient ON patientreservation.patientID = patient.patientID) INNER JOIN treatment ON patientreservation.treatmentID = treatment.treatmentID) WHERE patientreservation.day = " + DropDownListDay.SelectedValue + "";
try
{
da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sqlsel, conn);
ds = new DataSet();
da.Fill(ds, "myDay");
dt = ds.Tables["myDay"];
GridViewDay.DataSource = dt;
GridViewDay.DataBind();
}
catch (Exception ex)
{
LabelDay.Text = ex.Message;
}
finally
{
conn.Close();
}
}
else
{
LabelDay.Text = "You choose None:";
}
}
}
}
You can simply use this:
WHERE patientreservation.day = '" + DropDownListDay.Text.ToString() + "'";
Thanks for the help. I tried everything
DropDownListDay.DataTextField = "day";
DropDownListDay.DataValueField = "day"
helped. I was also having some problem with my gridview. I added up deleting it and making a new, somehow that help.
The configuration of your dropdown is
DropDownListDay.DataTextField = "day";
DropDownListDay.DataValueField = "patientID";
but you need the day as a value if you want to filter with the current selected value
DropDownListDay.DataTextField = "day";
DropDownListDay.DataValueField = "day";
SELECT patientreservation.patientID, patient.firstname, patient.lastname, patientreservation.day, patientreservation.hour, treatment.treatment FROM((patientreservation INNER JOIN patient ON patientreservation.patientID = patient.patientID) INNER JOIN treatment ON patientreservation.treatmentID = treatment.treatmentID) WHERE **patientreservation.day** = " + DropDownListDay.**SelectedValue** + "";
Or you can try with WHERE **patientreservation.day** = " + DropDownListDay.**SelectedText** + "";
The dropdownlist is not triggering the event. Try to put this AutoPostBack="true" in your asp dropdownlist tag.

No data is showing in combobox

I am trying show some data from a database to a combobox based on another combobox selection with this code:
private void metroComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet dt = new DataSet();
try
{
DateTime startDate = Convert.ToDateTime(metroLabel8.Text);
DateTime endDate = Convert.ToDateTime(metroLabel9.Text);
// Make sql readable
string sql =
#"Select [LedId],[LedName] from [Ledger] where Date >= #prmStartDate and Date <= #prmEndDate";
// wrap IDisposable (SqlCommand) into using
using (SqlCommand cmd = new SqlCommand(sql, con))
{
cmd.Parameters.Add("#prmStartDate", SqlDbType.DateTime).Value = startDate;
cmd.Parameters.Add("#prmEndDate", SqlDbType.DateTime).Value = endDate;
con.Close();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
VoucherLedgerName_combo.DisplayMember = "LedName";
VoucherLedgerName_combo.ValueMember = "LedId";
VoucherLedgerName_combo.DataSource = dt.Tables["Ledger"];
}
}
catch(Exception exe)
{
MessageBox.Show(exe.Message);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
But i am getting nothing in the second combobox, and I am sure that there is data in the database table Ledger. Can any one please help me to find the issue?
change your SQL statement as below(Date is reserved keyword)
string sql =
#"Select [LedId],[LedName] from [Ledger] where [Date] >= #prmStartDate and [Date] <= #prmEndDate";
You need to give table name when you fill dataset since you are using the name when you set data source
da.Fill(dt, "Ledger");
or set the data source as below
VoucherLedgerName_combo.DataSource = dt.Tables[0];
DataRow dr = dt.NewRow();
dr["Ledger"] = "--Select All--";
dt.Rows.InsertAt(dr, 0);
You can change from
da.Fill(dt);
VoucherLedgerName_combo.DataSource = dt.Tables["Ledger"];
to
da.Fill(dt, "Ledger");
VoucherLedgerName_combo.DataSource = dt.Tables["Ledger"].DefaultView;
else
VoucherLedgerName_combo.DataSource = dt.Tables[0].DefaultView;
or
VoucherLedgerName_combo.DataSource = dt;

Not getting any row data from database using c# asp.net.

I can not get any row data from database using c# asp.net.I am trying to fetch one row data from my DB but it is not returning any row.I am using 3-tire architecture for this and i am explaining my code below.
index.aspx.cs:
protected void userLogin_Click(object sender, EventArgs e)
{
if (loginemail.Text.Trim().Length > 0 && loginpass.Text.Trim().Length >= 6)
{
objUserBO.email_id = loginemail.Text.Trim();
objUserBO.password = loginpass.Text.Trim();
DataTable dt= objUserBL.getUserDetails(objUserBO);
Response.Write(dt.Rows.Count);
}
}
userBL.cs:
public DataTable getUserDetails(userBO objUserBO)
{
userDL objUserDL = new userDL();
try
{
DataTable dt = objUserDL.getUserDetails(objUserBO);
return dt;
}
catch (Exception e)
{
throw e;
}
}
userDL.cs:
public DataTable getUserDetails(userBO objUserBO)
{
SqlConnection con = new SqlConnection(CmVar.convar);
try
{
con.Open();
DataTable dt = new DataTable();
string sql = "SELECT * from T_User_Master WHERE User_Email_ID= ' " + objUserBO.email_id + "'";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter objadp = new SqlDataAdapter(cmd);
objadp.Fill(dt);
con.Close();
return dt;
}
catch(Exception e)
{
throw e;
}
}
When i am checking the output of Response.Write(dt.Rows.Count);,it is showing 0.So please help me to resolve this issue.
It looks like your your query string has a redundant space between ' and " mark. That might be causing all the trouble as your email gets space in front.
It is by all means better to add parameters to your query with use of SqlConnection.Parameters property.
string sql = "SELECT * from T_User_Master WHERE User_Email_ID=#userID";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("#userID", SqlDbType.NVarChar);
cmd.Parameters["#userID"].Value = objUserBO.email_id;

Show data in datagridview filtered by current date C#

i have large number of rows in my database. when i tried to load all the data in to datagridview it getting stuck. i want to load data to datagridview that only related datetimepicker date. this is my current code
private void showdatagrid()
{
string constring = string.Format("datasource='{0}';username=uwadminview;port=3306;password=*****************;Connect Timeout=20000;Command Timeout=28800", dbserverip.Text);
MySqlConnection conwaqDatabase = new MySqlConnection(constring);
MySqlCommand cmdwaqDatabase = new MySqlCommand(" select * from waq115.loans ; ", conwaqDatabase);
try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdwaqDatabase;
dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bsource = new BindingSource();
bsource.DataSource = dbdataset;
dataGridView1.DataSource = bsource;
sda.Update(dbdataset);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
conwaqDatabase.Close();
}
then soon after i call this i again use a rowfilter event like this
private void filterdata()
{
DataView DV = new DataView(dbdataset);
DV.RowFilter = string.Format("Convert(submittimestamp, System.String) LIKE '%{0}%'", adminviewDTP.Text);
dataGridView1.DataSource = DV;
}
but in this method performance is very bad when loading more than 9000 rows it getting stuck all the time. i want to method that directly query data from database only related to today
(users are daily update the database and they are insert more than 500 rows each day)
(application still in a testing progress)
(i'm using mysql database)
can someone show me any efficient way to do this)
MySqlCommand cmdwaqDatabase = new MySqlCommand("SELECT * FROM waq115.loans WHERE DATE(submittimestamp) = DATE(NOW())", conwaqDatabase);
UPDATE:
With parameter:
MySqlCommand cmdwaqDatabase = new MySqlCommand("SELECT * FROM waq115.loans WHERE DATE(submittimestamp) = DATE(#p)", conwaqDatabase);
then you can add the parameter like below
cmdwaqDatabase.Parameters.AddWithValue("#P", dateTimeValue); // convert datetime picker value to DateTime and set as the value;
or
cmdwaqDatabase.Parameters.Add(new MySqlParameter("#P", MySqlDbType.Timestamp)).Value = dateTimeValue;
Filter in the database tier instead of the middle tier.
MySqlConnection conwaqDatabase = new MySqlConnection(constring);
MySqlCommand cmdwaqDatabase =
new MySqlCommand("select * from waq115.loans where submittimestamp = #date; ", conwaqDatabase);
var dateParam = new MySqlParameter();
dateParam.Name = "#date";
dateParam.Value = Convert.ToDateTime(adminviewDTP.Text);
cmdwaqDatabase.Parameters.Add(dateParam);

Categories