Sunday, December 6, 2020

Assorted LINQ Solutions

Here I am trying to bring all problems which are commonly faced by developers and look for help for solutions. These are the scenarios which I faced at different times and got solution on my own or somewhere on google. I will keep updating it as I recollect it from my experience.

1) Convert all the comma separated numbers in a string to list of int.
    e.g. string numbers = "1,2,3";

Covert above numbers to list of int

var list = Array.ConvertAll<string, int>(numbers.Split(','), Convert.ToInt32);

Above solution works well till the time it is just comma separated numbers. If you add trailing comma or any other character in this string, it will throw exception.
For example: 
string numbers = "1,2,3,"; 
            OR
string numbers = "1,a,3";


Sunday, January 3, 2010

DataList with Radiobutton column (RadioButton selection solution)

aspx page:


<asp:DataList ID="dlstTest" runat="server" OnItemDataBound="dlstTest_ItemDataBound" >
<ItemTemplate >
<asp:RadioButton ID="rdbDataList" runat="server" />

< asp:Label id="lblnm" runat="server" Text="Name:"> </asp:Label >

< asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
<br />

< asp:Label ID="lblLastName" runat="server" Text='<%# Eval("LastName") %>'> </asp:Label >
< div id="divTest" runat="server" > </div>
</ItemTemplate >
</asp:DataList >


JavaScript code for Radiobutton selection:


< script type="text/javascript" language="javascript" >
function CheckRdb(spanChk){

var oItem = spanChk.children;
var theBox= (spanChk.type=="radio") ?
spanChk : spanChk.children.item[0];
xState=theBox.unchecked;
elm=theBox.form.elements;
for(i=0;iif(elm[i].type=="radio" &&
elm[i].id!=theBox.id)
{
elm[i].checked=xState;
}
}

< /Script >


Code behind:


protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{


DataTable dtTest = new DataTable();
dtTest.Columns.Add("Name");
dtTest.Columns.Add("LastName");
DataRow drTest = dtTest.NewRow();
drTest["Name"] = "Dushyant";
drTest["LastName"] = "Kotecha";
dtTest.Rows.Add(drTest);
drTest = dtTest.NewRow();
drTest["Name"] = "Debasis";
drTest["LastName"] = "Misra";
dtTest.Rows.Add(drTest);
drTest = dtTest.NewRow();
drTest["Name"] = "Jigar";
drTest["LastName"] = "Jokhakar";
dtTest.Rows.Add(drTest);

}

}



protected void dlstTest_ItemDataBound(object sender, DataListItemEventArgs e)
{
RadioButton rdb;
rdb = (RadioButton)e.Item.FindControl("rdbDataList");
if (rdb != null)
rdb.Attributes.Add("onclick", "CheckRdb(this);");
}