I am making a Crystal-Report from data of GridView. The GridView has three columns in which two columns are filled with values coming from DataBase in a label and third column has a CheckBox which on check sends the value of these two columns as query string to the .cs page of Crystal-Report. The column value is used by the stored-procedure and then result is shown in the Crystal-Report.
Now my problem is if user checks two CheckBox then the value of both columns should be sent to Crystal-Report page as array. How to implement this
My code behind in GridView page is
protected void ChkCity_CheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in gdCityDetail.Rows)
{
bool ischecked = ((System.Web.UI.WebControls.CheckBox)row.Cells[0].FindControl("CheckBox2")).Checked;
if (ischecked == true)
{
int getrow = Convert.ToInt32(e.CommandArgument);
Label label1 = (Label )gdVilDetail.Rows[getrow].FindControl("label1");
Label label2= (Label )gdVilDetail.Rows[getrow].FindControl("label2");
Response.Redirect("~/crystal report/Landowner.aspx?Yojna=" + label1.text + "&Village=" + label2.text);
}
and the code on .cs page of Crystal-Report is
private void cityreport()
{
SqlCommand Cmd = new SqlCommand("Showvillage", Constr1);
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.Parameters.Add("#Yojna_No", Request.QueryString[0]);
Cmd.Parameters.Add("#Village_Code", Request.QueryString[1]);
DataSet ds = new DataSet();
I think you cannot pass Array or any other Objects through Query string except string values. Rather you can use Session Variable.
Try this..
In page One.
//Set
Session["Variable"] = ArrayObj;
In page Two.
//If String[]
string[] arry = (string[])Session["Variable"];
hope it helps..
Check this link. There is a code in the link which has used array to send the value with querystring. I hvn't tried that but may be that will work too.
http://www.codeproject.com/Questions/298718/array-in-query-string
Related
I am working on a Windows forms application. I have two combo boxes, one to select the profile and the other to select the type of matrix. Each profile has a number of matrices which needs to be displayed in the combo box and set to the first matrix as default when the form loads.
I have set the data source and assigned the DisplayMember and Value member properties to both the combo boxes. However, when the form loads, in the second combo box where the different types of matrices should be listed, I have only System.Data.DataRowView for all the values. However, when I select the profile from the first comboBox, the second box is refreshed and the values are displayed correctly.
The code for the Profile comboBox
ddProfile.DataSource = dtProfile;
ddProfile.ValueMember = "ID";
ddProfile.DisplayMember = "Description";
ddProfile.Enabled = dtProfile.Rows.Count > 1;
foreach (DataRow dr in dtProfile.Rows)
{
if (dr["Ordinal"].ToString() == "1")
{
ddProfile.SelectedValue = dr["ID"];
break;
}
}
Code for the matrix comboBox
DataTable dtMatrix = new DataTable();
dtMatrix = DBConnector.GetTable("RiskMatrixList", "*", "", $"Profile={ddProfile.SelectedValue}", DBConnector.ConnectionType.Templates);
dtMatrix = DBConnector.GetTable($"SELECT * FROM RiskMatrixList WHERE Profile={ddProfile.SelectedValue}");
ddRiskMatrix.DataSource = dtMatrix;
ddRiskMatrix.DisplayMember = "Description";
ddRiskMatrix.ValueMember = "ID";
ddRiskMatrix.Enabled = dtMatrix.Rows.Count > 1;
foreach (DataRow dr in dtMatrix.Rows)
{
if (dr["IsDefault"].ToString() == "1")
{
ddRiskMatrix.SelectedValue = dr["ID"].ToString();
break;
}
}
Why am I not getting the right values when the form loads?
I observed that combo_SelectedValueChanged is called twice, when ValueMember and DisplayMember are being assigned, and then - I had the same problem.
After one day of very productive work :/ I discovered the trick:
1/ Added flag to form's properties:
public partial class frmMain : Form
{
bool IsLoadingCombo = false;
2/ Simply modified combo_SelectedValueChanged:
private void cbFilter_SelectedValueChanged(object sender, EventArgs e)
{
if (isLoadingCombo)
return;
// rest of method's code
}
Not very elegant, but better than wasting the next few days.
I think, that problem is that "rest of method's code" needs selected value which is unknown, when the method is stupidly triggered when disp/value members are being asigned.
i am new to asp.net. I am building an admin page and i want to display employee's data in GridView by fetching it from database table. Table has 3 columns (id, name, isManager). There are three possible values for "isManager" column. These values are "yes", "no" and "null". The admin has right to decide for an employee to make him a manager by selecting "yes" or "no" from DropDownList.
This admin page has a GridView control that contains two BoundFileds (i.e. id & name) and one template field (i.e. DropDownList). I am having difficulty in displaying "isManager" column values in DropDownList. I want DropDownList to display selected value/text as "yes" if database table-row has "yes" in "isManager" column, "no" if there is "no" in table-row and display an item "Select Choice" if table-row contains a null value.
My code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string query = "select * from tblUsersTable";
DataSet ds = DataBaseConnectivity.GetData(query);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
// RowDataBound() method
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the DropDownList in the Row
DropDownList ddlManager = (e.Row.FindControl("ddlManager") as DropDownList);
DataSet ds = DataBaseConnectivity.GetData
("select distinct [isManager] from tblUsersTable");
ddlManager.DataSource = ds;
ddlManager.DataTextField = "isManager";
ddlManager.DataValueField = "isManager";
ddlManager.DataBind();
ddlManager.Items.Insert(0, new ListItem("Please select","-1"));
/* After these lines of code i am not finding the right way to implement my logic
*/
Help me to figure it out.
You have to use the DataItem of the GridViewRow to access the underyling record. Then you can select the corect item via DropDownList.SelectedValue:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the DropDownList in the Row
DropDownList ddlManager = (e.Row.FindControl("ddlManager") as DropDownList);
DataSet ds = DataBaseConnectivity.GetData
("select distinct [isManager] from tblUsersTable");
ddlManager.DataSource = ds;
ddlManager.DataTextField = "isManager";
ddlManager.DataValueField = "isManager";
ddlManager.DataBind();
ddlManager.Items.Insert(0, new ListItem("Please select","-1"));
DataRow row = ((DataRowView)e.Row.DataItem).Row;
bool isManager= row.Field<bool>("isManager"); // use the correct type if it's not bool
ddlManager.SelectedValue = isManager.ToString();
Apart from that, i would not use such db-helper classes like DataBaseConnectivity in ASP.NET. They are just a source for nasty errors or performance issues, all the more if you use a static connection. Further informations here.
I have a GridView control that reads from a stored procedure in a SQL Server 2008 database and I am trying to change the backgound color of a row is a certain cell contains certain text. For whatever reason, the solution that I've come up with only works if there is more than one row in the GridView. If there is only one row in the GridView, then the row color of that row isn't changed. I'll post more code if needed, but I'm using the OnRowDataBound
event of the GridView control.
c# for the OnRowDataBound event
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//in every row I would like to check the text of the second cell
if (GridView1.Rows[i].Cells[1].Text == "C6N")
{
e.Row.BackColor = System.Drawing.Color.Red;
}
}
}
Here is the structure of the GridView control
columnName1 column1Desc columnName2 column2Desc
one C6N two zzz
The intent is to change the row color of the GridView if column1Desc is equal to C6N
Below is the button click that binds the GridView. Underneath if(rdr.HasRows) I wrote to the response object and it is in fact printing the correct value of C6N, but when the result set is only one row, the formatting change doesn't take place.
protected void Button2_Click(object sender, EventArgs e)
{
//craete the drugString variable
string drugString = string.Join(string.Empty,druglist.ToArray()).TrimEnd(',');
//create the connection string
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
//create the connection object
using (SqlConnection con = new SqlConnection(cs))
{
//create the command object
using (SqlCommand cmd = new SqlCommand("spMakeTempTable2", con))
{
//don't forget to open the connection
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#drugList", drugString);
SqlDataReader rdr = cmd.ExecuteReader();
GridView1.DataSource = rdr;
GridView1.DataBind();
if (rdr.HasRows)
Response.Write(GridView1.Rows[0].Cells[1].Text);
//this line prints to the screen as it should
else
Response.Write("There were no drug combinations present");
}
}
First thing - you don't need to loop through your gridview rows because this method gets called with EVERY row anyway.
You already know the row you want to query as it just called this method and passed information via the GridViewRowEventArgs property.
So something like this may suit better:
GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
// you only want to check DataRow type, and not headers, footers etc.
if (e.Row.RowType == DataControlRowType.DataRow) {
// you already know you're looking at this row, so check your cell text
if (e.Row.Cells(1).Text == "C6N") {
e.Row.BackColor = System.Drawing.Color.Red;
}
}
}
Also - are you using a template to house the text you're checking? Because if you are, you need to get the control that the text is in - rather than just Cell(1).Text - as this will return all manner of things OTHER than your text.
E.G if you have the text in a label, then check the text of the label instead, by using this instead
Label lbl = (Label)e.Row.FindControl("myTextLabel");
if (lbl.Text == "C6N") {
e.Row.BackColor = System.Drawing.Color.Red;
}
I am creating a website where our customers can order parts directly from us. I have a datatable setup and when users click a button it adds a quick detail of the order to a gridview. In the gridview, I have the edit and delete buttons enabled. the delete function works fine, however when you try to edit the information, it doesn't change the gridview with the new info. Here's what I have so far:
protected void griditems_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DataTable dt = (DataTable)Session["table"];
foreach (DataRow dr in dt.Rows)
{
part = Convert.ToString(dr["Part"]);
dr["Part"] = part;
dr["Quantity"] = qty;
dr["Ship-To"] = shipto;
}
griditems.EditIndex = -1;
BindData();
}
when trying this, it displays the gridview back with the original input values. I have also tried this (not working and get an error that says "There is no row at position 0":
DataTable dt = (DataTable)Session["table"];
GridViewRow row = griditems.Rows[e.RowIndex];
dt.Rows[row.DataItemIndex]["Part"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["Quantity"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["Ship-To"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;
griditems.EditIndex = -1;
BindData();
Am I missing an EditItemTemplate in the aspx file, or am I just doing the RowUpdating all wrong?
You probably need to step back a bit and first check out how you could do create, update, delete and read with a grid view. Also, you may wanna check this post.
i am trying to create a edit profile page so when the page loads first i am filling all text boxes values as the value present in the database for the particular user.i am doing this using the code:
DBconn obj1 = new DBconn();
String sql1 = "select* from users where user_id='" + Session["user_id"].ToString() + "'";
DataTable dt = new DataTable();
dt = obj1.getdatatable(sql1);
if (dt.Rows.Count > 0)
{
referal_id.Text = dt.Rows[0]["referal_id"].ToString();
name.Text = dt.Rows[0]["name"].ToString();
password.Text = dt.Rows[0]["password"].ToString();
email.Text = dt.Rows[0]["email"].ToString();
mobile.Text = dt.Rows[0]["mobile"].ToString();
city.Text = dt.Rows[0]["city"].ToString();
state.Text = dt.Rows[0]["state"].ToString();
pincode.Text = dt.Rows[0]["pincode"].ToString();
sec_ques.Text = dt.Rows[0]["securityq"].ToString();
answer.Text = dt.Rows[0]["answer"].ToString();
age.Text = dt.Rows[0]["age"].ToString();
gender.Text = dt.Rows[0]["gender"].ToString();
user_id.Text = dt.Rows[0]["user_id"].ToString();
address.Text = dt.Rows[0]["address"].ToString();
date_of_joining.Text = dt.Rows[0]["date_of_joining"].ToString();
user_type.Text = dt.Rows[0]["user_type"].ToString();
}
now this is filling all the text boxes with the values .
when user edits some values in some textbox and resubmits the form the database is getting updated implementing the code:
protected void LinkButton1_Click(object sender, EventArgs e)
{
int i;
DBconn obj2 = new DBconn();
String sql2 = "update users set name='"+name.Text+"',address='"+address.Text+"',age='"+age.Text+"',gender='"+gender.Text+"',email='"+email.Text+"',mobile='"+mobile.Text+"',securityq='"+sec_ques.Text+"',answer='"+answer.Text+"',city='"+city.Text+"',state='"+state.Text+"',pincode='"+pincode.Text+"' where user_id='"+Session["user_id"].ToString()+"'";
i = obj2.executeDML(sql2);
if (i > 0)
{
Label1.Visible = true;
Image2.Visible = true;
Label1.Text = "Updated successfully!";
}
else
{
Label1.Visible = true;
Label1.Text = "Oops Update was unsuccessfull!";
}
}
the problem is the database is getting updated with the previous values which was already present in the database.when i used watchpoints i found the sql statement gets loaded with the textbox values which i binded using the above database code but its not fetching the values which the user edits.
plz help.
Hey Robin,
Please check whether the code is placed under the !IsPostback condition or not.
Please bind the values under this if loop.
if(!IsPostback)
{
// Bind values
}
do this on the page load.
The values must only be bound for the first time the page is load.
In your case it seems the values to the text boxes are bound for each and every time it is loaded(even in the postback also the values are loaded.)
So, in page load you must check the condition whether the page is postback or not.
Thus add this condition while binding the values in page load.
if(!IsPostback)
Firstly check the location from where u r calling the mathod for binding values...
put it on
Page.isPostback
so it will not change the value at posting time then when u will save it the value will change
the value must be changing when u click on button and page is again loading.