views:

45

answers:

0

Hello,

I have table called tblfiles. I've added loaddate column here. I am uploading file to table using button click code. I want to add date in table automatically like system date and display on my grid. I am not entering date here, I need get automatically in table and display on grid.

I am using C# .NET and SQL Server 2005.

Here's the code:

<asp:GridView ID="GridView1" runat="server" 
              AutoGenerateColumns="False" DataKeyNames="ID"
              DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" 
                InsertVisible="False" ReadOnly="True"
                               SortExpression="ID" />

 <asp:BoundField DataField="ClaimNumber" HeaderText="Claim Number" 
                               SortExpression="ClaimNumber" />
 <asp:BoundField DataField="LoadDate" HeaderText="Load Date" 
                               SortExpression="LoadDate" />

<asp:BoundField DataField="Description" HeaderText="Description" 
                               SortExpression="Description" />
 <asp:BoundField DataField="ContentType" HeaderText="ContentType" 
                               SortExpression="ContentType" />                              
<asp:TemplateField HeaderText="Data">
<ItemTemplate>
   <%--<asp:Image ID="Image1" runat="server" 
           ImageUrl='<%# "FileUploadHandler.ashx?ID=" + Eval("ID")%>'/> --%>

      <asp:LinkButton ID="LinkButton1" runat="server" OnClick = "Retreive_Doc">Download</asp:LinkButton>     
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ AppSettings:connString %>"
SelectCommand="SELECT [ID], [ClaimNumber], [LoadDate], [Description], [ContentType], [Data] 
  FROM [tblFiles]"></asp:SqlDataSource>


        protected void btnUpload_Click(object sender, EventArgs e)
        {


            // Read the file and convert it to Byte Array
            string strClaimNumber = lblFileUploadCliamNumber.Text.ToUpper();

            string strDescription = txtDescription.Text.ToString();
            string filePath = FileUpload1.PostedFile.FileName;
            string filename = Path.GetFileName(filePath);
            string ext = Path.GetExtension(filename);
            string contenttype = String.Empty;

            //Set the contenttype based on File Extension

            switch (ext)
            {
                case ".doc":
                    contenttype = "application/vnd.ms-word";
                    break;
                case ".docx":
                    contenttype = "application/vnd.ms-word";
                    break;
                case ".xls":
                    contenttype = "application/vnd.ms-excel";
                    break;
                case ".xlsx":
                    contenttype = "application/vnd.ms-excel";
                    break;
                case ".jpg":
                    contenttype = "image/jpg";
                    break;
                case ".png":
                    contenttype = "image/png";
                    break;
                case ".gif":
                    contenttype = "image/gif";
                    break;
                case ".pdf":
                    contenttype = "application/pdf";
                    break;

            }

            if (contenttype != String.Empty)
            {


                Stream fs = FileUpload1.PostedFile.InputStream;
                BinaryReader br = new BinaryReader(fs);
                Byte[] bytes = br.ReadBytes((Int32)fs.Length);


                //insert the file into database

                string strQuery = "insert into tblFiles(Name, ContentType, Data, Description, ClaimNumber, LoadDate)" +
                   " values (@Name, @ContentType, @Data, @Description, @ClaimNumber, @LoadDate)";
                SqlCommand cmd = new SqlCommand(strQuery);
                cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename;
                cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value
                  = contenttype;
                cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
                cmd.Parameters.Add("@Description", SqlDbType.VarChar).Value = strDescription.ToString();
                cmd.Parameters.Add("@ClaimNumber", SqlDbType.NVarChar).Value = strClaimNumber.ToUpper();

                InsertUpdateData(cmd);
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Text = "File Uploaded Successfully";
                GridView1.DataBind();
            }

            else
            {

                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = "File format not recognised." +
                  " Upload Word/PDF/Excel formats";

            }

        }

Thanks, Ravi