Monday, August 4, 2014

Script To Disable Ctrl Keys Combination

<script  language="JavaScript">


 function disableCtrlKeyCombination(e) {
        //list all CTRL + key combinations you want to disable
        var forbiddenKeys = new Array('a', 'n', 'c', 'x', 'v', 'j', 'w','p','u');
        var key;
        var isCtrl;
        if (window.event) {
            key = window.event.keyCode;     //IE
            if (window.event.ctrlKey)
                isCtrl = true;
            else
                isCtrl = false;
        }
        else {
            key = e.which;     //firefox
            if (e.ctrlKey)
                isCtrl = true;
            else
                isCtrl = false;
        }
        //if ctrl is pressed check if other key is in forbidenKeys array
        if (isCtrl) {
            for (i = 0; i < forbiddenKeys.length; i++) {
                //case-insensitive comparation
                if (forbiddenKeys[i].toLowerCase() == String.fromCharCode(key).toLowerCase()) {
                    //alert('Key combination CTRL + ' + String.fromCharCode(key) + ' has been disabled.');
                    return false;
                }
            }
        }
        return true;
    }
</script>

How To Disable Print Copy Paste in Asp.net / HTML

<style type="text/css" media="print">
body { 

  display:none;
}
</style>

Wednesday, June 19, 2013

BULK DATA INSERT FROM CSV TO SQL SERVER

BULK INSERT TableBulkTest
FROM 'D:\BulkTest.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)

Monday, January 21, 2013

Insert data from One Table To another in SQL SERVER

insert into NEWTABLENAME (name,fathername) select name,fname from OLDTABLENAME


This Query user to copy data from one table to another, we can also use WHERE clause witth SELECT statement to verify data

Such as: 

insert into NEWTABLENAME (name,fathername) select name,fname from OLDTABLENAME
Where name IS NOT NULL

Friday, January 11, 2013

Add ScrollBar in CheckBox List in ASP.Net

It is not possible in Asp.Net but we can use by HTML TAG DIV


<divstyle="overflow-y:scroll; width:600px; height:500px">
</div>

Thursday, January 10, 2013

How To Use Split in Asp.net




 string[] arr=Txtname.Text.Split(new char[] {','});

Wednesday, January 2, 2013

how to delete file from folder in asp.net by FTP


 SqlDataAdapter da = new SqlDataAdapter("Select Path from Table_Image where loginID='" + TxtNo.Text + "'", connection);
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
         

            string host = "ftp://"; (Folder Path  Where image save)
            string userName = "uname";            (FTP User Name)
            string pwd = "password";             (FTP Password)
            string fileName = ds.Tables[0].Rows[0]["Path"].ToString();
            fileName = fileName.Replace("url", "");
            string remoteFile = Path.GetFileName(fileName);
            string FTPFilePath = Path.Combine(host, remoteFile);


            FtpWebRequest reqFTP = (FtpWebRequest)WebRequest.Create(FTPFilePath);
            reqFTP.Credentials = new NetworkCredential(userName, pwd);
            reqFTP.KeepAlive = false;
            reqFTP.UseBinary = true;
            reqFTP.UsePassive = false;
       
            reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;

            FtpWebResponse resFTP =(FtpWebResponse)reqFTP.GetResponse();
           string desc=resFTP.StatusDescription;
}

Thursday, November 22, 2012

Remove Duplicate Rows from DataBase in Sql Server

SELECT * FROM Tab_Test WHERE serial_num NOT IN (SELECT MIN(serial_num) FROM dbo.Tab_Test GROUP BY col1,col2)

serial_num is an auto indentity column

DELETE FROM Tab_Test WHERE serial_num NOT IN (SELECT MIN(serial_num) FROM dbo.Tab_Test GROUP BY col1,col2)

Tuesday, November 6, 2012

Upload Data from CSV file to Sql Server

1.FileUploader1 is a fileUploadControl
2.name,fathername,address are the columns which same as csv file name and sql server database 
3.G:\\FileName is your local computer path where CSV file save.
           

FileUploader1.SaveAs("G:\\FileName\\" + FileUploader1.FileName);

                // You can get connected to driver either by using DSN or connection string

                // Create a connection string as below, if you want to use DSN less connection.
The DBQ attribute sets the path of directory which contains CSV files

                string strConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + "G:\\FileName\\" + ";Extensions=asc,csv,tab,txt;Persist Security Info=False";


                string Query;

                DataSet ds = new DataSet();
                System.Data.Odbc.OdbcConnection conn;

                //Create connection to CSV file


                conn = new System.Data.Odbc.OdbcConnection(strConnString.Trim());

                // For creating a connection using DSN, use following line

                //Open the connection
                conn.Open();


                //Fetch records from CSV
                Query = "select * from [" + FileUploader1.FileName.ToString() + "]";

                System.Data.Odbc.OdbcDataAdapter oledaObj = new System.Data.Odbc.OdbcDataAdapter(Query, conn);

                //Fill dataset with the records from CSV file
                oledaObj.Fill(ds);


                int j = 0;
                if (ds.Tables[0].Rows.Count > 0)
                {

                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        try
                        {

                            DataSet ds2 = new DataSet();
                            SqlDataAdapter adap = new SqlDataAdapter("INSERT INTO TableDetails (name,fathername,address)  VALUES('" + ds.Tables[0].Rows[i]["name"].ToString() + "','" + ds.Tables[0].Rows[i]["fathername"].ToString() + "','" + ds.Tables[0].Rows[i]["address"].ToString() + "')", ConfigurationManager.ConnectionStrings["Conn"].ConnectionString.ToString());
                            adap.Fill(ds);
                            j++;
                        }
                        catch (Exception ex)
                        {

                        }
                    }


                    LblMsg.Text = "File Uploaded Successfully, No. of record save - " + System.Convert.ToString(j);
                    LblMsg.Visible = true;


                }
                conn.Close();

Thursday, October 18, 2012

Finding duplicate Row in sql Server

Select name from Table_Personal group by name having (count(*) >1)

Update Top 1 row in Sql Server

Update Top (1) Tab_Test set name='' where serialNum=1

Sunday, October 14, 2012

Set Sql Server Time Out in ASP.Net

How to Solve timeout expired problem:

simply Add in your web.config file inside ConnectionString;
<connectionStrings>
<add name="connect" connectionString="DataSource=MYPC;Initial Catalog=North;TimeOut=120"/>
</connectionStrings>
If u use a SqlCommand object in your code:
Then use

SqlCommand cmd=new SqlCommand();
cmd.TimeOut="180";

Sunday, September 16, 2012

Automatically Refresh Page in ASP.NET

Simply Add a meta tag in design code inside header tag

<meta http-equiv="refresh" content="15" /> 

Monday, August 27, 2012

READ AND WRITE COOKIES IN ASP.NET

 At Page load event


//////Read Cookie
 if(Request.Cookies["Cookiename"]!=null)
{
HttpCookie cok=new HttpCookie("Cookiename");
LblCookie.Text=cok.Value ;
}
//////



/////write cookie

HttpCookie cok=new HttpCookie("Cookiename");
cok.Value=txtUsername.Text;
Response.Cookies.Add(cok);



//////

Sunday, August 12, 2012

Character Count in asp.net


Define Script in design side

 <script type="text/javascript">
 
        function textCounter(field, countfield, maxlimit,msg,word)
        {
           if (field.value.length > maxlimit)
               field.value = field.value.substring(0, maxlimit);
           else
           {
               countfield.value =maxlimit - field.value.length;
               word.value=field.value.length;
               if(word.value > 160  && word.value <= 321)
                msg.value="2";
                else if(word.value > 321 && word.value <= 482)
                   msg.value="3";
                 
                    else if(word.value > 482 && word.value <= 643)
                   msg.value="4";
                 
                    else if(word.value > 643 && word.value <= 800)
                   msg.value="5";
                 
                   else
                         msg.value="1";
               
           }
        }
 
  </script>




<asp:TextBox ID="TxtMessage" runat="server" Height="92px"
            onkeydown="textCounter(this, this.form.countLen, 800,this.form.msgcount,this.form.wcount);"
            onkeyup="textCounter(this, this.form.countLen, 800,this.form.msgcount,this.form.wcount);" TextMode="MultiLine"></asp:TextBox>


<input maxlength="3" name="countLen" readonly="readonly" size="3"
            style="width: 26px; height: 16px;" type="text" value="800"
            style="border:none; border-color:White; background-color:White;" />Characters Left<br />
        <font color="red">
        Total
           
      <input name="wcount" value="0" type="text" readonly="readonly"
           
           
            style="border:medium none White; background-color:White; width: 24px; height: 15px; color:Red;" />Word(s)
        and&nbsp;<input name="msgcount" value="0" type="text" readonly="readonly"
            style="border:medium none White; background-color:White; width: 14px; height: 15px; color:Red;" />Message(s)

Saturday, June 16, 2012

How To Set Default focus and default button click

 <form id="form1" runat="server" defaultbutton="Btn_Log" defaultfocus="txtUname">

</form>

Monday, May 7, 2012

Create Table from another Table in SQL SERVER

select * into Newtable from oldtable where name like 'a%'

 Above query create table with name Newtable and copy all data from oldtable,


I f u want to create only structure as same as old table then you
can use :

WHERE 1=2

select * into   Newtable from oldtable where 1=2




Saturday, April 28, 2012

How to add days in Date in ASP.NET

DateTime dt=new DateTime();
dt=System.DateTime.Now.Date;
dt.AddDays(2);

How To Resize image in asp.net at upload

 System.Drawing.Image imageToBeResized = System.Drawing.Image.FromStream(FleUpPhoto.PostedFile.InputStream);
                    int imageHeight = imageToBeResized.Height;
                    int imageWidth = imageToBeResized.Width;
                    int maxHeight = 240;
                    int maxWidth = 320;
                    imageHeight = (imageHeight * maxWidth) / imageWidth;
                    imageWidth = maxWidth;

                    if (imageHeight > maxHeight)
                    {
                        imageWidth = (imageWidth * maxHeight) / imageHeight;
                        imageHeight = maxHeight;
                    }

                    Bitmap bitmap = new Bitmap(imageToBeResized, imageWidth, imageHeight);
                    System.IO.MemoryStream stream = new MemoryStream();
                    bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    stream.Position = 0;
                    byte[] image = new byte[stream.Length + 1];
                    stream.Read(image, 0, image.Length);

 SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["Connect"]);

 SqlCommand storeimage = new SqlCommand("INSERT INTO TablePic" + "(Content, ImgType, Size,imageSeen) " + " values (@image, @imagetype, @imagesize,@imgSeen)", myConnection);
                   
                    storeimage.Parameters.Add("@image", SqlDbType.Binary, image.Length).Value = image;
                    storeimage.Parameters.Add("@imagetype", SqlDbType.VarChar, 100).Value = FleUpPhoto.PostedFile.ContentType;
                    storeimage.Parameters.Add("@imagesize", SqlDbType.BigInt, 99999).Value = FleUpPhoto.PostedFile.ContentLength;
                    storeimage.Parameters.Add("@imgSeen", SqlDbType.VarChar, 100).Value = "Off";
                       myConnection.Open();
                    storeimage.ExecuteNonQuery();
                    myConnection.Close();

Thursday, April 26, 2012

Open new page as a popup window in asp.net



<script type="text/javascript" language="javascript">
        function changeScreenSize(w, h) {
            window.resizeTo(w, h)
        }
</script>

call this script at body onload
<body onload="changeScreenSize(500,300)" >

</body>