Friday 10 May 2013

Upload Multiple Files with One Fileupload Control and Save these file in database as new row



.aspx Page Content

<style type="text/css">
        #divFile p { font:13px Century }
        #divFile h3 { font:16px Century; font-weight:bold; }
    </style>
    <script type="text/javascript">
    $('#btUpload').click(function() { if (fileUpload.value.length == 0) { alert('No files selected.'); return false; } });
</script>
<div id="divFile" style="width: 95%;float:right; text-align:center">
<b>Upload Multiple Reports by selecting multiple reports files</b>
            <p><asp:FileUpload ID="fileUpload" multiple="true" runat="server" /></p>
            <p><input type="button" id="btUpload" value="Upload Reports"
                onserverclick="btUpload_Click" runat="server" /></p>
            <p><asp:label id="lblFileList" runat="server"></asp:label></p>
            <p><asp:Label ID="lblUploadStatus" runat="server"></asp:Label></p>
            <p><asp:Label ID="lblFailedStatus" runat="server"></asp:Label></p>
            <br />
            </div>



.cs Page Content


protected void btUpload_Click(object sender, EventArgs e)
        {
            if (fileUpload.HasFile) // CHECK IF ANY FILE HAS BEEN SELECTED.
            {
                int iUploadedCnt = 0;
                int iFailedCnt = 0;
                HttpFileCollection hfc = Request.Files;

                lblFileList.Text = "Select <b>" + hfc.Count + "</b> file(s)";

                if (hfc.Count <= 40)    // 40 FILES RESTRICTION.
                {
                    for (int i = 0; i <= hfc.Count - 1; i++)
                    {
                        HttpPostedFile hpf = hfc[i];
                        if (hpf.ContentLength > 0)
                        {
                            if (!File.Exists(Server.MapPath("../Report/") + Path.GetFileName(hpf.FileName)))
                            {
                                DirectoryInfo objDir = new DirectoryInfo(Server.MapPath("../Report/"));
                                string sFileName = Path.GetFileName(hpf.FileName);
                                string sFileExt = Path.GetExtension(hpf.FileName);

                                // CHECK FOR DUPLICATE FILES.
                                FileInfo[] objFI = objDir.GetFiles(sFileName.Replace(sFileExt, "") + ".*");

                                if (objFI.Length > 0)
                                {
                                    // CHECK IF FILE WITH THE SAME NAME EXISTS (IGNORING THE EXTENTIONS).
                                    foreach (FileInfo file in objFI)
                                    {
                                        string sFileName1 = objFI[0].Name;
                                        string sFileExt1 = Path.GetExtension(objFI[0].Name);

                                        if (sFileName1.Replace(sFileExt1, "") == sFileName.Replace(sFileExt, ""))
                                        {
                                            iFailedCnt += 1;        // NOT ALLOWING DUPLICATE.
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    // SAVE THE FILE IN A FOLDER.
                                    string pid = "";
                                    string fname = "";
                                    if (Session["HID"].ToString() == "ultrad")
                                    {
                                        pid = hpf.FileName.Substring(0, 5);
                                        fname = hpf.FileName.Substring(5, hpf.FileName.Length - 5);
                                    }
                                    else
                                    {
                                        pid = hpf.FileName.Substring(0, 6);
                                        fname = hpf.FileName.Substring(6, hpf.FileName.Length - 6);
                                    }

                                    string path1 = Server.MapPath("../Report/Hosp/") + Session["HID"].ToString();

                                    if (!Directory.Exists(path1))
                                    {
                                        Directory.CreateDirectory(path1);
                                    }

                                    string path = Server.MapPath("../Report/Hosp/" + Session["HID"].ToString() + "/") + pid;

                                    if (!Directory.Exists(path))
                                    {
                                        Directory.CreateDirectory(path);
                                    }

                                 
                                    hpf.SaveAs(Server.MapPath("../Report/Hosp/" + Session["HID"].ToString() + "/" + pid + "/") + Path.GetFileName(DateTime.Now.ToString("yyyyMMddHHmmss") + " " + hpf.FileName));

                                    du.ExecuteSql("insert into Patient_MR(PatientId,RName,ReportPath,Hospital) values('" + pid + "','" + fname + "','Report/Hosp/" + Session["HID"].ToString() + "/" + pid + "/" + Path.GetFileName(DateTime.Now.ToString("yyyyMMddHHmmss") + " " + hpf.FileName) + "','" + Session["HID"].ToString() + "')");
                                 
                                    iUploadedCnt += 1;
                                }
                            }
                        }
                    }
                    lblUploadStatus.Text = "<b>" + iUploadedCnt + "</b> file(s) Uploaded.";
                    lblFailedStatus.Text = "<b>" + iFailedCnt + "</b> duplicate file(s) could not be uploaded.";
                    fileUpload.Attributes.Clear();

                }
                else lblUploadStatus.Text = "Max. 40 files allowed.";
            }
            else lblUploadStatus.Text = "No files selected.";
        }


Tuesday 7 May 2013

Find Null/Empty Values in Asp.net Datatable and Replace with Other Values

Try this C# code on datatable to avoid null reference error message...........

for (int i = 0; i < dt.Rows.Count;i++ )
{
for(int j=0;j<dt.Columns.Count;j++)
{
if(string.IsNullOrEmpty(dt.Rows[i][j].ToString()))
{
// Write your Custom Code
dt.Rows[i][j] = "your required value";
}
}
}