Convert DataTable to List in c#

DataTable dt = ‘…some service called to fill this datatable….’
List<DemoModel> demoDetails = new List<DemoModel>();
demoDetails = ConvertDataTable<DemoModel>(dt);

 

public static List<T> ConvertDataTable<T>(DataTable dt)
{
List<T> data = new List<T>();
foreach (DataRow row in dt.Rows)
{
T item = GetItem<T>(row);
data.Add(item);
}
return data;
}

 

private static T GetItem<T>(DataRow dr)
{
Type temp = typeof(T);
T obj = Activator.CreateInstance<T>();

foreach (DataColumn column in dr.Table.Columns)
{
foreach (PropertyInfo pro in temp.GetProperties())
{
if (pro.Name == column.ColumnName)
pro.SetValue(obj, dr[column.ColumnName], null);
else
continue;
}
}
return obj;
}

Missing Microsoft RDLC Report Designer in Visual Studio

Open Control Panel > Programs > Programs and Features

  • Select the entry for your version of Microsoft Visual Studio 2015. In our case, it was Microsoft Visual Studio Enterprise 2015.
  • Click the “Change” button on the top bar above the program list. After the splash screen, a window will open.
  • Press the “Modify” button.
  • Select Windows and Web Development > Microsoft SQL Server Data Tools, and check the box next to it.
  • Press the “Update” button on the lower-right hand side of the window.
  • Once the installation is complete, open your version of Visual Studio. After the new .dll files are loaded, Reporting functionality should be reimplemented, and you should be able to access all related forms, controls, and objects.

get id of closest ‘ul’ or parent id and get value of ‘href’ attribute with Jquery

html:

//if menu is created dynamically it is very useful
<ul id="slider" style="display: none;">
 <li class="panel cloned">test1</li>
 <li class="panel">
  <article>
   <h1><a href="/Admin/RoleMaster"> Role Creation </a> </h1>
  </article>
 </li>
 <li class="panel cloned">test2</li>
</ul>

js:

var url = window.location.pathname; //or window.location.toString();

$('#slider li.panel h1 a').each(function(){
   var myHref= $(this).attr('href');
   if( url == myHref) //compare current url with url value
  {

        var ulId = $(this).closest('ul').attr('id'); //get id of ul tag 
        $("#"+ulId).css("display", "block");
   }
});

asp net mvc : why is html checkbox generating an additional hidden input

I noticed strange behaviour whilst coding and ASP.NET MVC CheckBox Html Helper. I noticed that the field which was in part of a GET form was creating the query string with the same field in it twice. When the box was checked one parameter was true and the other was false.

Say I have a boolean field, MyField, that I am creating a checkbox for. I would write this: @Html.CheckBoxFor("MyField"). You would expect this to output a single check box but if you actually look at the HTML that is generated you will notice that is also creates a hidden field.

<input id="MyField" name="MyField" type="checkbox" value="true">
<input name="MyField" type="hidden" value="false">

 

Solution

Simple solution really. Write the HTML you want in HTML, forget about the HtmlHelper:

1
2
<input type="checkbox" name="MyField" value="true" id="MyField"
@Html.Raw((Model.MyField) ? "checked=\"checked\"" : "") />

Remember the value="true" because the default value to be sent is “on” if it is checked, which obviously can’t be parsed to a Boolean.

Obviously, this doesn’t look that clean but I will continue to do this on forms that use the GET method.

How to send SMS using ASP.NET through HTTP [c#, from website]

protected void buttonSendOnClick(object sender, EventArgs e)
{
//are required fields filled in:
if (textboxRecipient.Text == “”)
{
textboxError.Text += “Recipient(s) field must not be empty!\n”;
textboxError.Visible = true;
return;
}

//we creating the necessary URL string:
string ozSURL = “http://127.0.0.1&#8221;; //where the SMS Gateway is running
string ozSPort = “9501”; //port number where the SMS Gateway is listening
string ozUser = HttpUtility.UrlEncode(“admin”); //username for successful login
string ozPassw = HttpUtility.UrlEncode(“abc123”); //user’s password
string ozMessageType = “SMS:TEXT”; //type of message
string ozRecipients = HttpUtility.UrlEncode(textboxRecipient.Text); //who will get the message
string ozMessageData = HttpUtility.UrlEncode(textboxMessage.Text); //body of message

string createdURL = ozSURL + “:” + ozSPort + “/httpapi” +
“?action=sendMessage” +
“&username=” + ozUser +
“&password=” + ozPassw +
“&messageType=” + ozMessageType +
“&recipient=” + ozRecipients +
“&messageData=” + ozMessageData;

try
{
//Create the request and send data to the SMS Gateway Server by HTTP connection
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(createdURL);

//Get response from the SMS Gateway Server and read the answer
HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
System.IO.StreamReader respStreamReader = new System.IO.StreamReader(myResp.GetResponseStream());
string responseString = respStreamReader.ReadToEnd();
respStreamReader.Close();
myResp.Close();

//inform the user
textboxError.Text = responseString;
textboxError.Visible = true;
}
catch (Exception)
{
//if sending request or getting response is not successful the SMS Gateway Server may do not run
textboxError.Text = “The SMS Gateway Server is not running!”;
textboxError.Visible = true;
}

}

failed to serialize the response in Web API

Open Global.asax file and add two lines given below in bold characters. Result will always be in json format.

namespace WebApiExample
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

 

//Add following lines in global.asax file
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);

}
}
}

Understanding Filters in MVC

The Different Types of Filters

The ASP.NET MVC framework supports four different types of filters:

  1. Authorization filters – Implements the IAuthorizationFilter attribute.
  2. Action filters – Implements the IActionFilter attribute.
  3. Result filters – Implements the IResultFilter attribute.
  4. Exception filters – Implements the IExceptionFilter attribute.

Filters are executed in the order listed above. For example, authorization filters are always executed before action filters and exception filters are always executed after every other type of filter.

Authorization filters are used to implement authentication and authorization for controller actions. For example, the Authorize filter is an example of an Authorization filter.

Action filters contain logic that is executed before and after a controller action executes. You can use an action filter, for instance, to modify the view data that a controller action returns.

Result filters contain logic that is executed before and after a view result is executed. For example, you might want to modify a view result right before the view is rendered to the browser.

Exception filters are the last type of filter to run. You can use an exception filter to handle errors raised by either your controller actions or controller action results. You also can use exception filters to log errors.

For more details please click on below link

https://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/understanding-action-filters-cs