I am developing a project. In this project, the rows I selected from datagridview1 should be added to the child table and deleted from there. Later, I want to be able to add the rows that I don't want to pull from datagridview2 back to datagridview1. How do I do this?
Here is my codes:
private void button1_Click(object sender, EventArgs e)
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM AçıkKalemler", con);
sda.Fill(dt);
dataGridView1.Rows.Clear();
foreach (DataRow item in dt.Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = false; // Select butonlarının seçili olmadan gelmesini sağlar.
dataGridView1.Rows[n].Cells[1].Value = item["MÜŞTERİ"].ToString();
dataGridView1.Rows[n].Cells[2].Value = item["SIPARISNUMARASI"].ToString();
dataGridView1.Rows[n].Cells[3].Value = item["SATISNUMARASI"].ToString();
dataGridView1.Rows[n].Cells[4].Value = item["KALEMTANIMI"].ToString();
dataGridView1.Rows[n].Cells[5].Value = item["PLANLANANTESLİMTARİHİ"].ToString();
dataGridView1.Rows[n].Cells[6].Value = item["SIPARISMIKTARI"].ToString();
}
}
private void button2_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow item in dataGridView1.Rows)
{
if ((bool)item.Cells[0].Value == true)
{
int n = dataGridView2.Rows.Add();
dataGridView2.Rows[n].Cells[0].Value = false;
dataGridView2.Rows[n].Cells[1].Value = item.Cells[1].Value.ToString();
dataGridView2.Rows[n].Cells[2].Value = item.Cells[2].Value.ToString();
dataGridView2.Rows[n].Cells[3].Value = item.Cells[3].Value.ToString();
dataGridView2.Rows[n].Cells[4].Value = item.Cells[4].Value.ToString();
dataGridView2.Rows[n].Cells[5].Value = item.Cells[5].Value.ToString();
dataGridView2.Rows[n].Cells[6].Value = item.Cells[6].Value.ToString();
}
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
if ((bool)dataGridView1.SelectedRows[0].Cells[0].Value == false)
{
dataGridView1.SelectedRows[0].Cells[0].Value = true;
}
else
{
dataGridView1.SelectedRows[0].Cells[0].Value = false;
}
}
private void button3_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow item in dataGridView2.Rows)
{
if ((bool)item.Cells[0].Value == true)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = false;
dataGridView1.Rows[n].Cells[1].Value = item.Cells[1].Value.ToString();
dataGridView1.Rows[n].Cells[2].Value = item.Cells[2].Value.ToString();
dataGridView1.Rows[n].Cells[3].Value = item.Cells[3].Value.ToString();
dataGridView1.Rows[n].Cells[4].Value = item.Cells[4].Value.ToString();
dataGridView1.Rows[n].Cells[5].Value = item.Cells[5].Value.ToString();
}
}
}
private void dataGridView2_MouseClick(object sender, MouseEventArgs e)
{
if ((bool)dataGridView2.SelectedRows[0].Cells[0].Value == false)
{
dataGridView2.SelectedRows[0].Cells[0].Value = true;
}
else
{
dataGridView2.SelectedRows[0].Cells[0].Value = false;
}
}
Button3 should do this.
Please help me!!
Related
Currently i am having trouble updating a row in my listview that contains both text and a timer that counts down. So far I've tried updating the entire row as well as trying to update just the text and i get an error in each instance.
System.ArgumentOutOfRangeException: InvalidArgument=Value of '0' is not valid for 'index
I am also having a slight problem with the delete button. It deletes the row as planned but it appears the information is still running in the background and haven't come across any info on how to delete the session. I pray for your guidance.
private void Confirm_Click(object sender, EventArgs e)
{
CSession newSession = new CSession();
if(PasswordText.Text == "")
{
MessageBox.Show("Password not entered");
return;
}
newSession.password = PasswordText.Text;
newSession.purchased_time = workingTimeSpan;
newSession.remaining_time = workingTimeSpan;
newSession.status = "Online";
sessionlist.Add(newSession);
PasswordText.Text = "";
TimerLabel.Text = "";
workingTimeSpan = new TimeSpan();
}
private void DisplayAllSessions()
{
listView1.Items.Clear();
foreach(CSession c in sessionlist)
{
string[] row = { c.password, c.purchased_time.ToString(), c.remaining_time.ToString(), c.status };
ListViewItem i = new ListViewItem(row);
listView1.Items.Add(i);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
foreach(CSession c in sessionlist)
{
if (c.remaining_time.TotalMinutes == 5 && !c.MessageDisplayed)
{
c.MessageDisplayed = true;
MessageBox.Show("Time almost up for client.");
}
if (c.remaining_time.TotalSeconds < 1)
{
c.status = "Offline";
}
if(c.status == "Online")
{
c.remaining_time -= oneSecond;
}
}
DisplayAllSessions();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void Reset_Click(object sender, EventArgs e)
{
}
private void updatebutton()
{
listView1.SelectedItems[0].SubItems[0].Text = PasswordText.Text;
PasswordText.Text = "";
}
private void Update_Click(object sender, EventArgs e)
{
updatebutton();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
PasswordText.Text = listView1.SelectedItems[0].Text;
TimerLabel.Text = listView1.SelectedItems[0].SubItems[1].Text;
}
private void Delete_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Do you wish to delete.", "Removing User", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
listView1.Items.Clear();
timer1.Stop();
}
}
This is what i used for the update function and delete row function.
private void updatebutton()
{
CSession updatedsession = new CSession();
int updateindex = 0;
int index = 0;
foreach (CSession lookup in sessionlist)
{
if(lookup.password == PasswordText.Text)
{
if (MessageBox.Show("Do you wish to add time.", "Adding Time",
MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
updatedsession = lookup;
updateindex = index;
}
break;
}
index++;
}
updatedsession.purchased_time = workingTimeSpan +
updatedsession.purchased_time;
updatedsession.remaining_time = workingTimeSpan +
updatedsession.remaining_time;
sessionlist[updateindex] = updatedsession;
//sortedSessionList[PasswordText.Text] = updatedsession;
PasswordText.Text = "";
TimerLabel.Text = "";
workingTimeSpan = new TimeSpan();
}
private void Update_Click(object sender, EventArgs e)
{
updatebutton();
}
private void Delete_Click(object sender, EventArgs e)
{
CSession cSession = new CSession();
if (MessageBox.Show("Do you wish to delete.", "Removing User",
MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
int index = 0;
foreach (CSession c in sessionlist)
{
if(c.password == PasswordText.Text)
{
break;
}
index++;
}
sessionlist.RemoveAt(index);
}
}
I am needing that if there is no CheckBox selected in the DataGridView the button Uno and the button Varios are disabled.
If a single CheckBox is selected, the button Uno is enabled and the button Varios disabled.
And if there is more than one CheckBox selected, the button Uno is disabled and the button Varios is enabled.
But, the following happens:
The code I use is the following:
public Form1()
{
InitializeComponent();
btnUno.Enabled = false;
btnVarios.Enabled = false;
}
To enable and disable the buttons:
private void dtgTitulo_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int contador = 0;
foreach (DataGridViewRow row in dtgTitulo.Rows)
{
if (row.Cells["Seleccione"].Value != null && row.Cells["Seleccione"].Value.Equals(true))//Columna de checks
{
contador++;
if (contador <= 0)
{
btnUno.Enabled = false;
btnVarios.Enabled = false;
}
else if (contador == 1)
{
btnUno.Enabled = true;
btnVarios.Enabled = false;
}
else
{
btnUno.Enabled = false;
btnVarios.Enabled = true;
}
}
}
}
Can someone help me? Any suggestion?
UPDATE
HOW TO I LOAD THE DATAGRIDVIEW WITH CHECKBOXES:
private DataTable Query()
{
DataTable datos = new DataTable();
SqlConnection sqlConn = new SqlConnection("STRING");
try
{
sqlConn.Open();
string consulta = "SELECT Titulo AS Título FROM V_CuetaWeb GROUP BY titulo ORDER BY titulo DESC";
SqlCommand sqlCommand = new SqlCommand(consulta, sqlConn);
SqlDataAdapter da = new SqlDataAdapter(sqlCommand);//este se encarga de inicializar el command
da.Fill(datos);
sqlConn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return datos;
}
I form_Load:
private void Form1_Load(object sender, EventArgs e)
{
ds = new DataSet();
ds.Tables.Add(Query());
ds.Tables[0].Columns.Add("Seleccione", typeof(bool));
dtgTitulo.DataSource = ds.Tables[0];
}
Try Below code:
AS per #JIMI's suggestion
private void dtgTitulo_CellMouseUp(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex != 1) return;
dtgTitulo.CommitEdit(DataGridViewDataErrorContexts.Commit);
var contador = dtgTitulo.Rows.OfType<DataGridViewRow>().Count(r => (r.Cells[1].Value != null) && ((bool)r.Cells[1].Value == true));
if (contador <= 0)
{
btnUno.Enabled = false;
btnVarios.Enabled = false;
}
else
{
if (contador == 1)
{
btnUno.Enabled = true;
btnVarios.Enabled = false;
}
else
{
btnUno.Enabled = false;
btnVarios.Enabled = true;
}
}
}
after click on checkboxes they show/hide ticks, but value in cell doesn't change immediately. call EndEdit to apply them.
private void dtgTitulo_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex < 0 || dtgTitulo.Columns[e.ColumnIndex].Name != "Seleccione")
return;
dtgTitulo.EndEdit();
int contador = 0;
foreach (DataGridViewRow row in dtgTitulo.Rows)
{
if (Equals(true, row.Cells["Seleccione"].Value))
{
contador++;
if (contador > 1)
break;
}
}
btnUno.Enabled = contador == 1;
btnVarios.Enabled = contador > 1;
}
p.s. note optimizations made to avoid unnecessary iterations
So, I'm creating a program to check names of a client. But I get this error.
"No row can be added to a DataGridView control that does not have columns. Columns must be added first.'"
I'm unsure of what to do at this point, as it's all coded correctly. and I just, I don't understand why it's not working. A friend of mine is running the EXACT same code, and his build worked flawlessly.
Here's my code.
private void PopulateDataGrideView(string Name, string Status)
{
if (base.InvokeRequired)
{
base.BeginInvoke(new MethodInvoker(() => {
DataGridViewRow dataGridViewRow = new DataGridViewRow();
if (Status == "Yes")
{
dataGridViewRow.DefaultCellStyle.BackColor = Color.LightGreen;
Status = "Name Available";
}
else if (Status == "No")
{
dataGridViewRow.DefaultCellStyle.BackColor = Color.Red;
Status = "Not Available";
if (this.VisibilityBx.Checked)
{
dataGridViewRow.Visible = false;
}
}
dataGridViewRow.CreateCells(this.checkedNames, new object[] { Name, Status });
this.checkedNames.Rows.Add(dataGridViewRow);
if (scrollBx.Checked)
{
this.checkedNames.FirstDisplayedScrollingRowIndex = this.checkedNames.RowCount - 1;
}
this.progressBar1.Increment(1);
}));
}
}
private void saveAvailableNamesToolStripMenuItem_Click(object sender, EventArgs e)
{
List<string> strs = new List<string>();
if (this.checkedNames.Rows.Count > 0)
{
foreach (DataGridViewRow row in (IEnumerable)this.checkedNames.Rows)
{
if (row.Cells[1].Value.Equals("Name Available"))
{
strs.Add(row.Cells[0].Value.ToString());
}
}
if (strs.Count > 0)
{
string currentDirectory = Environment.CurrentDirectory;
DateTime now = DateTime.Now;
File.WriteAllLines(string.Concat(currentDirectory, "\\Available Names ", now.ToString("dd-MM-yyyy HH.mm.ss tt"), ".txt"), strs);
}
}
}
private void saveTakenNamesToolStripMenuItem_Click(object sender, EventArgs e)
{
List<string> strs = new List<string>();
if (this.checkedNames.Rows.Count > 0)
{
foreach (DataGridViewRow row in (IEnumerable)this.checkedNames.Rows)
{
if (row.Cells[1].Value.Equals("Not Available"))
{
strs.Add(row.Cells[0].Value.ToString());
}
}
if (strs.Count > 0)
{
string currentDirectory = Environment.CurrentDirectory;
DateTime now = DateTime.Now;
File.WriteAllLines(string.Concat(currentDirectory, "\\Taken Names ", now.ToString("dd-MM-yyyy HH.mm.ss tt"), ".txt"), strs);
}
}
}
private void scrollBx_CheckedChanged(object sender, EventArgs e)
{
if (this.checkedNames.RowCount > 0)
{
if (this.scrollBx.Checked)
{
this.checkedNames.FirstDisplayedScrollingRowIndex = this.checkedNames.RowCount - 1;
}
else if (!this.scrollBx.Checked)
{
this.checkedNames.FirstDisplayedScrollingRowIndex = 0;
}
}
}
private void VisibilityBx_CheckedChanged(object sender, EventArgs e)
{
if (this.checkedNames.Rows.Count > 0)
{
foreach (DataGridViewRow row in (IEnumerable)this.checkedNames.Rows)
{
if (!this.VisibilityBx.Checked)
{
if (!row.Visible)
{
row.Visible = true;
}
}
else if (this.VisibilityBx.Checked)
{
if ((string)row.Cells[1].Value == "Not Available")
{
row.Visible = false;
}
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void CheckedNames_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
Use:
DataGridViewRow row = (DataGridViewRow)YOURDATAGRIDVIEW.Rows[0].Clone();
to create a new row to be added, instead of:
DataGridViewRow dataGridViewRow = new DataGridViewRow();
This preserves the column names in the new row.
You should no longer need this line of code:
dataGridViewRow.CreateCells(this.checkedNames, new object[] { Name, Status });
Also ensure that data grid view has columns, which is an often cause of this error.
I am selecting a row from Gridview, working good except for checkbox, like I am assigning value of checkbox retreived from gridview to checkbox that is placed on web form but it isn't represented by checkbox on form, it shows empty checkbox in every case
if (gridviewDesignations.SelectedRow.Cells[5].Text == " ")
{
chkIsHead.Text = gridviewDesignations.SelectedRow.Cells[5].Text;
}
in short, checkbox is not picking value from gridview
Update:
tried this too:
CheckBox chkIsHead = (CheckBox) gridviewDesignations.SelectedRow.Cells[5].Controls[0];
if (chkIsHead.Checked == false)
{
chkIsHead.Checked = false;
}
else
{
chkIsHead.Checked = true;
}
Update:
my full code:
public partial class frmDesignations : System.Web.UI.Page
{
AccessibleVariables accessVariables = new AccessibleVariables(); //Used to access global variables
public void Clear(params TextBox[] txtBoxes)
{
foreach (TextBox txtbx in txtBoxes)
{
txtbx.Text = "";
}
}
public void fillddlDepartments()
{
ManageDepartmentsBizz mngDepBizz = new ManageDepartmentsBizz();
DataSet ds = (DataSet)mngDepBizz.SelectDepartments();
if (ds.Tables[0].Rows.Count != 0)
{
ddlDepartments.DataValueField = "DepID";
ddlDepartments.DataTextField = "DepName";
ddlDepartments.DataSource = ds.Tables[0];
// ddlDepartments.SelectedIndex = -1;
ddlDepartments.DataBind();
ddlDepartments.Items.Insert(0, "--Select--");
}
//else
// ddlDepartments.SelectedIndex = -1;
}
protected void Page_Load(object sender, EventArgs e)
{
if (Session.Count <= 0)
{
Response.Redirect("login.aspx");
}
lblMsgPopUp.Visible = false;
if (!IsPostBack)
{
fillddlDepartments();
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
int DepartmentID = Convert.ToInt32(ddlDepartments.SelectedValue);
bool IsHead = Convert.ToBoolean(chkIsHead.Checked);
DesignationsBizz DesigBizz = new DesignationsBizz(-1, txtTitle.Text, DepartmentID, txtContactNo.Text, IsHead);
//-1 is bogus,used to fill parameters criteria i.e no of params
ManageDesignationsBizz mngDesigBizz = new ManageDesignationsBizz();
bool Result = mngDesigBizz.Insert(DesigBizz);
if (Result == true)
{
HiddenFieldSetMessage.Value = "Saved";
HiddenFieldShowMessage.Value = "True";
Clear(txtTitle, txtSelectedID, txtContactNo);
}
else
{
HiddenFieldSetMessage.Value = "RecordAlreadyExists";
HiddenFieldShowMessage.Value = "True";
}
}
catch (Exception)
{
HiddenFieldSetMessage.Value = "NotSaved";
HiddenFieldShowMessage.Value = "True";
}
}
protected void btnSearchPopup_Click(object sender, EventArgs e)
{
string DesignationTitle = txtDesignationPopUp.Text;
ManageDesignationsBizz mngDepsBizz = new ManageDesignationsBizz();
DataSet ds = (DataSet)mngDepsBizz.Select(DesignationTitle);
if (ds.Tables[0].Rows.Count != 0)
{
lblMsgPopUp.Visible = false;
gridviewDesignations.DataSource = ds.Tables[0];
gridviewDesignations.DataBind();
gridviewDesignations.Visible = true;
}
else
{
lblMsgPopUp.Visible = true;
gridviewDesignations.Visible = false;
}
}
protected void gridviewDesignations_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
string DesignationTitle = txtDesignationPopUp.Text;
ManageDesignationsBizz mngDepBizz = new ManageDesignationsBizz();
DataSet ds = (DataSet)mngDepBizz.Select(DesignationTitle);
if (ds.Tables[0].Rows.Count != 0)
{
gridviewDesignations.PageIndex = e.NewPageIndex;
gridviewDesignations.DataSource = ds.Tables[0];
gridviewDesignations.DataBind();
gridviewDesignations.Visible = true;
}
}
protected void btnEdit_Click(object sender, EventArgs e)
{
if (gridviewDesignations.SelectedRow != null)
{
if (gridviewDesignations.SelectedRow.Cells[1].Text == " ")
{
txtSelectedID.Text = string.Empty;
}
else
{
txtSelectedID.Text = gridviewDesignations.SelectedRow.Cells[1].Text;
}
if (gridviewDesignations.SelectedRow.Cells[2].Text == " ")
{
txtTitle.Text = string.Empty;
}
else
{
txtTitle.Text = gridviewDesignations.SelectedRow.Cells[2].Text;
}
if (gridviewDesignations.SelectedRow.Cells[3].Text == " ")
{
ddlDepartments.SelectedValue = string.Empty;
}
else
{
ddlDepartments.SelectedValue = gridviewDesignations.SelectedRow.Cells[3].Text;
accessVariables.DepID = Convert.ToInt32(gridviewDesignations.SelectedRow.Cells[3].Text);
ViewState["depID"] = accessVariables.DepID;
}
if (gridviewDesignations.SelectedRow.Cells[4].Text == " ")
{
txtContactNo.Text = string.Empty;
}
else
{
txtContactNo.Text = gridviewDesignations.SelectedRow.Cells[4].Text;
}
CheckBox chkIsHead = (CheckBox) gridviewDesignations.SelectedRow.Cells[5].Controls[0];
if (chkIsHead.Checked == false)
{
chkIsHead.Checked = false;
}
else
{
chkIsHead.Checked = true;
}
gridviewDesignations.DataBind();
gridviewDesignations.SelectedIndex = -1;
HiddenFieldShowHideButtons.Value = "True";
}
}
protected void btnUpdatePopUp_Click(object sender, EventArgs e)
{
try
{
int id = Convert.ToInt32(txtSelectedID.Text);
int DepartmentID = Convert.ToInt32(ddlDepartments.SelectedValue);
bool IsHead = Convert.ToBoolean(chkIsHead.Checked);
DesignationsBizz DesigBizz = new DesignationsBizz(id, txtTitle.Text, DepartmentID, txtContactNo.Text, IsHead );
ManageDesignationsBizz mngDesigBizz = new ManageDesignationsBizz();
bool Result = mngDesigBizz.Update(DesigBizz);
if (Result == true)
{
HiddenFieldSetMessage.Value = "Updated";
HiddenFieldShowMessage.Value = "True";
Clear(txtSelectedID, txtTitle, txtContactNo);
}
else
{
HiddenFieldSetMessage.Value = "NotUpdated";
HiddenFieldShowMessage.Value = "True";
}
}
catch (Exception)
{
HiddenFieldSetMessage.Value = "NotUpdated";
HiddenFieldShowMessage.Value = "True";
}
}
protected void btnDeletePopUp_Click(object sender, EventArgs e)
{
try
{
int ID = Convert.ToInt32(txtSelectedID.Text.Trim());
ManageDesignationsBizz mngDepBizz = new ManageDesignationsBizz();
mngDepBizz.Delete(ID);
Clear(txtTitle, txtSelectedID, txtContactNo);
HiddenFieldSetMessage.Value = "Deleted";
HiddenFieldShowMessage.Value = "True";
}
catch (Exception)
{
HiddenFieldSetMessage.Value = "NotDeleted";
HiddenFieldShowMessage.Value = "True";
}
}
protected void btnClosePopup_Click(object sender, EventArgs e)
{
Clear(txtTitle, txtSelectedID, txtContactNo);
}
protected void ddlDepartments_SelectedIndexChanged(object sender, EventArgs e)
{
if (txtSelectedID.Text != "")
{
accessVariables.DepID = Convert.ToInt32(ddlDepartments.SelectedValue);
ViewState["depID"] = accessVariables.DepID;
}
else
{
accessVariables.DepID = Convert.ToInt32(ddlDepartments.SelectedValue);
ViewState["depID"] = accessVariables.DepID;
}
}
protected void chkIsHead_CheckedChanged(object sender, EventArgs e)
{
if (txtSelectedID.Text != "")
{
int DepID = Convert.ToInt32(ViewState["depID"]);
ManageDesignationsBizz mngDesig = new ManageDesignationsBizz();
bool isHead = mngDesig.SelectIsHeadExistsByDepID(DepID);
if (isHead == true)
{
HiddenFieldSetMessage.Value = "HeadExists";
HiddenFieldShowMessage.Value = "True";
chkIsHead.Checked = false;
}
}
else
{
int DepID = Convert.ToInt32(ViewState["depID"]);
ManageDesignationsBizz mngDesig = new ManageDesignationsBizz();
bool isHead = mngDesig.SelectIsHeadExistsByDepID(DepID);
if (isHead == true)
{
HiddenFieldSetMessage.Value = "HeadExists";
HiddenFieldShowMessage.Value = "True";
chkIsHead.Checked = false;
}
}
}
}
I believe the following is what you need to do:
Then select EditProgrammatically.
I'm not sure if yoou'll need to manually put data into the grid though but that won't be too hard. Examples can be seen here : http://msdn.microsoft.com/en-us/library/system.data.datatable(v=vs.110).aspx
PS : You can set the DataSource in the grid view to the DataTable.
I am creating a web site but anytime I run this code, an exception is raised:
#Exception Details: System.IndexOutOfRangeException: There is no row at position 0.
How do I solve this problem?
public partial class Employee_frmSearchClaims : System.Web.UI.Page
{
int empno;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["empno"] != null)
empno = int.Parse(Session["empno"].ToString());
if (!IsPostBack)
{
DataRow r = AddPoliciesOnEmployees.GetPolicyOnEmployee(empno).Tables[0].Rows[0];
Label7.Text = r["policyname"].ToString();
Label8.Text = r["policyid"].ToString();
Label9.Text = r["policyamount"].ToString();
Label10.Text = r["TotalAmount"].ToString();
Label11.Text = r["pstartdate"].ToString();
}
}
protected void btnapplicy_Click(object sender, EventArgs e)
{
if (ddlReason.SelectedItem.ToString() != "--Select--")
{
if (ddlReason.SelectedItem.Text == "Death")
{
Session["Reason"] = ddlReason.SelectedItem.Text;
Response.Redirect("frmDeathCliam.aspx");
}
else if (ddlReason.SelectedItem.Text == "Accident")
{
Session["Reason"] = ddlReason.SelectedItem.Text;
Response.Redirect("frmAccidentClaim.aspx");
}
else
{
Session["Reason"] = ddlReason.SelectedItem.Text;
Response.Redirect("frmCompleteClaim.aspx");
}
}
else
lblmsg.Text = "You have to select the reason";
}
}
You need to make sure your table contains rows first:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["empno"] != null)
empno = int.Parse(Session["empno"].ToString());
if (!IsPostBack)
{
var ds = AddPoliciesOnEmployees.GetPolicyOnEmployee(empno);
if (ds.Tables[0].Rows.Count > 0)
{
DataRow r = ds.Tables[0].Rows[0];
Label7.Text = r["policyname"].ToString();
Label8.Text = r["policyid"].ToString();
Label9.Text = r["policyamount"].ToString();
Label10.Text = r["TotalAmount"].ToString();
Label11.Text = r["pstartdate"].ToString();
}
}
}