Monday, July 9, 2012

Generate random characters

public string GetRandomPasswordUsingGUID(int length)
        {
            // Get the GUID
            string guidResult = System.Guid.NewGuid().ToString();
            // Remove the hyphens
            guidResult = guidResult.Replace("-", string.Empty);
            // Make sure length is valid
            if (length <= 0 || length > guidResult.Length)
                throw new ArgumentException("Length must be between 1 and " + guidResult.Length);
            // Return the first length bytes
            return guidResult.Substring(0, length);
        }

Friday, July 6, 2012

Save file onto a folder(on solution) while upload

 //To Save the file
string test = Server.MapPath("~/Files/");
FileUpload1.PostedFile.SaveAs(test + FileUpload1.PostedFile.FileName);
//To delete the file
//add System.IO namespace for the "File" attribute
File.Delete(Server.MapPath("~/Files/" + FileUpload1.PostedFile.FileName));




<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Button" onclick="btnUpload_Click" />

Thursday, June 14, 2012

Save and Retreive Cookie

Reference : http://msdn.microsoft.com/en-us/library/ms178194.aspx

MODE1: To single cookie value
STEP1: Save
          //METHOD 1
            /*
            HttpContext.Current.Response.Cookies["Test123"].Value = Name;
            HttpContext.Current.Response.Cookies["Test123"].Expires = DateTime.Now.AddDays(2);
             */
            //METHOD 2
              
            HttpCookie oCookie = new HttpCookie("Test321");
            oCookie.Value = Name;
            oCookie.Expires = DateTime.Now.AddDays(2);
            HttpContext.Current.Response.Cookies.Add(oCookie);STEP2: Retreive
//METHOD 1
            /*
            string Name = "";
            if (HttpContext.Current.Request.Cookies["Test123"] != null)
            {
                Name = HttpContext.Current.Server.HtmlEncode(HttpContext.Current.Request.Cookies["Test123"].Value);
            }
            return Name;
             */
            //METHOD 2
            string Name = "";
            if (HttpContext.Current.Request.Cookies["Test321"] != null)
            {
                HttpCookie oCookie = HttpContext.Current.Request.Cookies["Test321"];
                Name = HttpContext.Current.Server.HtmlEncode(oCookie.Value);
            }
            return Name;
MODE2: more than one cookie value
STEP1: Save
            HttpCookie oCookie = new HttpCookie("abc123");
            oCookie.Values["Name"] = Name;
            oCookie.Values["Pwd"] = Pwd;
            oCookie.Expires = DateTime.Now.AddDays(2);
            HttpContext.Current.Response.Cookies.Add(oCookie);
STEP2: Retreive
           string Name = "";
            string Pwd = "";
            string result = "";
            if (HttpContext.Current.Request.Cookies["abc123"] != null)
            {
                System.Collections.Specialized.NameValueCollection oCollection;
                oCollection = HttpContext.Current.Request.Cookies["abc123"].Values;

                Name = oCollection["Name"];
                Pwd = oCollection["Pwd"];
                result = "<table><tr><td>" + Name + "</td><td>" + Pwd + "</td></tr></table>";
            }
            return result;


Wednesday, March 28, 2012

XML Insert


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:        Sareesh
-- Create date: 28 March 2012
-- Description:    here @TestXML root node is /STHome
--    and declare ID,TestValue same name as in the xml format

--var TestXML = "<STHome>";
--TestXML = TestXML + "<Home>";
--TestXML = TestXML + "<ID>" + $(this).val() + "</ID>";                                               
--TestXML = TestXML + "<TestValue>" + "" + "</TestValue>";                  
--TestXML = TestXML + "</Home>";
--TestXML = STHomeXML + "</STHome>";
-- =============================================
CREATE PROCEDURE SPR_TEST_Insert(
    @TestID INT,
    @TestXML XML
)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    DECLARE @XmlDocumentHandle int
    DECLARE @ID INT
    DECLARE @TestValue VARCHAR(2000)
   
    -- reads the xml then parses the text using the MSXML parser,
    -- and provides the parsed document in a state ready for consumption
    EXEC sp_xml_preparedocument @XmlDocumentHandle OUTPUT, @TestXML

    -- I create a cursor to iterate over the developer items in the XML file 
    -- OPENXML allows access to XML data as though it is a relational rowset
    DECLARE TestCursor CURSOR FOR
    SELECT ID, TestValue
    FROM OPENXML (@XmlDocumentHandle, '/STHome/Home',2)
    WITH (ID INT, TestValue VARCHAR(2000))
   
    OPEN DeficitCursor 
    FETCH NEXT FROM DeficitCursor INTO @DeficitID, @DeficitValue  
    WHILE @@FETCH_STATUS = 0
        BEGIN
              INSERT INTO TESTTABLE VALUES (@ID, @TestValue, @TestID)
              FETCH NEXT FROM TestCursor INTO @ID, @TestValue
        END

    CLOSE TestCursor
    DEALLOCATE TestCursor
   
    EXEC sp_xml_removedocument @XmlDocumentHandle
END
GO

Friday, March 16, 2012

Handling wih DataView for filter dataset values

we have a dataset
Dataset dsTest = oTestDL.GetTestData(ID);

if we want to use values in between a range from dataset we can use like this

DataView dvTest = dsTest .Tables[0].DefaultView;
dvTest .RowFilter = "TestID > 10 AND TestID < 18";
  foreach (DataRowView drv in dvTest)
     {
          string TestValues = drv[3].ToString();
    }