我想使用以下c#代码保存数据,但它不起作用

时间:2016-10-14 07:40:42

标签: c# sql asp.net

我想使用带有以下c#的asp.net上传两张pic和一个文件以及其他数据。但是它给出了Uploadresumepic.SaveAs(path1)的错误;

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class resume_add : System.Web.UI.Page
{
    string strConnString = ConfigurationManager.ConnectionStrings["JOBConnectionString1"].ConnectionString;
    string f1;
    string f2;
    string f3;
    string path1;
    string path2;
    string path3;
    protected void Page_Load(object sender, EventArgs e)
    {

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

        using (SqlConnection con = new SqlConnection(strConnString))
        {

            if (Uploadresumepic.PostedFile.ContentLength > 0)
            {
                f1 = Path.GetFileName(Uploadresumepic.FileName);
                path1 = Server.MapPath("profile_pic") + "/" + f1;
                Uploadresumepic.SaveAs(path1);
            }
            if (FileUploadresumefile.PostedFile.ContentLength > 0)
            {
                f2 = Path.GetFileName(FileUploadresumefile.FileName);
                path2 = Server.MapPath("resume_file") + "/" + f2;
                FileUploadresumefile.SaveAs(path2);
            }
            if (coverimage.PostedFile.ContentLength > 0)
            {
                f3 = Path.GetFileName(coverimage.FileName);
                path3 = Server.MapPath("cover_image") + "/" + f3;
                coverimage.SaveAs(path3);
            }
            using (SqlCommand com = new SqlCommand("Recruiter_detail"))
            {

                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    com.CommandType = CommandType.StoredProcedure;
                    com.Connection = con;
                    com.Parameters.AddWithValue("@profile_pic", f1);
                    com.Parameters.AddWithValue("@name", TextName.Text.Trim());
                    com.Parameters.AddWithValue("@headline", TxtHedline.Text.Trim());
                    com.Parameters.AddWithValue("@short_description", textdescription.Text.Trim());
                    com.Parameters.AddWithValue("@location", TextBoxlocation.Text.Trim());
                    com.Parameters.AddWithValue("@website_address", TextBoxadress.Text.Trim());
                    com.Parameters.AddWithValue("@salary", TextBoxsalary.Text.Trim());
                    com.Parameters.AddWithValue("@phone_no", TxtBoxphoneno.Text.Trim());
                    com.Parameters.AddWithValue("@resume_file", f2);
                    com.Parameters.AddWithValue("@cover_image", f3);
                    com.Parameters.AddWithValue("@facebook_Url", FbURL.Text.Trim());
                    com.Parameters.AddWithValue("@Twitter_Url", TwitterUrl.Text.Trim());
                    com.Parameters.AddWithValue("@google_url", GoogleUrl.Text.Trim());
                    com.Parameters.AddWithValue("@youtube_url", YoutubeUrl.Text.Trim());
                    com.Parameters.AddWithValue("@age", TextBoxage.Text.Trim());
                    com.Connection = con;
                    con.Open();
                    com.ExecuteNonQuery();
                    con.Close();
                }

                Lblsubmit.Text = "Resume Created";
            }

        }
    }

我使用的sql create procedure是:

USE [JOB]
GO


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[Recruiter_detail]
    @profile_pic varchar(50),
    @name varchar(50),
    @headline nvarchar(50),
    @short_description nvarchar(50),
    @location nvarchar(50),
    @website_address nvarchar(50),
    @salary nvarchar(50),
    @phone_no nvarchar(50),
    @resume_file nvarchar(50),
    @cover_image nvarchar(50),
    @facebook_Url nvarchar(50),
    @Twitter_Url nvarchar(50),
    @google_url nvarchar(50),
    @youtube_url nvarchar(50)
AS
BEGIN
    SET NOCOUNT ON;
    IF EXISTS(SELECT Recruiter_id FROM Recruiter WHERE Username = '" + Session["name"] + "')
    BEGIN
        SELECT -1 
    END

    ELSE
    BEGIN
        INSERT INTO [Recruiter]
               ([profile_pic]
               ,[name]
               ,[headline]
               ,[short_description]
               ,[location]
               ,[website_address]
               ,[salary]
               ,[phone_no]
               ,[resume_file]
               ,[cover_image]
               ,[facebook_Url]
               ,[Twitter_Url]
               ,[google_url]
               ,[youtube_url]
               )
        VALUES
               (@profile_pic
               ,@name
               ,@headline
               ,@short_description
               ,@location
               ,@website_address
               ,@salary
               ,@phone_no
               ,@resume_file
               ,@cover_image
               ,@facebook_Url
               ,@Twitter_Url
               ,@google_url
               ,@youtube_url
               )


        SELECT SCOPE_IDENTITY() -- Recruiter_id            
     END
END


GO

我正在使用

的aspx代码中的以下标记
<div class="row">
                <div class="col-xs-12 col-sm-4">
                    <div class="form-group">

                        <asp:FileUpload ID="Uploadresumepic" type="file" class="dropify" data-default-file="assets/img/avatar.jpg" runat="server" />
                        <span class="help-block">Please choose a 4:6 profile picture.</span>
                    </div>
                </div>

                <div class="col-xs-12 col-sm-8">
                    <div class="form-group">
                        <asp:TextBox ID="TextName" type="text" class="form-control input-lg" placeholder="Name" runat="server"></asp:TextBox>
                    </div>

                    <div class="form-group">
                        <asp:TextBox ID="TxtHedline" type="text" class="form-control" placeholder="Headline (e.g. Front-end developer)" runat="server"></asp:TextBox>
                    </div>

                    <div class="form-group">
                        <asp:TextBox ID="textdescription" class="form-control" rows="3" placeholder="Short description about you" runat="server"></asp:TextBox>
                    </div>

                    <hr class="hr-lg">

                    <h6>Basic information</h6>
                    <div class="row">

                        <div class="form-group col-xs-12 col-sm-6">
                            <div class="input-group input-group-sm">
                                <span class="input-group-addon"><i class="fa fa-map-marker"></i></span>
                                <asp:TextBox ID="TextBoxlocation" type="text" class="form-control" placeholder="Location, e.g. Melon Park, CA" runat="server"></asp:TextBox>
                            </div>
                        </div>

                        <div class="form-group col-xs-12 col-sm-6">
                            <div class="input-group input-group-sm">
                                <span class="input-group-addon"><i class="fa fa-globe"></i></span>
                                <asp:TextBox ID="TextBoxadress" type="text" class="form-control" placeholder="Website address" runat="server"></asp:TextBox>
                            </div>
                        </div>

                        <div class="form-group col-xs-12 col-sm-6">
                            <div class="input-group input-group-sm">
                                <span class="input-group-addon"><i class="fa fa-usd"></i></span>
                                <asp:TextBox ID="TextBoxsalary" type="text" class="form-control" placeholder="Salary, e.g. 85" runat="server"></asp:TextBox>
                                <span class="input-group-addon">Per hour</span>
                            </div>
                        </div>

                        <div class="form-group col-xs-12 col-sm-6">
                            <div class="input-group input-group-sm">
                                <span class="input-group-addon"><i class="fa fa-birthday-cake"></i></span>
                                <asp:TextBox ID="TextBoxage" type="text" class="form-control" placeholder="Age" runat="server"></asp:TextBox>
                                <span class="input-group-addon">Years old</span>
                            </div>
                        </div>

                        <div class="form-group col-xs-12 col-sm-6">
                            <div class="input-group input-group-sm">
                                <span class="input-group-addon"><i class="fa fa-phone"></i></span>
                                <asp:TextBox ID="TxtBoxphoneno" type="text" class="form-control" placeholder="Phone number" runat="server"></asp:TextBox>
                            </div>
                        </div>

                        <div class="form-group col-xs-12 col-sm-6">
                            <div class="input-group input-group-sm">
                                <span class="input-group-addon"><i class="fa fa-envelope"></i></span>
                                <asp:TextBox ID="TextBoxaddress" type="text" class="form-control" placeholder="Email address" runat="server"></asp:TextBox>
                            </div>
                        </div>

                    </div>



                </div>
            </div>
<div class="button-group">
                <div class="action-buttons">

                    <div class="upload-button">
                        <button class="btn btn-block btn-gray">Choose a resume file</button>
                        <asp:FileUpload ID="FileUploadresumefile" type="file" runat="server" />
                    </div>

                    <div class="upload-button">
                        <button class="btn btn-block btn-primary">Choose a cover image</button>
                        <asp:FileUpload ID="coverimage" type="file" runat="server" />
                    </div>

                </div>
            </div>
<div class="row">
                <div class="col-xs-12 col-sm-6">
                    <div class="form-group">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-facebook"></i></span>
                            <asp:TextBox ID="FbURL" type="text" class="form-control" placeholder="Profile URL" runat="server"></asp:TextBox>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-google-plus"></i></span>
                            <asp:TextBox ID="GoogleUrl" type="text" class="form-control" placeholder="Profile URL" runat="server"></asp:TextBox>
                        </div>
                    </div>




                </div>

                <div class="col-xs-12 col-sm-6">
                    <div class="form-group">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-twitter"></i></span>
                            <asp:TextBox ID="TwitterUrl" type="text" class="form-control" placeholder="Profile URL" runat="server"></asp:TextBox>
                        </div>
                    </div>





                    <div class="form-group">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-youtube"></i></span>
                            <asp:TextBox ID="YoutubeUrl" type="text" class="form-control" placeholder="Profile URL" runat="server"></asp:TextBox>
                        </div>
                    </div>
                </div>
            </div>
<asp:Button ID="submitresumebtn" class="btn btn-success btn-xl btn-round" runat="server" Text="Submit your resume" OnClick="submitresumebtn_Click" />

1 个答案:

答案 0 :(得分:0)

只需将代码更改为

open