Search This Blog

Friday, December 9, 2011

Retrive the data from Radio buttons in C#

Anyone solve my problem,
I want to retrieve data in RadioButtonList from Database in DetailsView like in Database it is either True/False.Here my problem is i want to show data in  RadioButtonList  as checked format and ten i want to update the changed value.

Plz solve my problem.

My Code is here

Aspx
---------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EditArticlenew.aspx.cs" Inherits="EditArticlenew" %>
<%@ Register Src="~/Admin/Header.ascx" TagName="header" TagPrefix="hd" %>
<%@ Register Src="~/Footer.ascx" TagName="Footer" TagPrefix="FT" %>
<%@ Register Src="~/Admin/AdminMenu.ascx" TagName="AdminMenu" TagPrefix="Menu" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>:: Welcome to Neerjaal ::</title>
</head>
<body>
    <center>
    <form id="frmAddArticle" runat="server">
    <div>
       <table border="0" cellspacing="0" cellpadding="0" style="border-collapse:collapse" width="800">
       <tr>
            <td style="width:100%">
                <hd:header ID="Header1" runat="server" />
            </td>
        </tr>
       <tr>
        <td style="width:800px">
            <table width="800px" border="0" cellspacing="0" cellpadding="1" style="border-collapse:collapse; background-color:#C2EBF8">
                 <tr>
                    
                    <td style="width:800px" align="left" valign="top">
                        <table width="800px" border="0" cellspacing="0" cellpadding="0" style="border-collapse:collapse;">
                        <tr>
                            <td colspan="3" align="center">EDIT ARTICLE</td>
                        </tr>
                        <tr>
                            <td colspan="3" align="center"><asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
                                <asp:Button ID="btnEdit" runat="server" onclick="btnEdit_Click" Text="Edit" />
                                <asp:Button ID="btnUpdate" runat="server" onclick="btnUpdate_Click" 
                                    Text="Update" />
                                <asp:Button ID="btnCancel0" runat="server" onclick="btnCancel_Click" 
                                    Text="Cancel" />
                            </td>
                        </tr>
                        <tr>
                            <td colspan="3" align="center">
                                <asp:DetailsView ID="DetailsView1" runat="server" Height="100px" 
                                onitemupdating="DetailsView1_ItemUpdating" Width="600px" 
                                    AutoGenerateRows="False" ondatabound="DetailsView1_DataBound">
                                <Fields>
                                    <asp:BoundField HeaderText="ArticleId" DataField="Article_Id" ReadOnly="True" />
                                    <asp:BoundField HeaderText="ArticleName" DataField="Article_Name" />
                                    <asp:BoundField HeaderText="ArticleTitle" DataField="Article_Title" />
                                    <asp:BoundField HeaderText="Date Of Publishing" DataField="Article_DOP" />
                                    <asp:BoundField HeaderText="Date Of Posting" DataField="Article_DOPOS" />
                                    <asp:BoundField HeaderText="Article Source" DataField="Article_Source" />
                                    <asp:BoundField HeaderText="Synopsis" DataField="Article_Synopsis" />
                                    <asp:BoundField HeaderText="Full Text" DataField="Article_FullText" />
                                    <asp:BoundField HeaderText="PastTab Text" DataField="PastTabText" />
                                    <asp:BoundField HeaderText="Article Url" DataField="Article_URL" />
                                   
                                    <asp:TemplateField HeaderText="IsExternal" >
                                        <ItemTemplate>
                                            <asp:Label ID="Label4" runat="server" Text='<%#Eval("IsExternal") %>'></asp:Label>
                                            <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" DataValueField='<%#Eval("IsExternal") %>'>
                                                <asp:ListItem>Yes</asp:ListItem>
                                                <asp:ListItem>No</asp:ListItem>
                                            </asp:RadioButtonList>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    
                                    <asp:TemplateField HeaderText="IsWaterEssay">
                                        <ItemTemplate>
                                            <asp:Label ID="Label5" runat="server" Text='<%#Eval("IsWaterEssay") %>'></asp:Label>
                                            <asp:RadioButtonList ID="RadioButtonList2" runat="server" RepeatDirection="Horizontal">
                                                <asp:ListItem Value="1">Yes</asp:ListItem>
                                                <asp:ListItem Value="0">No</asp:ListItem>
                                            </asp:RadioButtonList>
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:Label ID="Label5" runat="server" Text='<%#Eval("IsWaterEssay") %>'></asp:Label>
                                            <asp:RadioButtonList ID="RadioButtonList2" runat="server" RepeatDirection="Horizontal" DataValueField='<%#Bind("IsWaterEssay") %>'>
                                                <asp:ListItem Selected='<%=Eval("IsWaterEssay") %>'>Yes</asp:ListItem>
                                                <asp:ListItem>No</asp:ListItem>
                                            </asp:RadioButtonList>
                                        </EditItemTemplate>
                                        
                                    </asp:TemplateField>
                                    
                                </Fields> 
                                </asp:DetailsView>
                                
                            </td>
                        </tr>
                 
                </table> 
               </td>
                 </tr>               
                                                             
            </table>
        </td>
       </tr>
      </table>
      <div style="text-align:center"><ft:Footer ID="Footer" runat="server" /></div> 
    </div>
    </form>
    </center>
</body>
</html>
Aspx.cs
---------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;
using NeerjaalAPI;
using Neerjaal;
using Utility.Neerjaal;

public partial class Admin_EditTankaSurvay: GloblizationBasePage
{
    private string TankaID
    {
        get
        {
            if (ViewState["TankaID"] != null)

                return (string)ViewState["TankaID"];
            else
                return null;
        }
        set
        {
            ViewState["TankaID"] = value;
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
    }
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            try
            {
                if (Request.QueryString["sid"] != null)
                {
                    this.TankaID = Request.QueryString["sid"].ToString();
                    NeerjaalAPI.IDParsing wb = new NeerjaalAPI.IDParsing();
                    wb.connectionString = ConfigurationManager.ConnectionStrings["ConnString1"].ToString();
                    string WbName = wb.FindWaterBody(TankaID);
                    lblWBName.Text = WbName;
                    BindData(TankaID);
                }
            }
            catch (Exception exp)
            {
                lblMessage.Text = exp.Message;

            }

        }
    }
    protected void Btn_UpdateTankaSurvay(object sender, EventArgs e)
    {
        string message = "";

        try
        {
            using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope())
            {

                int wbType = Convert.ToInt32(Session["wbType"]);
                if (wbType == 3)
                {
                    WaterBodySurvay srvWB = WaterBodyManager.UpdateSurvayWaterBody(WaterBodyType.CircularTanka);
                    SurvayTanka srvTanka = (SurvayTanka)srvWB;
                    srvTanka.SurvayId = TankaID;
                    if (Is_WaterBody_Funcational.Text.Equals("Yes"))
                    {
                        if (Att_Taste.Text != "")
                        {
                            srvTanka.ListAttributeId.Add(Att_Taste1.Value);
                            srvTanka.AttributeValues.Add(Att_Taste.Text);
                        }
                        if (Att_Ordour.Text != "")
                        {
                            srvTanka.ListAttributeId.Add(Att_Ordour1.Value);
                            srvTanka.AttributeValues.Add(Att_Ordour.Text);
                        }
                        if (Att_Color.Text != "")
                        {
                            srvTanka.ListAttributeId.Add(Att_Color1.Value);
                            srvTanka.AttributeValues.Add(Att_Color.Text);
                        }
                        if (Att_Turbidity.Text != "")
                        {
                            srvTanka.ListAttributeId.Add(Att_Turbidity1.Value);
                            srvTanka.AttributeValues.Add(Att_Turbidity.Text);
                        }
                        if (Att_TDS.Text != "")
                        {
                            srvTanka.ListAttributeId.Add(Att_TDS1.Value);
                            srvTanka.AttributeValues.Add(Att_TDS.Text);
                        }
                        if (Att_Ph.Text != "")
                        {
                            srvTanka.ListAttributeId.Add(Att_Ph1.Value);
                            srvTanka.AttributeValues.Add(Att_Ph.Text);
                        }
                        if (Att_Chloride.Text != "")
                        {
                            srvTanka.ListAttributeId.Add(Att_Chloride1.Value);
                            srvTanka.AttributeValues.Add(Att_Chloride.Text);
                        }
                        if (Att_Flouride.Text != "")
                        {
                            srvTanka.ListAttributeId.Add(Att_Flouride1.Value);
                            srvTanka.AttributeValues.Add(Att_Flouride.Text);
                        }
                        if (Att_Hardness.Text != "")
                        {
                            srvTanka.ListAttributeId.Add(Att_Hardness1.Value);
                            srvTanka.AttributeValues.Add(Att_Hardness.Text);
                        }
                        if (Att_Nitrate.Text != "")
                        {
                            srvTanka.ListAttributeId.Add(Att_Nitrate1.Value);
                            srvTanka.AttributeValues.Add(Att_Nitrate.Text);
                        }
                        if (Att_Iron.Text != "")
                        {
                            srvTanka.ListAttributeId.Add(Att_Iron1.Value);
                            srvTanka.AttributeValues.Add(Att_Iron.Text);
                        }
                        //if (Att_Coliform.Text != "")
                        //{
                        //    srvTanka.ListAttributeId.Add(Att_Coliform1.Value);
                        //    srvTanka.AttributeValues.Add(Att_Coliform.Text);
                        //}
                        //if (Att_Ecoli.Text != "")
                        //{
                        //    srvTanka.ListAttributeId.Add(Att_Ecoli1.Value);
                        //    srvTanka.AttributeValues.Add(Att_Ecoli.Text);
                        //}
                        CheckBox chkAttrGa = Att_Gastroin;
                        string chkGatroint = "";
                        if (chkAttrGa.Checked)
                        {
                            chkGatroint = "yes";
                            srvTanka.ListAttributeId.Add(Att_Gastroin1.Value);
                            srvTanka.AttributeValues.Add(chkGatroint);
                        }
                        else
                        {
                            chkGatroint = "no";
                            srvTanka.ListAttributeId.Add(Att_Gastroin1.Value);
                            srvTanka.AttributeValues.Add(chkGatroint);
                        }
                        CheckBox chkattDiarr = Att_Diarrhea;
                        string Diarrhea = "";
                        if (chkattDiarr.Checked)
                        {
                            Diarrhea = "yes";
                            srvTanka.ListAttributeId.Add(Att_Diarrhea1.Value);
                            srvTanka.AttributeValues.Add(Diarrhea);
                        }
                        else
                        {
                            Diarrhea = "no";
                            srvTanka.ListAttributeId.Add(Att_Diarrhea1.Value);
                            srvTanka.AttributeValues.Add(Diarrhea);
                        }
                        CheckBox chkFlourosis = Att_Fluorosis;
                        string flourosis = "";
                        if (chkFlourosis.Checked)
                        {
                            flourosis = "yes";
                            srvTanka.ListAttributeId.Add(Att_Fluorosis1.Value);
                            srvTanka.AttributeValues.Add(flourosis);
                        }
                        else
                        {
                            flourosis = "no";
                            srvTanka.ListAttributeId.Add(Att_Fluorosis1.Value);
                            srvTanka.AttributeValues.Add(flourosis);
                        }
                    }
                    srvTanka.SourceOfWater = SourceOfWater.Text;
                    if (amountOfWater.Text == "")
                    {
                        srvTanka.AmountOfWater = "0";
                    }
                    else
                    {
                        srvTanka.AmountOfWater = amountOfWater.Text;
                    }
                    srvTanka.IsWaterBodyFunctional = Is_WaterBody_Funcational.Text;
                    srvTanka.IsMaintenanceRequired = Is_Maintenance_Required.Text;
                    srvTanka.SurvayRemarks = Survey_Remarks.Text;
                    if (Survay_MaintCoast.Text == "")
                    {
                        srvTanka.MaintenanceCost = 0;
                    }
                    else
                    {
                        srvTanka.MaintenanceCost = Convert.ToDecimal(Survay_MaintCoast.Text);
                    }
                    srvTanka.UpdateSurvay(out message);
                    scope.Complete();
                }
                else
                {
                    WaterBodySurvay srvWB = WaterBodyManager.UpdateSurvayWaterBody(WaterBodyType.RectangularTanka);
                    SurvayTanka srvTanka = (SurvayTanka)srvWB;
                    srvTanka.SurvayId = TankaID;
                    if (Att_Taste.Text != "")
                    {
                        srvTanka.ListAttributeId.Add(Att_Taste1.Value);
                        srvTanka.AttributeValues.Add(Att_Taste.Text);
                    }
                    if (Att_Ordour.Text != "")
                    {
                        srvTanka.ListAttributeId.Add(Att_Ordour1.Value);
                        srvTanka.AttributeValues.Add(Att_Ordour.Text);
                    }
                    if (Att_Color.Text != "")
                    {
                        srvTanka.ListAttributeId.Add(Att_Color1.Value);
                        srvTanka.AttributeValues.Add(Att_Color.Text);
                    }
                    if (Att_Turbidity.Text != "")
                    {
                        srvTanka.ListAttributeId.Add(Att_Turbidity1.Value);
                        srvTanka.AttributeValues.Add(Att_Turbidity.Text);
                    }
                    if (Att_TDS.Text != "")
                    {
                        srvTanka.ListAttributeId.Add(Att_TDS1.Value);
                        srvTanka.AttributeValues.Add(Att_TDS.Text);
                    }
                    if (Att_Ph.Text != "")
                    {
                        srvTanka.ListAttributeId.Add(Att_Ph1.Value);
                        srvTanka.AttributeValues.Add(Att_Ph.Text);
                    }
                    if (Att_Chloride.Text != "")
                    {
                        srvTanka.ListAttributeId.Add(Att_Chloride1.Value);
                        srvTanka.AttributeValues.Add(Att_Chloride.Text);
                    }
                    if (Att_Flouride.Text != "")
                    {
                        srvTanka.ListAttributeId.Add(Att_Flouride1.Value);
                        srvTanka.AttributeValues.Add(Att_Flouride.Text);
                    }
                    if (Att_Hardness.Text != "")
                    {
                        srvTanka.ListAttributeId.Add(Att_Hardness1.Value);
                        srvTanka.AttributeValues.Add(Att_Hardness.Text);
                    }
                    if (Att_Nitrate.Text != "")
                    {
                        srvTanka.ListAttributeId.Add(Att_Nitrate1.Value);
                        srvTanka.AttributeValues.Add(Att_Nitrate.Text);
                    }
                    if (Att_Iron.Text != "")
                    {
                        srvTanka.ListAttributeId.Add(Att_Iron1.Value);
                        srvTanka.AttributeValues.Add(Att_Iron.Text);
                    }
                    //if (Att_Coliform.Text != "")
                    //{
                    //    srvTanka.ListAttributeId.Add(Att_Coliform1.Value);
                    //    srvTanka.AttributeValues.Add(Att_Coliform.Text);
                    //}
                    //if (Att_Ecoli.Text != "")
                    //{
                    //    srvTanka.ListAttributeId.Add(Att_Ecoli1.Value);
                    //    srvTanka.AttributeValues.Add(Att_Ecoli.Text);
                    //}
                    CheckBox chkAttrGa = Att_Gastroin;
                    string chkGatroint = "";
                    if (chkAttrGa.Checked)
                    {
                        chkGatroint = "yes";
                        srvTanka.ListAttributeId.Add(Att_Gastroin1.Value);
                        srvTanka.AttributeValues.Add(chkGatroint);
                    }
                    else
                    {
                        chkGatroint = "no";
                        srvTanka.ListAttributeId.Add(Att_Gastroin1.Value);
                        srvTanka.AttributeValues.Add(chkGatroint);
                    }
                    CheckBox chkattDiarr = Att_Diarrhea;
                    string Diarrhea = "";
                    if (chkattDiarr.Checked)
                    {
                        Diarrhea = "yes";
                        srvTanka.ListAttributeId.Add(Att_Diarrhea1.Value);
                        srvTanka.AttributeValues.Add(Diarrhea);
                    }
                    else
                    {
                        Diarrhea = "no";
                        srvTanka.ListAttributeId.Add(Att_Diarrhea1.Value);
                        srvTanka.AttributeValues.Add(Diarrhea);
                    }
                    CheckBox chkFlourosis = Att_Fluorosis;
                    string flourosis = "";
                    if (chkFlourosis.Checked)
                    {
                        flourosis = "yes";
                        srvTanka.ListAttributeId.Add(Att_Fluorosis1.Value);
                        srvTanka.AttributeValues.Add(flourosis);
                    }
                    else
                    {
                        flourosis = "no";
                        srvTanka.ListAttributeId.Add(Att_Fluorosis1.Value);
                        srvTanka.AttributeValues.Add(flourosis);
                    }
                    srvTanka.SourceOfWater = SourceOfWater.Text;
                    if (amountOfWater.Text == "")
                    {
                        srvTanka.AmountOfWater = "0";
                    }
                    else
                    {
                        srvTanka.AmountOfWater = amountOfWater.Text;
                    }
                    srvTanka.IsMaintenanceRequired = Is_Maintenance_Required.Text;
                    srvTanka.SurvayRemarks = Survey_Remarks.Text;
                    if (Survay_MaintCoast.Text == "")
                    {
                        srvTanka.MaintenanceCost = 0;
                    }
                    else
                    {
                        srvTanka.MaintenanceCost = Convert.ToDecimal(Survay_MaintCoast.Text);
                    }
                    srvTanka.UpdateSurvay(out message);
                    scope.Complete();
                }
            }            
        }
        catch (System.Transactions.TransactionAbortedException tbe)
        {
            ;
        }

        lblMessage.Text = message;
    }
    protected void Btn_Del(object sender, EventArgs e)
    {
        int wbType = Convert.ToInt32(Session["wbType"]);
        if (wbType == 3)
        {
            WaterBodySurvay srvWB = WaterBodyManager.UpdateSurvayWaterBody(WaterBodyType.CircularTanka);
            SurvayTanka srvCircTanka = (SurvayTanka)srvWB;
            srvCircTanka.SurvayId = TankaID;
            string message = null;
            srvCircTanka.DeleteWaterBody(out message);
            lblMessage.Text = message;
        }
        else
        {
            WaterBodySurvay srvWB = WaterBodyManager.UpdateSurvayWaterBody(WaterBodyType.RectangularTanka);
            SurvayTanka srvRecTanka = (SurvayTanka)srvWB;
            srvRecTanka.SurvayId = TankaID;
            string message = null;
            srvRecTanka.DeleteWaterBody(out message);
            lblMessage.Text = message;
        }
    }
    private void BindData(string sid)
    {
        string selSuvValue = "select * from Survay where Survay_Id=@TankaID";
        string selTankaServ = "select * from Survay_Tanka where Survay_Id=@sid";
        string survayId = sid;
        string path = null;
        NeerjaalAPI.Locality loc = new NeerjaalAPI.Locality();
        loc.connectionString = ConfigurationManager.ConnectionStrings["ConnString1"].ToString();
        int lid = Convert.ToInt32(Session["locId"].ToString());
        path = loc.ComputedPath(lid);
        lblLocality.Text = path;
        string selAtrValue = "select * from AttributeValue where Survay_Id=@survayId";
        string connection = ConfigurationManager.ConnectionStrings["connString1"].ToString();
        SqlConnection conn = new SqlConnection(connection);
        SqlDataAdapter da = new SqlDataAdapter("TankaID", conn);
        SqlDataAdapter da1 = new SqlDataAdapter("TankaID", conn);
        SqlDataAdapter da2 = new SqlDataAdapter("TankaID", conn);
        da.SelectCommand.CommandText = selSuvValue;
        da1.SelectCommand.CommandText = selTankaServ;
        da2.SelectCommand.CommandText = selAtrValue;
        da.SelectCommand.Parameters.AddWithValue("@TankaID", TankaID);
        da1.SelectCommand.Parameters.AddWithValue("@sid", sid);        
        da2.SelectCommand.Parameters.AddWithValue("@survayId", survayId);
        DataTable dt = new DataTable();
        DataTable dt1 = new DataTable();
        DataTable dt2 = new DataTable();
        try
        {
            da.Fill(dt);
            da1.Fill(dt1);
            da2.Fill(dt2);

        }
        catch (Exception exp)
        {
            lblMessage.Text = exp.Message;
        }
        finally
        {
            conn.Dispose();
            da.Dispose();
            da1.Dispose();
        }
        if (dt.Rows.Count > 0)
        {
            //DateTime dt = Convert.ToDateTime(dt.Rows[0]["RecordDate"].ToString()).Date;

            DateTime date = Convert.ToDateTime(dt.Rows[0]["RecordDate"].ToString());
            string month = Convert.ToString(date.Month);
            string day = Convert.ToString(date.Day);
            string year = Convert.ToString(date.Year);
            string strDate = month + "/" + day + "/" + year;
            RecordDate.Text = strDate;
            Is_WaterBody_Funcational.Text = dt.Rows[0]["Is_WaterBody_Funcational"].ToString();
            Is_Maintenance_Required.Text = dt.Rows[0]["Is_Maintenance_Required"].ToString();
            Survey_Remarks.Text = dt.Rows[0]["Survey_Remarks"].ToString();
            Survay_MaintCoast.Text = dt.Rows[0]["MaintenanceCost"].ToString();

            SourceOfWater.Text = dt1.Rows[0]["SourceOfWater"].ToString();
            amountOfWater.Text = dt1.Rows[0]["AmountOfWater"].ToString();

            Att_Taste.Text = dt2.Rows[0]["Attribute_Value"].ToString();
            Att_Ordour.Text = dt2.Rows[1]["Attribute_Value"].ToString();
            Att_Color.Text=dt2.Rows[2]["Attribute_Value"].ToString();
            Att_Turbidity.Text = dt2.Rows[3]["Attribute_Value"].ToString();
            Att_TDS.Text = dt2.Rows[4]["Attribute_Value"].ToString();
            Att_Ph.Text = dt2.Rows[5]["Attribute_Value"].ToString();
            Att_Chloride.Text = dt2.Rows[6]["Attribute_Value"].ToString();
            Att_Flouride.Text = dt2.Rows[7]["Attribute_Value"].ToString();
            Att_Hardness.Text = dt2.Rows[8]["Attribute_Value"].ToString();
            Att_Nitrate.Text = dt2.Rows[9]["Attribute_Value"].ToString();
            Att_Iron.Text = dt2.Rows[10]["Attribute_Value"].ToString();
            //Att_Coliform.Text = dt2.Rows[11]["Attribute_Value"].ToString();
            //Att_Ecoli.Text = dt2.Rows[12]["Attribute_Value"].ToString();
            
            if (dt2.Rows[11]["Attribute_Value"].ToString().Equals("yes"))
            {
                Att_Gastroin.Checked = true;
            }
            else
            {
                Att_Gastroin.Checked = false;
            }
            if (dt2.Rows[12]["Attribute_Value"].ToString().Equals("yes"))
            {
                Att_Diarrhea.Checked = true;
            }
            else
            {
                Att_Diarrhea.Checked = false;
            }
            if (dt2.Rows[13]["Attribute_Value"].ToString().Equals("yes"))
            {
                Att_Fluorosis.Checked = true;
            }
            else
            {
                Att_Fluorosis.Checked = false;
            }
        }
    }
}

try this

RadioButtonList1 and RadioButtonList2 in this which one you would like to set true. If you would like to set one as true in each individual list first check for the RadioButtonList from gridview and then try some thing like this based on your condition

RadioButtonList2.items[0].selected=true
 
 
 
 

No comments:

Post a Comment