Tuesday, August 28, 2012

DataListControl Continuing..

  public void FillDataList()
        {
            Test1BL oTest1BL = new Test1BL();
            dlistTest.DataSource = oTest1BL.GetDetails();
            dlistTest.DataBind();          
        }

        protected void dlistTest_EditCommand(object source, DataListCommandEventArgs e)
        {          
            dlistTest.EditItemIndex = e.Item.ItemIndex;
            FillDataList();
        }

        protected void dlistTest_UpdateCommand(object source, DataListCommandEventArgs e)
        {
            int ID = Convert.ToInt32(((Label)e.Item.FindControl("lblID")).Text);
            string Name = ((TextBox)e.Item.FindControl("txtName")).Text;
            int Age = Convert.ToInt32(((TextBox)e.Item.FindControl("txtAge")).Text);

            Test1BL oTest1BL = new Test1BL();
            oTest1BL.UpdateData(ID, Name, Age);
            dlistTest.EditItemIndex = -1;
            FillDataList();
        }
protected void dlistTest_CancelCommand(object source, DataListCommandEventArgs e)
        {
            dlistTest.EditItemIndex = -1;
            FillDataList();
        }

        protected void dlistTest_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            int ID = Convert.ToInt32(((Label)e.Item.FindControl("lblIDDelete")).Text);
            Test1BL oTest1BL = new Test1BL();
            oTest1BL.DeleteData(ID);
            dlistTest.EditItemIndex = -1;
            FillDataList();
        }

DataList Control ASPX

<asp:DataList ID="dlistTest" runat="server" Style="width: 30%" AlternatingItemStyle-BackColor="AliceBlue" BorderWidth="2px" OnEditCommand="dlistTest_EditCommand"
onupdatecommand="dlistTest_UpdateCommand" oncancelcommand="dlistTest_CancelCommand"
 ondeletecommand="dlistTest_DeleteCommand">
 <HeaderTemplate>
 <table style="width: 100%">
<tr><td style="width: 40%">Name </td><td style="width: 40%">Age</td>
<td style="width: 20%"></td></tr></table>
 </HeaderTemplate>
<ItemTemplate>
 <table style="width: 100%"> <tr> <td style="width: 30%">
 <asp:Label runat="server" ID="lblName" Text='<%#Eval("Name")%>'></asp:Label>
 </td><td style="width: 30%">
 <asp:Label runat="server" ID="lblAge" Text='<%#Eval("Age")%>'></asp:Label></td>
<td style="width: 40%">
<asp:Label runat="server" ID="lblIDDelete" Text='<%#Eval("ID")%>' ></asp:Label>
<asp:LinkButton runat="server" ID="lbtnEdit" CommandName="Edit">Edit</asp:LinkButton>&nbsp;&nbsp;
<asp:LinkButton runat="server" ID="lbtnDelete" CommandName="Delete">Delete</asp:LinkButton>
 </td></tr></table>
</ItemTemplate>
<EditItemTemplate>
 <table style="width: 100%">
 <tr><td style="width: 10%">Name</td>
<td style="width: 20%">
<asp:TextBox runat="server" ID="txtName" Text='<%#Bind("Name")%>' Width="60px"></asp:TextBox>
</td>
<td style="width: 10%">Age </td>
<td style="width: 20%">
<asp:TextBox runat="server" ID="txtAge" Text='<%#Bind("Age")%>' Width="60px"></asp:TextBox>
</td>
<td style="width: 40%">
<asp:Label runat="server" ID="lblID" Text='<%#Eval("ID")%>' ></asp:Label></td>
</tr><tr><td></td><td></td>
<td colspan="3">
<asp:Button runat="server" ID="btnUpdate" CommandName="Update" Text="Update" />
 <asp:Button runat="server" ID="btnCancel" CommandName="Cancel" Text="Cancel" />
</td></tr></table>
</EditItemTemplate>
</asp:DataList>

Monday, July 30, 2012

Declaring OUT parameter in SQL and to retrive it

public int CheckalreadyExists(string Name)
        {
            param = new SqlParameter[2];
            param[0] = new SqlParameter("@Name", Name);
            param[1] = new SqlParameter("@IsFound", SqlDbType.Int);
            param[1].Direction = ParameterDirection.Output;

            SqlHelper.ExecuteDataset(oDBConnection.ConString(), CommandType.StoredProcedure, "SPR_CheckalreadyExists", param);
            return Convert.ToInt32(param[2].Value.ToString());
        }

CREATE PROCEDURE SPR_CheckalreadyExists(
    @ID INT,   
    @IsFound TINYINT OUT
)
AS
BEGIN
    IF EXISTS(SELECT ID FROM StudentTable WHERE Name = @Name )
        BEGIN
            SET @IsFound = 1
        END
    ELSE
        BEGIN
            SET @IsFound = 0
        END
END

Wednesday, July 18, 2012

Repeater Control Sample 2

STEP 1
<asp:Repeater runat="server" ID="reptrID2">
            <HeaderTemplate>
                Data :
            </HeaderTemplate>
            <ItemTemplate>
                <%#DataBinder.Eval(Container.DataItem,"Name") %>
                (<%#DataBinder.Eval(Container.DataItem,"Age") %>)
            </ItemTemplate>
            <SeparatorTemplate>
                ,
            </SeparatorTemplate>
</asp:Repeater>

STEP 2
Just bind it....
public void FillRepeater2()
 {
    Test1BL oTest1BL = new Test1BL();
    reptrID2.DataSource = oTest1BL.GetDetails();
    reptrID2.DataBind();
  }

Repeater Control Sample 1

STEP 1
<asp:Repeater ID="reptrID" runat="server">
            <HeaderTemplate>
                <table>
                    <tr><th>Name</th><th>Age</th></tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr><td><%#DataBinder.Eval(Container.DataItem,"Name") %></td>
                    <td><%#DataBinder.Eval(Container.DataItem,"Age") %></td></tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
STEP 2
Just Bind it to DB
public void FillRepeater()
 {
    Test1BL oTest1BL = new Test1BL();
    reptrID.DataSource = oTest1BL.GetDetails();
    reptrID.DataBind();
 }

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();
    }