DATA and IMAGE are populated in two rows. I want to bind them in a single row

偶尔善良 提交于 2019-12-31 03:51:08

问题


I'M GOING TO ASK THIS FOR THE SECOND TIME. I'M GOING TO MAKE IT CLEAR SO YOU CAN HELP ME :) Okay let's start.

I have a Registration Process. I split the process into two page. The first page is for "Personal Information" only. Then when i click the next button the next page will appear (postbackURL) This page is for "Upload Photo" page. They work fine! the data and the image are displayed on my gridview control. But my problem is here: As you can see in the figure below. The data are populated in two rows! I want them to appear in only single row! I think the problem is THE DATABASE FROM PAGE 1 IS NOT CONTINUING. THAT'S WHY WHEN I UPLOAD AN IMAGE IN PAGE 2 THE DATABASE WILL CREATE ANOTHER RECORD. hmmm...what's the problem :) I don't know how to do it. Please help!

THE DATA SHOWN IN THE TABLE BELOW ARE ONLY DUMMY. PLEASE DON'T TAKE IT SERIOUSLY.

Here's the code for INSERT/UPDATE for PersonalINFO.aspx page:

<asp:AccessDataSource runat="server" ID="AccessDataSource1" DeleteCommand="DELETE FROM [PendingRecords] WHERE [ID] = ?" InsertCommand="INSERT INTO [PendingRecords] ([Username], [Password], [FirstName], [LastName], [MiddleName], [Address], [Gender], [ContactNumber], [PlateNumber], [Color], [Brand], [LiscensedNumber]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" UpdateCommand="UPDATE [PendingRecords] SET [Username] = ?, [Password] = ?, [FirstName] = ?, [LastName] = ?, [MiddleName] = ?, [Address] = ?, [Gender] = ?, [ContactNumber] = ?, [PlateNumber] = ?, [Color] = ?, [Brand] = ?, [LiscensedNumber] = ? WHERE [ID] = ?" DataFile="_private/records.mdb" SelectCommand="SELECT * FROM [PendingRecords]">
        <DeleteParameters>
            <asp:parameter Name="ID" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:parameter Name="Username" Type="String" />
            <asp:parameter Name="Password" Type="String" />
            <asp:parameter Name="FirstName" Type="String" />
            <asp:parameter Name="LastName" Type="String" />
            <asp:parameter Name="MiddleName" Type="String" />
            <asp:parameter Name="Address" Type="String" />
            <asp:parameter Name="Gender" Type="String" />
            <asp:parameter Name="ContactNumber" Type="String" />
            <asp:parameter Name="PlateNumber" Type="String" />
            <asp:parameter Name="Color" Type="String" />
            <asp:parameter Name="Brand" Type="String" />
            <asp:parameter Name="LiscensedNumber" Type="String" />
            <asp:parameter Name="ID" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:parameter Name="Username" Type="String" />
            <asp:parameter Name="Password" Type="String" />
            <asp:parameter Name="FirstName" Type="String" />
            <asp:parameter Name="LastName" Type="String" />
            <asp:parameter Name="MiddleName" Type="String" />
            <asp:parameter Name="Address" Type="String" />
            <asp:parameter Name="Gender" Type="String" />
            <asp:parameter Name="ContactNumber" Type="String" />
            <asp:parameter Name="PlateNumber" Type="String" />
            <asp:parameter Name="Color" Type="String" />
            <asp:parameter Name="Brand" Type="String" />
            <asp:parameter Name="LiscensedNumber" Type="String" />
        </InsertParameters>
    </asp:AccessDataSource>

Here's the code for btnUpload_Click in UploadPhoto.aspx page:

<script runat="server" type="text/c#">
protected void btnUpload_Click(object sender, EventArgs e)


{
    {
   byte[] imageSize = new byte
                 [FileUpload1.PostedFile.ContentLength];
  HttpPostedFile uploadedImage = FileUpload1.PostedFile;
  uploadedImage.InputStream.Read
     (imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);
 // Create SQL Connection 
  OleDbConnection con = new OleDbConnection();
  con.ConnectionString = ConfigurationManager.ConnectionStrings["recordsConnectionString12"].ConnectionString;
 // Create SQL Command 
 OleDbCommand cmd = new OleDbCommand();
 cmd.CommandText = "INSERT INTO PendingRecords([Image])" + " VALUES (@Image)";
 cmd.CommandType = CommandType.Text;
 cmd.Connection = con;

 OleDbParameter UploadedImage = new OleDbParameter
              ("@Image", OleDbType.VarBinary, imageSize.Length);

 UploadedImage.Value = imageSize;
 cmd.Parameters.Add(UploadedImage);
 con.Open();
 cmd.ExecuteNonQuery();  
 GridView1.DataSourceID = "";
 GridView1.DataSource = SqlDataSource1;
 GridView1.DataBind();
 con.Close();
 }
}
</script>

UploadPhoto.aspx page DATASOURCE CODE:

<asp:SqlDataSource runat="server" id="SqlDataSource1" ProviderName="<%$ ConnectionStrings:recordsConnectionString12.ProviderName %>" ConnectionString="<%$ ConnectionStrings:recordsConnectionString12 %>" SelectCommand="SELECT * FROM [PendingRecords]" DeleteCommand="DELETE FROM [PendingRecords] WHERE [ID] = ?" InsertCommand="INSERT INTO [PendingRecords] ([Username], [Password], [FirstName], [LastName], [MiddleName], [Address], [Gender], [ContactNumber], [PlateNumber], [Color], [Brand], [LiscensedNumber]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" UpdateCommand="UPDATE [PendingRecords] SET [Username] = ?, [Password] = ?, [FirstName] = ?, [LastName] = ?, [MiddleName] = ?, [Address] = ?, [Gender] = ?, [ContactNumber] = ?, [PlateNumber] = ?, [Color] = ?, [Brand] = ?, [LiscensedNumber] = ? WHERE [ID] = ?">
    <DeleteParameters>
        <asp:parameter Name="ID" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:parameter Name="Username" Type="String" />
        <asp:parameter Name="Password" Type="String" />
        <asp:parameter Name="FirstName" Type="String" />
        <asp:parameter Name="LastName" Type="String" />
        <asp:parameter Name="MiddleName" Type="String" />
        <asp:parameter Name="Address" Type="String" />
        <asp:parameter Name="Gender" Type="String" />
        <asp:parameter Name="ContactNumber" Type="String" />
        <asp:parameter Name="PlateNumber" Type="String" />
        <asp:parameter Name="Color" Type="String" />
        <asp:parameter Name="Brand" Type="String" />
        <asp:parameter Name="LiscensedNumber" Type="String" />
        <asp:parameter Name="ID" Type="Int32" />
    </UpdateParameters>
    <InsertParameters>
        <asp:parameter Name="Username" Type="String" />
        <asp:parameter Name="Password" Type="String" />
        <asp:parameter Name="FirstName" Type="String" />
        <asp:parameter Name="LastName" Type="String" />
        <asp:parameter Name="MiddleName" Type="String" />
        <asp:parameter Name="Address" Type="String" />
        <asp:parameter Name="Gender" Type="String" />
        <asp:parameter Name="ContactNumber" Type="String" />
        <asp:parameter Name="PlateNumber" Type="String" />
        <asp:parameter Name="Color" Type="String" />
        <asp:parameter Name="Brand" Type="String" />
        <asp:parameter Name="LiscensedNumber" Type="String" />
    </InsertParameters>
</asp:SqlDataSource>

回答1:


You will need to pass the ID from personal info page to the Upload page (probably via query string).

And then the query for your image update should be and UPDATE query, see below.

cmd.CommandText = "UPDATE PendingRecords SET [Image] = @Image WHERE ID = @ID";

You'll now pass in the ID parameter as needed.

If the parameter is in the query string ( /UploadPhoto.aspx?id=60 )

OleDbParameter UploadedImage = new OleDbParameter
              ("@Image", OleDbType.VarBinary, imageSize.Length);

 UploadedImage.Value = imageSize;
 cmd.Parameters.Add(UploadedImage);

OleDbParameter IdParameter = new OleDbParameter("@ID", Request.QueryString["id"]);
    cmd.Parameters.Add(IdParameter);


来源:https://stackoverflow.com/questions/14262967/data-and-image-are-populated-in-two-rows-i-want-to-bind-them-in-a-single-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!