locked
cant store image via fileupload control RRS feed

  • Question

  • User-464009799 posted

    Hi 

    The code its working very well but when i make new user and check in MS Sql i can find the new user that i enter it but without the image name and also cant store the image in the project file, please any help

    <%@ Page Title="" Language="C#" MasterPageFile="~/MPage.master" AutoEventWireup="true"
        CodeFile="Registration.aspx.cs" Inherits="Registration" %>
    
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    
        <style type="text/css">
        .style5
        {
            width: 191px;
            color:#606060;
        }
    </style>
    
    
    
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
       
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
        
    
        <div class="space">
        </div>
        <div class="space">
        </div>
        <div class="regepic">
            Registre New User</div>
        <br />
        <br />
        <br />
        <br />
        <table style="width: 69%; height: 338px;" align="center">
    
    
           <tr>
           
           <td class="style5"> &nbsp; Användarnamn</td>
            <td style="text-align: left" class="style8">
                    <asp:TextBox ID="UsrNme" runat="server" Width="258px" Height="20px"></asp:TextBox>
                </td>
           <td>
               <asp:RequiredFieldValidator ID="Userreqlbl" runat="server" 
                   ControlToValidate="UsrNme" ErrorMessage="This field is required" ForeColor="Red"></asp:RequiredFieldValidator><br />
               <br />
               </td>
    
    
           </tr>
    
    
            <tr>
                <td class="style5">
                    &nbsp; Förnamn
                </td>
                <td style="text-align: left" class="style8">
                    <asp:TextBox ID="fnbox" runat="server" Height="20px" Width="258px" 
                        ></asp:TextBox>
                </td>
                <td style="text-align: left">
                    &nbsp;
                </td>
            </tr>
            
            <tr>
                <td class="style5">
                    &nbsp; Efternamn
                </td>
                <td style="text-align: left" class="style8">
                    <asp:TextBox ID="lnamebox" runat="server" Height="20px" Width="258px"></asp:TextBox>
                </td>
                <td style="text-align: left">
                    &nbsp;
                </td>
            </tr>
          
            <tr>
                <td class="style5">
                    &nbsp; Lösenord
                </td>
                <td style="text-align: left" class="style8">
                    <asp:TextBox ID="passtxtbx1" runat="server" TextMode="Password" Width="258px" Height="20px"></asp:TextBox>
                </td>
                <td style="text-align: left">
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="passtxtbx1"
                        ErrorMessage="This part is required" ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="style5">
                    &nbsp; Återskrivnings Lösenord
                </td>
                <td style="text-align: left" class="style8">
                    <asp:TextBox ID="passtxtbx2" runat="server" TextMode="Password" Width="258px" Height="20px"></asp:TextBox>
                </td>
                <td style="text-align: left">
                    <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="passtxtbx1"
                        ControlToValidate="passtxtbx2" ErrorMessage="The password is not match" ForeColor="Red"></asp:CompareValidator>
                </td>
            </tr>
            <tr>
                <td class="style5">
                    &nbsp; E-post
                </td>
                <td style="text-align: left" class="style8">
                    <asp:TextBox ID="emailbox" runat="server" Height="20px" Width="258px"></asp:TextBox>
                </td>
                <td style="text-align: left">
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="emailbox"
                        ErrorMessage="Invalid Format" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td class="style5">
                    &nbsp; Land
                </td>
                <td style="text-align: left" class="style8">
                    
                    <asp:DropDownList ID="DDLCOUNTRY" runat="server" AutoPostBack="True" 
                        DataSourceID="SqlDataSourcecountry" DataTextField="CountID" 
                        DataValueField="CountID">
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSourcecountry" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:BeravaConnectionString %>" 
                        SelectCommand="SELECT [CountID] FROM [Countries]"></asp:SqlDataSource>
                    
                </td>
                <td style="text-align: left">
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td class="style5">
                    &nbsp; Lan
                </td>
                <td style="text-align: left" class="style8">
                    <asp:DropDownList ID="DDLSTATE" runat="server" AutoPostBack="True" 
                        DataSourceID="SqlDataSourcestate" DataTextField="Swelan" 
                        DataValueField="Swelan">
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSourcestate" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:BeravaConnectionString %>" 
                        SelectCommand="SELECT [Swelan] FROM [Countries]"></asp:SqlDataSource>
                </td>
                <td style="text-align: left">
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td class="style5">
                    &nbsp; Stad
                </td>
                <td style="text-align: left" class="style11">
                    <asp:TextBox ID="citytxtbox" runat="server" Height="20px" Width="258px"></asp:TextBox>
                </td>
                <td style="text-align: left" class="style12">
                </td>
            </tr>
            
           
            <tr>
                <td class="style5">
                    &nbsp; Postnummer
                </td>
                <td style="text-align: left" class="style8">
                    <asp:TextBox ID="postbox" runat="server" Width="258px" CausesValidation="True" 
                        Height="20px"></asp:TextBox>
                </td>
                <td style="text-align: left">
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td class="style5">
                    &nbsp; Personlig bild
                </td>
                <td style="text-align: left" class="style8">
                    <asp:FileUpload ID="FileUpload1" runat="server" Width="230px" />
                </td>
                <td style="text-align: left">
                    &nbsp;
                </td>
            </tr>
           <tr>
           <td>
               
           
           </tr>
            <tr>
                <td class="style5">
                    &nbsp;
                    </td>
                <td class="style8">
                    
                    <asp:Button ID="btnSave" runat="server" onclick="btnSave_Click" 
                        Text="Enter" style="height: 26px" />
                    
                    
                    
                </td>
                <td>
                    <asp:Label ID="lblmsg" runat="server"></asp:Label>
                </td>
            </tr>
        </table>
        <br />
        <br />
        <br />
        </ContentTemplate>
        </asp:UpdatePanel>
        
    </asp:Content>
    

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Drawing;
    
    
    public partial class Registration : System.Web.UI.Page
    {
    
        string sc = ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString.ToString();
    
        protected void Page_Load(object sender, EventArgs e)
        {
    
           
        }
       
    
        protected void btnSave_Click(object sender, EventArgs e)
        {
        
             SqlConnection cn = new SqlConnection(sc);
             cn.Open();
             SqlCommand cmd = new SqlCommand();
            string sqlstatment= "INSERT INTO [dbo].[UserInfo]"+
              " ([UID]"+
              " ,[FN] "+
              " ,[LN]" +
               ",[Password] "+
              " ,[RePass] "+
              " ,[Email] "+
              " ,[CountID] "+
              " ,[State] "+
              " ,[City] "+
              " , [Post] " +
    
              "  ,[Img]) values (@UID,@FN,@LN,@Password,@RePass,@Email,@CountID,@State,@City,@Post,@Img)";
    
    
             cmd.CommandType = CommandType.Text;
             cmd.CommandText = sqlstatment;
             cmd.Connection = cn;
    
             cmd.Parameters.AddWithValue("@UID", UsrNme.Text);
             cmd.Parameters.AddWithValue("@FN", fnbox.Text);
             cmd.Parameters.AddWithValue("@LN", lnamebox.Text);
             cmd.Parameters.AddWithValue("@Password", passtxtbx1.Text);
             cmd.Parameters.AddWithValue("@RePass", passtxtbx2.Text);
             cmd.Parameters.AddWithValue("@Email", emailbox.Text);
             cmd.Parameters.AddWithValue("@CountID", DDLCOUNTRY.Text);
             cmd.Parameters.AddWithValue("@State", DDLSTATE.Text);
             cmd.Parameters.AddWithValue("@City", citytxtbox.Text);
             cmd.Parameters.AddWithValue("@Post",postbox.Text);
             cmd.Parameters.AddWithValue("@Img", FileUpload1.FileName);
    
            if (FileUpload1.HasFile && FileUpload1.PostedFile.ContentLength > 0)
            {
    
                FileUpload1.SaveAs(Server.MapPath("~/images/users/" + FileUpload1.FileName));
                
            }
    
    
           
             SqlDataAdapter ad = new SqlDataAdapter(cmd);
             DataSet ds = new DataSet();
             ad.SelectCommand = cmd;
             ad.Fill(ds);
             Response.Redirect("User panel.aspx");
        }
        
    }
    
    
       
            
    
    
    
    
    
       
    

    Saturday, March 29, 2014 8:30 PM

Answers

  • User-933407369 posted

    According to your description, i would suggest you try the codes below:

    if (FileUpload1.HasFile && FileUpload1.PostedFile.ContentLength > 0)
            {
    
                FileUpload1.SaveAs(Server.MapPath("~/images/users/" + FileUpload1.FileName));
                
            }
           
             // SqlDataAdapter ad = new SqlDataAdapter(cmd);
             // DataSet ds = new DataSet();
             //ad.SelectCommand = cmd;
             //ad.Fill(ds);
           cmd.ExecuteNonQuery();
      cn.Close();

    Hope it helps you, If you have any questions please do not hesitate to contact me.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 31, 2014 6:03 AM

All replies

  • User-1716253493 posted
    You need to run cmd.ExecuteNonQuery() to insert the data.
    Saturday, March 29, 2014 8:59 PM
  • User-464009799 posted

    Hi 

    Thank you for your reply, but could you explain how i can insert cmd.ExecuteNonQuery() and where in my code. As i am still beginner in asp.net

    Saturday, March 29, 2014 9:27 PM
  • User-933407369 posted

    According to your description, i would suggest you try the codes below:

    if (FileUpload1.HasFile && FileUpload1.PostedFile.ContentLength > 0)
            {
    
                FileUpload1.SaveAs(Server.MapPath("~/images/users/" + FileUpload1.FileName));
                
            }
           
             // SqlDataAdapter ad = new SqlDataAdapter(cmd);
             // DataSet ds = new DataSet();
             //ad.SelectCommand = cmd;
             //ad.Fill(ds);
           cmd.ExecuteNonQuery();
      cn.Close();

    Hope it helps you, If you have any questions please do not hesitate to contact me.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 31, 2014 6:03 AM