简体   繁体   English

在网格视图页面上按索偿号显示数据

[英]Display data by claim numbers on grid view page

I am using C#. 我正在使用C#。 net and SQL server 2005. NET和SQL Server 2005。

I am uploading files (Word, PDF) into the database, and displaying on page using grid view. 我正在将文件(Word,PDF)上传到数据库,并使用网格视图显示在页面上。

I have different claim numbers, like co50000006 (like 10 rows). 我有不同的索偿编号,例如co50000006(例如10列)。 I mean the claim number has different files. 我的意思是索赔编号具有不同的文件。 I have other claim numbers on my table. 我的桌子上还有其他索赔号码。 (like c08000131, c01000001). (如c08000131,c01000001)。

I want to display only one claim number rows on grid. 我只想在网格上显示一个索赔编号行。 I mean what ever number my querystring show I want to display the particular claim number on my grid.( Request.QueryString["ClaimNum"]; ). 我的意思是我的查询字符串显示的编号是什么,我想在我的网格上显示特定的索赔编号。( Request.QueryString["ClaimNum"]; )。

Please help me with this. 请帮我解决一下这个。

I am getting claimnumber on table on my page header and I am inserting this value into the table. 我在页面标题上的表上获得了ClaimNumber,并将此值插入到表中。

protected void Page_Load(object sender, EventArgs e)
{
        //Showing Claim Number on file upload.
        lblFileUploadCliamNumber.Text = Request.QueryString["ClaimNum"];
}

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

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

     <asp:TemplateField HeaderText="Load Date">
     <ItemTemplate>
     <asp:Label runat="server" ID="LoadDate"
     Text='<%# String.Format("{0:M/d/yyyy}", Eval("LoadDate"))  %>' />
     </ItemTemplate>
     </asp:TemplateField>                         

<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>

I am using a SQL query here: 我在这里使用SQL查询:

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

Below are the my total code using in file upload.aspx.cs. 以下是我在文件upload.aspx.cs中使用的全部代码。

public partial class FileUpload : System.Web.UI.Page
{

    private string redirectFrom = "FileUpload";


    protected void Page_Load(object sender, EventArgs e)
    {
        //Showing Claim Number on file upload.
        lblFileUploadCliamNumber.Text = Request.QueryString["ClaimNum"];

    }
    protected void Page_Init(object sender, EventArgs e)
    {

        WireEvents();

    }

    private void WireEvents()
    {

       btnFileUploadClose.Click += new EventHandler(btnFileUploadClose_Click);



    }

    private void btnFileUploadClose_Click(object sender, EventArgs e)
    {
        ReturnToClaimInfo();
    }


    private void ReturnToClaimInfo()
    {

        Response.Redirect(String.Format("/ClaimInfoView.aspx?ClaimNum={0}&ClaimCertSeqNo={1}&ClaimCovNum={2}&RedirectedFrom={3}"
                                           , Request.QueryString["ClaimNum"]
                                           , Request.QueryString["ClaimCertSeqNo"]
                                           , Request.QueryString["ClaimCovNum"]
                                           , redirectFrom));
    }








    /// Different file types start


    protected void btnUpload_Click(object sender, EventArgs e)
    {


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

        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();
            cmd.Parameters.Add("@LoadDate", SqlDbType.DateTime).Value = strDate.ToString();
            InsertUpdateData(cmd);
            lblMessage.ForeColor = System.Drawing.Color.Green;
            lblMessage.Text = "File Uploaded Successfully";
            if (lblMessage.Text == "File Uploaded Successfully")
            {
                txtDescription.Text = string.Empty;
            }
            //if(FileUpload1 != null)
            //{
            //    lblMessage.Text = string.Empty;
            //}
            GridView1.DataBind();
        }

        else
        {

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

        }

    }


    private Boolean InsertUpdateData(SqlCommand cmd)
    {
        String strConnString = System.Configuration.ConfigurationManager
        .AppSettings["connString"];
        SqlConnection con = new SqlConnection(strConnString);
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        try
        {

            con.Open();
            cmd.ExecuteNonQuery();
            return true;

        }

        catch (Exception ex)
        {
            Response.Write(ex.Message);
            return false;

        }

        finally
        {
            con.Close();
            con.Dispose();
        }

    }


    //Retrieve doc
    protected void Retreive_Doc(object sender, EventArgs e)
    {
        string strQuery = "select Name, ContentType, Data from tblFiles where id=@id";
        SqlCommand cmd = new SqlCommand(strQuery);
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = ((LinkButton) sender).CommandArgument;
        DataTable dt = GetData(cmd);
        if (dt != null)
        {

                download(dt);
        }
    }

    // Get data
    public DataTable GetData(SqlCommand cmd)
    {
        DataTable dt = new DataTable();
        String strConnString = System.Configuration.ConfigurationManager
        .AppSettings["connString"];
        SqlConnection con = new SqlConnection(strConnString);
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;

        try
        {
            con.Open();
            sda.SelectCommand = cmd;
            sda.Fill(dt);
            return dt;
        }
        catch
        {
            return null;
        }
        finally
        {
            con.Close();
            sda.Dispose();
            con.Dispose();
        }
    }

    // download file
    public void download(DataTable dt)
    {
        Byte[] bytes = (Byte[])dt.Rows[0]["Data"];
        Response.Buffer = true;
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = dt.Rows[0]["ContentType"].ToString();
        Response.AddHeader("content-disposition", "attachment;filename="
            + dt.Rows[0]["Name"].ToString());
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();
    } 

    //end
  }
}

你有没有考虑过

SelectCommand="SELECT [ID], [ClaimNumber], [LoadDate], [Description], [ContentType], [Data] FROM [tblFiles] where [ClaimNumber] = " Request.QueryString["ClaimNum"]>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM