C# Crystal Report detail loop - c#

I have this loop on my report on c# winform crystal report.
The data repeatedly entered on my report, but when I check on the database it only saved once. Consider that the final value is correct.
try
{
Cursor = Cursors.WaitCursor;
Reports.OfficialReceipt crt = new Reports.OfficialReceipt();
cmd = new MySqlCommand();
MySqlDataAdapter myDA = new MySqlDataAdapter();
DataSet DaTs = new DataSet();
con = new MySqlConnection(cs);
cmd.Connection = con;
cmd.CommandText = "SELECT collection_type.collection_id, collection_type.school_year, collection_type.IDno, collection_type.student_org_desc, collection_type.man_org, collection_type.grand_total, collection_type.tendered_cash, collection_type.payment_change, collection_type.collection_date, student_info.FName, student_info.LName, accounts.name, collection_list.detail, collection_list.unit, collection_list.amount FROM collection_type JOIN collection_list on (collection_type.collection_id = collection_list.collection_id) JOIN accounts on (accounts.id = collection_type.account_id) JOIN student_info on (student_info.IDno = collection_type.IDno) WHERE collection_type.IDno='" + txtIDNo_Collection.Text + "'";
cmd.CommandType = CommandType.Text;
myDA.SelectCommand = cmd;
myDA.Fill(DaTs, "collection_type");
myDA.Fill(DaTs, "student_info");
myDA.Fill(DaTs, "accounts");
myDA.Fill(DaTs, "collection_list");
crt.SetDataSource(DaTs);
Reports.frmSampleForm frm = new Reports.frmSampleForm();
frm.crystalReportViewer1.ReportSource = crt;
frm.Visible = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

There are some fields that you're querying but not showing.
Please drop in the report student_info.LName field.
What do you see? Are there reciepts for many students? Is this what you need?
Run you query to a DB. What is the result you get?

Related

"String or Binary data would be truncated" on Web Deploy, not in debug mode

I have an ASP.Net app which transfers Excel rows to a SQL Server table and then they get processed by a stored procedure (TableA data gets formatted to TableB, then TableA gets truncated).
Both the app and the stored procedure work fine while debugging directly on the VS environment, but on the web deploy I get the following SQL Exception:
String or binary data would be truncated.
The statement has been terminated.
I've already changed the table schema to nvarchar(200) on every column, and turned ansi_warnings off on the stored procedure but nothing seems to affect the error. Again, this is only on the web deploy which only makes me think that the user permissions on the server side (every user logs in as NT AUTHORITY\NETWORK SERVICE on SQL).
This is the C# side of the app:
if (subeArchivo.HasFile)
{
var fechaYMD = DateTime.Now.ToString("yyyyMMdd");
var fechaHM = DateTime.Now.ToString("hhmm");
string path = string.Concat(Server.MapPath("~/archivoCargado/" + fechaHM + "_" + fechaYMD + "_" + subeArchivo.FileName));
subeArchivo.SaveAs(path);
DataTable dt = ExcelToDataTable(subeArchivo.FileBytes, cbEncabezado.Checked);
gridDT.DataSource = dt;
gridDT.DataBind();
try
{
string cadenaConn = ConfigurationManager.ConnectionStrings["cadenaSQL"].ConnectionString.ToString();
SqlConnection conn = new SqlConnection(cadenaConn);
SqlCommand cmd = new SqlCommand();
SqlBulkCopy bulkcopy = new SqlBulkCopy(cadenaConn);
SqlBulkCopyColumnMapping ID_USU_CAR = new SqlBulkCopyColumnMapping("ID_USU_CAR", "ID_USU_CAR");
bulkcopy.ColumnMappings.Add(ID_USU_CAR);
SqlBulkCopyColumnMapping RUT_DCT = new SqlBulkCopyColumnMapping("RUT_DCT", "RUT_DCT");
bulkcopy.ColumnMappings.Add(RUT_DCT);
SqlBulkCopyColumnMapping TIP_CON = new SqlBulkCopyColumnMapping("TIP_CON", "TIP_CON");
bulkcopy.ColumnMappings.Add(TIP_CON);
SqlBulkCopyColumnMapping ID_CON = new SqlBulkCopyColumnMapping("ID_CON", "ID_CON");
bulkcopy.ColumnMappings.Add(ID_CON);
SqlBulkCopyColumnMapping INI_BEN = new SqlBulkCopyColumnMapping("INI_BEN", "INI_BEN");
bulkcopy.ColumnMappings.Add(INI_BEN);
SqlBulkCopyColumnMapping FIN_BEN = new SqlBulkCopyColumnMapping("FIN_BEN", "FIN_BEN");
bulkcopy.ColumnMappings.Add(FIN_BEN);
SqlBulkCopyColumnMapping MON_CLP = new SqlBulkCopyColumnMapping("MON_CLP", "MON_CLP");
bulkcopy.ColumnMappings.Add(MON_CLP);
bulkcopy.DestinationTableName = "TMP_CAR_DCT_BEN";
bulkcopy.WriteToServer(dt);
lblCarga.Visible = true;
lblCarga.Text = "CARGA A SQL COMPLETADA";
lblSP.Visible = true;
lblSP.Text = "EJECUTANDO VALIDACIONES";
cmd.CommandText = "VAL_DCT_BEN_BETA2";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
lblSP.Text = "VALIDACIÓN COMPLETADA";
}
catch (Exception ex)
{
lblConfirma.Text = ex.Message.ToString();
}
try
{
lblRes.Visible = true;
connSQL.Open();
// PROCESO RESUMEN
SqlCommand selProc = connSQL.CreateCommand();
SqlDataAdapter adaptadorDatos1 = new SqlDataAdapter(selProc);
selProc.CommandText = "SELECT TOP 1 * FROM CON_PRO_BEN ORDER BY CAST(FEC_CAR AS DATETIME) DESC, HOR_CAR DESC";
selProc.CommandType = CommandType.Text;
DataTable tablaProcRes = new DataTable();
adaptadorDatos1.Fill(tablaProcRes);
gridProcRes.DataSource = tablaProcRes;
gridProcRes.DataBind();
gridProcRes.Visible = true;
connSQL.Close();
connSQL.Open();
// PROCESO DESCRIPTIVO
SqlCommand selProcDes = connSQL.CreateCommand();
SqlDataAdapter adaptadorDatos2 = new SqlDataAdapter(selProcDes);
selProcDes.CommandText = "SELECT * FROM CON_PRO_BEN_DES " +
"WHERE FEC_CAR = (SELECT MAX(FEC_CAR) FROM CON_PRO_BEN_DES) AND HOR_CAR = (SELECT MAX(HOR_CAR) FROM CON_PRO_BEN_DES) " +
"ORDER BY HOR_CAR DESC";
selProcDes.CommandType = CommandType.Text;
DataTable tablaProcResDes = new DataTable();
adaptadorDatos2.Fill(tablaProcResDes);
gridProcDes.DataSource = tablaProcResDes;
gridProcDes.DataBind();
gridProcDes.Visible = true;
connSQL.Close();
}
catch (Exception ex)
{
lblResQ.Text = ex.Message.ToString();
}
}
else
lblError.Visible = true;
lblError.Text = "Incidencias en la carga, reintentar.";

How to search data in gridview using a TextBox value with an Oracle Database

I am trying to show Oracle Data in a DataGridView in my Windows Form Application but it just returns a grey blank view. My code for this currently is:
string insertquery = "select * from Candidate where CandidateName like '"+ txtBoxSearchData.Text +"%'";
OracleConnection con = new OracleConnection(oradb);
con.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = con;
cmd.CommandText = insertquery;
try
{
OracleDataReader reader = cmd.ExecuteReader();
OracleDataAdapter orada = new OracleDataAdapter(cmd);
DataTable dataTable = new DataTable();
orada.Fill(dataTable);
dataTable.Load(reader);
BindingSource bSource = new BindingSource();
bSource.DataSource = dataTable;
dataGridViewSearch.DataSource = bSource;
orada.Update(dataTable);
}
catch(ArgumentException ex)
{
MessageBox.Show("Error: " + ex.Message);
}
catch(OracleException ex1)
{
MessageBox.Show("Error: " + ex1.Message);
}
finally
{
cmd.Dispose();
con.Dispose();
}
}
I am positive I do not require all those functions in my Try statement but I have come across many different methods to do this - I just included all of those into my code to experiment. The connection string is correct too as I have succeeded in adding data into the tables through queries in another part of my application.
Doing something like the following should be enough. There should be no need for an OracleDataAdapter or BindingSource. Just can't test it here, as I do not have an Oracle database around.
public void fillDataGrid()
{
try
{
using(OracleConnection connection = new OracleConnection("connectstring"))
using(OracleCommand cmd = new OracleCommand("select * from my super table", connection ))
{
connection .Open();
using(OracleDataReader oracleDataReader = cmd.ExecuteReader())
{
DataTable dataTable = new DataTable();
dataTable.Load(oracleDataReader );
myDataGrid.DataSource = dataTable;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}

update bit datatype value in database

try
{
UserMaster ObjUserMst = new UserMaster();
ObjUserMst.GetData("UPDATE MemberDetails SET Active = 0 WHERE Member_No = '" + txtmemberno.Text + "'");
MessageBox.Show("Installment Close Successfully.", "Close Installment", MessageBoxButtons.OK, MessageBoxIcon.Information);
btndebit.Visible = true;
btndebit.Visible = false;
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.Message.ToString(), "btncloseinstallment_Click", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
its get data code
public DataTable GetData(string Query)
{
string cn = GlobalClass.ConnectionStringGet();
Con = new SqlConnection(cn);
cmd = new SqlCommand();
cmd.Connection = Con;
if (cmd.Connection.State == ConnectionState.Closed)
{
cmd.Connection.Open();
}
SqlTransaction ObjTrans = cmd.Connection.BeginTransaction();
cmd.Transaction = ObjTrans;
cmd.CommandType = CommandType.Text;
cmd.CommandText = Query;
cmd.CommandTimeout = 500;
SqlDataReader dreader = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dreader);
Con.Close();
Con.Dispose();
return dt;
}
i have winforms.
i have memberdetails Table - in this Active Field & its datatype is BIT. its
default value is 1. but i need to update it to 0.
1 = ture
0 = false
when i tried above code Active Field data didnt update
but i got message "Installment Close Successfully."
http://i.stack.imgur.com/mkuhW.png
http://i.stack.imgur.com/ToXFV.png
I upload my images on above link
help me guys.. sorry if i didnt explain very well bcz i m new here
ok i got it.
string constring = GlobalClass.ConnectionStringGet();
string sqlUpdate = "UPDATE MemberDetails SET Active = '0' WHERE Member_No = '" + txtmemberno.Text + "'";
SqlConnection conDatabase = new SqlConnection(constring);
SqlCommand cmdd = new SqlCommand(sqlUpdate, conDatabase);
conDatabase.Open();
cmdd.ExecuteNonQuery();
conDatabase.Close();
MessageBox.Show("Installment Close Successfully.");
its update Active Field 1 to 0 successfully.

fatal error encountered during execution... during update

This code is placed in the button. and when i click it to update the data, a messagebox error appears saying "fatal error encountered during command execution".
Your answers would be a great help. Thank you
MySqlConnection connection = new MySqlConnection(MyConnectionString);
MySqlCommand cmd;
try
{
connection.Open();
cmd = connection.CreateCommand();
cmd.CommandText = "UPDATE student_offense SET TYPE=#TYPE,DATE_HAPPENED=#DH,DESCRIPTION=#DESC,SANCTION=#SANC" +
"Where STUDENT_NO = #STUDENT_NO And DESCRIPTION=#DESC And SANCTION=#SANC And DATE_HAPPENED=#DH";
cmd.Parameters.AddWithValue("#TYPE", offense_combo.Text);
cmd.Parameters.AddWithValue("#DH", date_hapen.Text);
cmd.Parameters.AddWithValue("#DESC", description_txt.Text);
cmd.Parameters.AddWithValue("#SANC", sanction_txt.Text);
cmd.Parameters.AddWithValue("#STUDENT_NO", studentNo_txt.Text);
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
MessageBox.Show("updated");
//refresh
cmd.CommandText = "SELECT student_info.Student_no,student_info.Lastname,student_info.Firstname,student_offense.Type,student_offense.Description,student_offense.Date_Happened,student_offense.Sanction,student_offense.Date_Recorded from student_info,student_offense where student_info.student_no = student_offense.student_no";
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmd;
dbdataset = new DataTable();
sda.Fill(dbdataset);
bSource = new BindingSource();
bSource.DataSource = dbdataset;
dataGridView1.DataSource = bSource;
sda.Update(dbdataset);
bSource.DataSource = dbdataset;
dataGridView1.DataSource = bSource;
student_no_valid.Visible = false;
stud_no_error.Visible = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
listBox1.Items.Clear();
description_txt.Text = "";
studentNo_txt.Text = "";
offense_combo.Text = "";
current_date();
sanction_txt.Text = "";
You are missing space between Parameter #SANC and Where .
Try This:
cmd.CommandText = "UPDATE student_offense SET TYPE=#TYPE,DATE_HAPPENED=#DH,
DESCRIPTION=#DESC,SANCTION=#SANC" + " Where STUDENT_NO = #STUDENT_NO And
DESCRIPTION=#DESC And SANCTION=#SANC And DATE_HAPPENED=#DH";
Suggestion : if your DATE_HAPPENED column type is Date in your table,then You need to send the proper Date format.
Try This: Assuming user enters Date in dd-MM-yyyy format.
DateTime dt = DateTime.ParseExact(date_hapen.Text,"dd-MM-yyyy",
CutureInfo.InvariantCulture);
Now while assigning the DATE_HAPPENED value provide the following format
cmd.Parameters.AddWithValue("#DH",dt.ToString("yyyy-MM-dd"));
Probably this happened at cmd.ExecuteNonQuery(); or in the subsequent query. You can verify this by single-stepping through after a breakpoint. There is probably an error in the SQL. You can find this by looking at the internal error, or by trying the query on MySQL Workbench. Check to see that all of the parameters match table columns, and all the data types match.
Incidentally, there is no need to assign SANCTION and DATE_HAPPENED in the update statement since you required them to be equal in the WHERE.

Retrieving data in DataGridView using SELECT on MySQL

I have a Windows Forms Application. I have to search data from the MySQL database using the "Like" operator. I have used the placeholder (#test, see code below) for the search term but it does not happen.
In simple, I have a TestBox named txtSearch in which I can enter the name of a student partially or completely. I need to retrieve all the entries matching search criteria on DataGrid.
Please check my code below and let me know where I am wrong (Conn String is removed below) :
try
{
dataGridView1.Visible = true;
BindingSource bs = new BindingSource();
DataTable newadm = new DataTable();
String term = txtSearch.Text;
string db = ----Connection String goes here----
bs.DataSource = newadm;
this.dataGridView1.DataSource = bs;
MySqlConnection conn = new MySqlConnection(db);
conn.Open();
string s = "select * from newadm where firstname like '%#term%' OR lastname like '%#term%'";
MySqlCommand cmd = new MySqlCommand(s, conn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("#term", MySqlDbType.VarChar).Value = txtSearch.Text;
MySqlDataAdapter da = new MySqlDataAdapter();
da.SelectCommand = new MySqlCommand(s, conn);
da.Fill(newadm);
da.Update(newadm);
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
I get no results in DataGrid using this code(and many variations of it). If I remove the "#Term" place holder, and replace the exact terms, it works.
Please help out. Thanks in advance.
Try,
string s = "select * from newadm where firstname like #term OR lastname like #term";
MySqlCommand cmd = new MySqlCommand(s, conn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("#term", MySqlDbType.VarChar,40).Value = "%" + txtSearch.Text + "%";

Categories