Saturday, January 18, 2014

Convert datatable to html table in c#

using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public static string Datatable2HTMLtable(DataTable dt)
{
       try
       {
              GridView dg = new GridView();
              dg.AutoGenerateColumns = true;
              dg.DataSource = dt;
              dg.DataBind();
              System.Text.StringBuilder sb = new System.Text.StringBuilder();
              System.IO.StringWriter sw = new System.IO.StringWriter(sb);
              HtmlTextWriter htw = new HtmlTextWriter(sw);
              dg.RenderControl(htw);
              return sb.ToString();
       }
       catch (Exception ex)
       {
              return "Datatable2HTMLtable ERROR: " + ex.Message;
       }
}

Thursday, January 16, 2014

Iframe Seft Redirection From Code behind Asp.net

this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myUniqueKey",
                    "self.parent.location='results.aspx';", true);

URL Redirect/Rewrite when opened from Mobile Browser

<rule name="Mobile Redirect" stopProcessing="true">
          <match url="^home/Account/$" ignoreCase="true" />
          <conditions logicalGrouping="MatchAny" trackAllCaptures="false">
            <add input="{HTTP_USER_AGENT}" pattern="midp|mobile|phone" />
            <add input="{HTTP_X-Device-User-Agent}" pattern="midp|mobile|phone" />
            <add input="{HTTP_X-OperaMini-Phone-UA}" pattern="midp|mobile|phone" />
          </conditions>
          <action type="Redirect" url="http://bbl.fecundtechno.com/mobile/index.html" appendQueryString="false" />
        </rule>

Fastest way to Import data from XML to SQL asp.net

//create dataset which will have all data
 DataSet ds1 = new DataSet("XML data1");
 
 //load schema first, this is updates schema will attch with email ds1.ReadXmlSchema(@"D:\userdocs\Ken_XML\FAC_SSIS_Identities\output.xsd");
 
 ds1.EnforceConstraints = false;
 
 //load data in dataset
 ds1.ReadXml(@"<XML file>");
 
 //connect to sql server and appropriate database see if table already present, if not present create new table after that load data from dataset to sql using bulkcopy
 try
 {
 SqlConnection conn = new SqlConnection("Server=localhost;Initial Catalog= XML_data;Trusted_Connection=True;");
 conn.Open();
 foreach (DataTable dt in ds1.Tables)
 {
 //check if table is present or not
 string exists = null;
 try
 {
 SqlCommand cmd = new SqlCommand("SELECT * FROM sysobjects where name = '" + dt.TableName + "'", conn);
 exists = cmd.ExecuteScalar().ToString();
 }catch (Exception exce)
 {
 exists = null;
 }
 // selecting each column of the datatable to create a table in the database
 Console.WriteLine("Bulk Insert Started table:" + dt.TableName);
 SqlBulkCopy bulk = new SqlBulkCopy(conn);
 bulk.DestinationTableName = "[" + dt.TableName + "]";
 foreach (DataColumn dc in dt.Columns)
 {
 bulk.ColumnMappings.Add(dc.ColumnName, dc.ColumnName);
 string type = "";
 //Getting right data type for column is very importatnt as it can create problem later if wrong data type is chosen
 //for mapping we are using below key value pair
 //Dictionary<string, string> typemappings = new Dictionary<string, string>();
 //typemappings.Add("Decimal", "Numeric");
 //typemappings.Add("String", "varchar(255)");
 //typemappings.Add("Int32", "Int");
 typemappings.TryGetValue(dc.DataType.FullName.Split('.')[1], out type);
 if (type == null)
 type = "varchar(255)";
 if (exists == null)
 {
 SqlCommand createtable = new SqlCommand("CREATE TABLE [" + dt.TableName + "] ([" + dc.ColumnName + "] " + type + ")", conn);
 createtable.ExecuteNonQuery();
 exists = dt.TableName;
 }
 else
 {
 try
 {
 SqlCommand addcolumn = new SqlCommand("ALTER TABLE [" + dt.TableName + "] ADD [" + dc.ColumnName + "] " + type, conn);
 addcolumn.ExecuteNonQuery();
 }catch (Exception ex2) { }
 }
 }
 //load data in sql
 bulk.WriteToServer(dt);
 Console.WriteLine("Bulk Insert completed table:" + dt.TableName);
 }
 conn.Close()
 }catch (Exception ex)
 {
 Console.WriteLine(ex.Message.ToString() + "\n" + ex.StackTrace.ToString());
 }
 finally
 {
 ds1.Clear();
 ds1.Dispose();
 }

Thursday, November 28, 2013

hide div after 5 seconds javascript

 function hidemsg() {

            setTimeout(function () {
                $("#" + '<%=lblSkillmsg.ClientID%>').hide('blind', {}, 500)
            }, 5000);
           
        }


you can set second also (5000 change to 6000 or etc.)

Friday, November 22, 2013

prevent a password input from clearing after submit asp.net

You require to set it again in page_load or in button click event like this :

string Password = txtPassword.Text;
txtPassword.Attributes.Add("value", Password);

get the value from radiobuttonlist through javascript asp.net

<form runat="server">
<asp:RadioButtonList ID="RadioButtonList1" runat="server" 
          RepeatDirection="Horizontal" RepeatLayout="flow" >
         <asp:ListItem Selected="True" Text ="Yes" Value="0"></asp:ListItem>
         <asp:ListItem  Text ="No" Value="1"></asp:ListItem>
      </asp:RadioButtonList>    </form>
<p id="button" onclick="getvalue()">click me</p> 
 
<script type="text/javascript">
function getvalue(){
 alert($('#<%=RadioButtonList1.ClientID %> input[type=radio]:checked').val());
}
</script>

Opps Part 1 : Abstraction

  Abstraction in C# is a fundamental concept of object-oriented programming (OOP) that allows developers t...