Wednesday, 30 May 2012

How to convert the table values to XML file and XSD file

SqlConnection Con = new SqlConnection("server=.;uid=.;Pwd=.;database=.");
SqlDataAdapter da = new SqlDataAdapter("Select top 10 * from SoftLawrence", Con);
DataSet DS = new DataSet();
da.Fill(DS);
DS.WriteXml("F:\\SoftLawrence\\Sample.XML");
DS.WriteXmlSchema("F:\\SoftLawrence\\Test.XSD");

//To convert the Xml file to Dataset
DataSet dsVat = new DataSet();
string appPath =  GetExecutingPath();
dsVat.ReadXml(appPath + "Sample.xml");
 return dsVat;

Wednesday, 16 May 2012

To give Bold in the particular word of the Asp label control text

<asp:Label ID="lblText" runat ="server" Text="To attach a file, type the path of the file in the field below or click on the &lt;b&gt;Browse&lt;/b&gt; button to locate it in your computer." />

Tuesday, 1 May 2012

Use of COALESCE in the SQL Server

DECLARE @RegistrationNo VARCHAR(1000);    

                    SELECT  @RegistrationNo = COALESCE( CASE WHEN CAST(@RegistrationNo AS VARCHAR(1000)) IS NOT NULL AND @RegistrationNo<>'' then CAST(@RegistrationNo AS VARCHAR(1000)) + ', ' end, '') +''+ CAST(RegistrationNo AS VARCHAR(100))   

                    FROM Table_Name

                    SELECT '[' + @RegistrationNo + ']'

Friday, 27 April 2012

To convert the DataSet to the JsonString

[AjaxPro.AjaxMethod]
    public string GetContractJsonString()
    {
var dataSource = ContractServiceInstance.SelectContractList(cultureName, lessorName, customerId);
        return dataSource.ToJsonString();
}

 /// <summary>
    /// ToJSONString
    /// </summary>
    /// <param name="ds"></param>
    /// <returns></returns>
    public static string ToJsonString(this DataSet ds)
    {
        return ToJsonString(ds.Tables[0]);
    }

    public static string  ToJsonString(this DataTable dataTable)
    {
        var strDc = new string[dataTable.Columns.Count];
        var headStr = string.Empty;
        if (dataTable.Rows.Count == 0)
        {
            return "";
        }
        for (var i = 0; i < dataTable.Columns.Count; i++)
        {
            strDc[i] = dataTable.Columns[i].Caption;
            headStr += "\"" + strDc[i] + "\" : \"" + strDc[i] + i + "¾" + "\",";
        }

        headStr = headStr.Substring(0, headStr.Length - 1);
        var builder = new StringBuilder();
        builder.Append("[");
        for (var i = 0; i < dataTable.Rows.Count; i++)
        {
            var tempStr = headStr;
            builder.Append("{");
            for (int j = 0; j < dataTable.Columns.Count; j++)
            {
                tempStr = tempStr.Replace(dataTable.Columns[j] + j.ToString() + "¾",
                                          dataTable.Rows[i][j].ToString());
            }
            builder.Append(tempStr + "},");
        }
        builder = new StringBuilder(builder.ToString().Substring(0, builder.ToString().Length - 1));
        builder.Append("]");
        return builder.ToString();
    }

Tuesday, 17 April 2012

How to know the number of rows in all the tables in a SQL server 2005 database?

The below query will show the number of rows in all the tables in a SQL server 2005 database


select distinct sysobjects.name,sysindexes.rows from sysobjects
inner join sysindexes on sysindexes.id=sysobjects.id
where sysobjects.xtype='u'
order by sysindexes.rows desc

or you can use the below querry to the same details

SELECT OBJECT_NAME(OBJECT_ID) TableName, st.row_count

FROM sys.dm_db_partition_stats st
WHERE index_id < 2
ORDER BY st.row_count DESC

To create the business objects based on the SQL table columns

//SQL Query to create entity
select 'public '+
case when system_type_id=56 then ' int '+sys.columns.name+' { get; set; }'
when system_type_id=167 then ' string '+sys.columns.name+' { get; set; }'
when system_type_id=239 then ' string '+sys.columns.name+' { get; set; }'
when system_type_id=231 then ' string '+sys.columns.name+' { get; set; }'
when system_type_id=104 then ' string '+sys.columns.name+' { get; set; }'
when system_type_id=61 then ' DateTime '+sys.columns.name+' { get; set; }'
when system_type_id=108 then ' double '+sys.columns.name+' { get; set; }'
when system_type_id=175 then ' string '+sys.columns.name+' { get; set; }'
when system_type_id=34 then ' byte[] '+sys.columns.name+' { get; set; }'
when system_type_id=99 then ' string '+sys.columns.name+' { get; set; }'
when system_type_id=48 then ' byte '+sys.columns.name+' { get; set; }'
when system_type_id=52 then ' short '+sys.columns.name+' { get; set; }'
when system_type_id=127 then ' long '+sys.columns.name+' { get; set; }'
when system_type_id=106 then ' decimal '+sys.columns.name+' { get; set; }'
end AS 'Class Properties'
from sys.columns
inner join sys.tables on sys.columns.object_id=sys.tables.object_id and sys.tables.name='CustomerInquiry_AssetDetails'

Sunday, 15 April 2012

How to get the machine and windows user name of a logged in user in c#?

The below code will help you to find the Logged in windows user

System.Security.Principal.WindowsIdentity.GetCurrent().Name;
For getting the Machine Name you can use the below code.

WindowsIdentity.GetCurrent().Name;