Last post Mar 31, 2014 06:03 AM by Happy Chen - MSFT
Member
329 Points
743 Posts
Mar 29, 2014 08:30 PM|msimo|LINK
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"> 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"> 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"> </td> </tr> <tr> <td class="style5"> 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"> </td> </tr> <tr> <td class="style5"> 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"> Å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"> 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> </td> </tr> <tr> <td class="style5"> 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"> </td> </tr> <tr> <td class="style5"> 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"> </td> </tr> <tr> <td class="style5"> 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"> 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"> </td> </tr> <tr> <td class="style5"> Personlig bild </td> <td style="text-align: left" class="style8"> <asp:FileUpload ID="FileUpload1" runat="server" Width="230px" /> </td> <td style="text-align: left"> </td> </tr> <tr> <td> </tr> <tr> <td class="style5"> </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"); } }
fileupload asp.net
All-Star
52523 Points
15672 Posts
Mar 29, 2014 08:59 PM|oned_gk|LINK
Mar 29, 2014 09:27 PM|msimo|LINK
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
15648 Points
2151 Posts
Mar 31, 2014 06:03 AM|Happy Chen - MSFT|LINK
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.
Member
329 Points
743 Posts
cant store image via fileupload control
Mar 29, 2014 08:30 PM|msimo|LINK
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
fileupload asp.net
All-Star
52523 Points
15672 Posts
Re: cant store image via fileupload control
Mar 29, 2014 08:59 PM|oned_gk|LINK
fileupload asp.net
Suwandi - Non Graduate Programmer
Member
329 Points
743 Posts
Re: cant store image via fileupload control
Mar 29, 2014 09:27 PM|msimo|LINK
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
fileupload asp.net
All-Star
15648 Points
2151 Posts
Re: cant store image via fileupload control
Mar 31, 2014 06:03 AM|Happy Chen - MSFT|LINK
According to your description, i would suggest you try the codes below:
Hope it helps you, If you have any questions please do not hesitate to contact me.
fileupload asp.net