crystal reports using parameters - c#

i am using crystal reports for the first time. I have written code which is as follows
public partial class _Default : System.Web.UI.Page
{
private ReportDocument report = new ReportDocument();
protected void Page_Load(object sender, EventArgs e)
{
report.Load(Server.MapPath("CrystalReport1.rpt"));
report.FileName = Server.MapPath("CrystalReport1.rpt");
if (!Page.IsPostBack)
{
BindData();
}
}
protected override void OnUnload(EventArgs e)
{
base.OnUnload(e);
this.Unload+=new EventHandler(Page_Unload);
}
public void Page_Unload(object sender, EventArgs e)
{
report.Clone();
report.Dispose();
}
private void BindData()
{
Trusted_Connection=true";
string connectionString = #"Data Source=WINSERVER;Initial Catalog=card;User ID=sa;Password = db2admin";
SqlConnection myConnection = new SqlConnection(connectionString);
SqlDataAdapter ad = new SqlDataAdapter("SELECT name,address,idno FROM iffcar", myConnection);
DataSet ds = new DataSet();
ad.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "idno";
DropDownList1.DataBind();
}
protected void Btn_DisplayReport(object sender, EventArgs e)
{
int idno = Convert.ToInt32(DropDownList1.SelectedValue);
report.SetParameterValue("idno", idno);
CrystalReportViewer1.ReportSource = report;
}
}
i am getting values in the dropdownlist but,
Now my problem is once i select a value from dropdownlist i want report of that value.
how to do it.....
please help me to solve my problem.

Try using the index number of the parameter:
protected void Btn_DisplayReport(object sender, EventArgs e)
{
int idno = Convert.ToInt32(DropDownList1.SelectedValue);
report.SetParameterValue(0, idno);
CrystalReportViewer1.ReportSource = report;
}

Related

choose name from dropownlist and associated ID appear in textbox

this is my code for populating a dropdown list with customers name.
protected void Page_Load(object sender, EventArgs e)
{
//Creating a connection to my database using the connection string
string cs = System.Configuration.ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
{
SqlCommand cmd = new SqlCommand("Select CustomerName from Customer", con);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
DropDownList1.DataTextField = "CustomerName";
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
}
}
}
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
}
}
I want to be able too; when this name is chosen from the dropdownlist box , the associated customer ID will appear in a textbox.
thanks!
It should be as simple as this, by the looks of it:
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
TextBox1.Text = DropDownList1.SelectedValue
}
Assuming you have a textbox whose ID is TextBox1.
Alternatively if you want to display the text from the dropdown list (as opposed to the ID value), then simply:
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
TextBox1.Text = DropDownList1.SelectedItem.Text
}

C# Master Detail gridview on SelectionChanged

I have one form with grid(dataGridView1) and textbox(txtSearch). When I type something in textbox grid filter by field acSubject. Now I put second grid and I want new Custom SQL query which will be depend on selected row in dataGridView1.
SQL would be:
select anUserID from the_setsubjcontact where acSubject = #acSubject
How can I do this?
Code is:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = #"Data Source=local\s08r2;Initial Catalog=Demo;User id=sa;Password=sa";
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(#"
SELECT acSubject, acAddress, acPost, acName, acPhone,
acFieldSA, acFieldSB, acFieldSC, acFieldSD, acFieldSE,
anFieldNA, anFieldNB, anFieldNC, anFieldND, anFieldNE, OdgovornaOsoba, acSubjTypeBuyer
FROM ARS.dbo._ARSCRM_vSubjekti
", con);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
}
private void txtSearch_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtSearch.Text))
{
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Empty;
}
else
{
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("acSubject like '%{0}%'", txtSearch.Text);
}
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
DataGridView dgv = (DataGridView)sender;
//User selected a cell (show the first cell in the row)
if (dgv.SelectedCells.Count > 0)
txtAcFieldSA.Text = dgv.Rows[dgv.SelectedCells[0].RowIndex].Cells[5].Value.ToString();
}
}
I was success when I use DataSet from C#, but with CustomSQL I don't know how to do that.
private void ShowDetails(int UserId)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = #"Data Source=local\s08r2;Initial Catalog=Demo;User id=sa;Password=sa";
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(#"
select anUserID from the_setsubjcontact where acSubject = #acSubjec", con);
da.SelectCommand.Parameters.AddWithValue(#acSubjec, UserId.ToString());
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView2.DataSource = dt;
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
DataGridView dgv = (DataGridView)sender;
//User selected a cell (show the first cell in the row)
if (dgv.SelectedCells.Count > 0 && dgv.SelectedCells[0].RowIndex >-1 && dgv.Rows[dgv.SelectedCells[0].RowIndex].Cells.Count > 0)
txtAcFieldSA.Text = dgv.Rows[dgv.SelectedCells[0].RowIndex].Cells[0].Value.ToString();
ShowDetails(int.Parse(txtAcFieldSA.Text));
}

Get more information on GridView row click

I need to get this : When I click on a row, to get a more information about that particular file(every row represents one file from database), and that information is stored in different table... So, how is the best way to do this? Here you have a picture...
public partial class WebForm1 : System.Web.UI.Page
{
Web_service.WebService1SoapClient service;
protected void Page_Load(object sender, EventArgs e)
{
service = new Web_service.WebService1SoapClient();
BindData();
}
protected void BindData()
{
string strConnection = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand cmd = new SqlCommand("select * from request", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
}
protected override void Render(HtmlTextWriter writer)
{
foreach (GridViewRow r in GridView1.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
r.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
r.Attributes["onmouseout"] = "this.style.textDecoration='none';";
r.ToolTip = "Click to select row";
r.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + r.RowIndex, true);
}
}
base.Render(writer);
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindData();
}
}
}

Display specific Column when checkbox is checked

I would like to display a specific column (date of birth, for an example) on a dataGridView which is connected to MS Access, when a checkbox (date of birth checkbox) is checked. It would help much more if it was an array, because I want to add more than one checkbox.
namespace emp_db1
{
public partial class Print : Form
{
private OleDbConnection connection = new OleDbConnection();
public Print()
{
InitializeComponent();
connection.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source = emp_0.mdb";
}
DataTable ds;
private void Print_Load(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "SELECT ID, Name, DOB FROM emp_per";
OleDbDataAdapter da = new OleDbDataAdapter(command);
ds = new DataTable();
da.Fill(ds);
dataGridView1.DataSource = ds;
da.Update(ds);
connection.Close();
dataGridView1.AutoResizeColumns();
dataGridView1.AutoResizeColumnHeadersHeight();
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
this.dataGridView1.Sort(this.dataGridView1.Columns[0], ListSortDirection.Ascending);
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void dob_check_CheckedChanged(object sender, EventArgs e)
{
}
}
}
thankyou.
Solution :
namespace emp_db1
{
public partial class Print : Form
{
List<CheckBox> chkboxes = new List<CheckBox>();
private OleDbConnection connection = new OleDbConnection();
public Print()
{
InitializeComponent();
connection.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source = emp_0.mdb";
}
DataTable ds;
private void Print_Load(object sender, EventArgs e)
{
chkboxes.Add(dob_check); //0
chkboxes.Add(mother_check); //1
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "SELECT ID, Name, DOB, Mother FROM emp_per";
OleDbDataAdapter da = new OleDbDataAdapter(command);
ds = new DataTable();
da.Fill(ds);
dataGridView1.DataSource = ds;
da.Update(ds);
connection.Close();
dataGridView1.AutoResizeColumns();
dataGridView1.AutoResizeColumnHeadersHeight();
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
this.dataGridView1.Sort(this.dataGridView1.Columns[0], ListSortDirection.Ascending);
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
}
private void refreshCheckBoxes(int id)
{
for(int x = 0; x < 2; x++)
{
if (!chkboxes[x].Checked) dataGridView1.Columns[x+2].Visible = false; else dataGridView1.Columns[x+2].Visible = true;
}
}
private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
}
private void dob_check_CheckedChanged(object sender, EventArgs e)
{
refreshCheckBoxes(0);
}
private void mother_check_CheckedChanged(object sender, EventArgs e)
{
refreshCheckBoxes(1);
}
}
}

search, edit and delete using gridview with sql server in c#

i am using Gridview buttons to edit and delet records from sql DB , the problem that i use specific criteris (search by textbox and combobox) then i do edit for the results here is the code i use :
protected void Page_Load(object sender, EventArgs e)
{
conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["zamzammembersConnectionString"].ConnectionString);
if (!IsPostBack)
{
bind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbl = (Label)row.FindControl("lblid");
TextBox textname = (TextBox)row.FindControl("textbox1");
TextBox textmarks = (TextBox)row.FindControl("textbox2");
GridView1.EditIndex = -1;
conn.Open();
SqlCommand cmd = new SqlCommand("update emp set marks=" + textmarks.Text + " , name='" + textname.Text + "' where rowid=" + lbl.Text + "", conn);
cmd.ExecuteNonQuery();
conn.Close();
bind();
}
public void bind()
{
conn.Open();
SqlCommand com = new SqlCommand("select zam_pcinfo.employe_name as Name , zam_location.locationname as Location from zam_pcinfo inner join zam_location on zam_pcinfo.location_id = zam_location.locationId where zam_pcinfo.employe_name like #name or zam_location.locationname like #locationname;", conn);
com.Parameters.AddWithValue("#name", SqlDbType.VarChar).Value = nametxt.Text;
com.Parameters.AddWithValue("#locationname", SqlDbType.VarChar).Value = locationdrop.SelectedValue;
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
conn.Close();
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbldeleteID = (Label)row.FindControl("lblid");
conn.Open();
SqlCommand cmd = new SqlCommand("delete emp where rowid=" + lbldeleteID.Text + "", conn);
cmd.ExecuteNonQuery();
conn.Close();
bind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bind();
}
}
when i try to make the same code in bind() method to a button it works fine but i couldn't do edit and delete on it . how can i make edit and delete in gridview with data that i search for ?

Categories