Posts

Showing posts from November, 2012

Import Data from Excel to DataGridView in C# or Asp.net

protected void Submit_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable dt = ss();
            gvOne.DataSource = dt;
            gvOne.DataBind();
        }
        catch (Exception ex)
        {
            throw new Exception("Upload Failed: " + ex.Message);
        }

    }


private DataTable ss()
    {


        string FilePath = @"E:\Excel_Sample\Excel\ActionSchedule1.xlsx";
        string excelConnectString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties=Excel 12.0;";
        OleDbConnection objConn = new OleDbConnection(excelConnectString);
        OleDbCommand objCmd = new OleDbCommand("Select * From [Sheet1$]", objConn);
        OleDbDataAdapter objDatAdap = new OleDbDataAdapter();
        objDatAdap.SelectCommand = objCmd;
        DataSet ds = new DataSet();
        objDatAdap.Fill(ds);
        //BulkInsertUpload(ds);
        return ds.Tables[0];



    }


<body>
    <form id=&q…

Load Css file Dynamically on page load in asp.net

if (!IsPostBack)
        {
            HtmlGenericControl css;
            css = new HtmlGenericControl();
            css.TagName = "style";
            css.Attributes.Add("type", "text/css");
            css.InnerHtml = "@import \"../css/CentralReport.css\";";
            Page.Header.Controls.Add(css);
        }

When Select value in DropDownlist it will redirect to particular page in javascript

function RedirectToPage(targ, selObj, restore) {        
            var vIndexValue = selObj.options[selObj.selectedIndex].value;
            var vCurInd = selObj.selectedIndex;
            // Making all Drops selected Index 0.........
            document.getElementById("Value").selectedIndex = 0;

            //alert(vIndexValue);
            // Making the Curent Selected .....
            selObj.selectedIndex = vCurInd;
            //selObj.options[selObj.selectedIndex].value = vIndexValue;

            eval(targ + ".location='" + vIndexValue + "'");
        }

<select name="Value" class="Item_IB_Drop" id="Call" onchange="RedirectToPage('parent',this,0)">
   <option value="#" selected="selected">Select Option Below </option>
   <option value="../FolderName/PageName.aspx">DisplayName</option>
         </select>

How to auto-increment serial number in gridview

<asp:TemplateField HeaderText="SI No">
                                <ItemTemplate>
                                    <%# Container.DataItemIndex +1 %>
                                </ItemTemplate>
                            </asp:TemplateField>

Validate radioButtonlist in asp.net using custom validator

<script type="text/javascript" language="javascript">
        function ClientValidate(source, args) {

            var MyRadioButton = document.getElementById("<%=rbl_Menu.ClientID %>");
            var options = MyRadioButton.getElementsByTagName("input");
            var Checkvalue = false;
            var check;
            for (i = 0; i < options.length; i++) {
                if (options[i].checked) {
                    Checkvalue = true;
                    check = options[i].value;
                }
            }
            if (!Checkvalue) {
                args.IsValid = false;
            }
            else {
                args.IsValid = true;
            }

        }
    </script>
<asp:RadioButtonList ID="rbl" runat="server" Width="30%"
RepeatDirection="Horizontal">
                                    <asp:ListItem Value="1">Data</asp:ListItem>
                    …