C#保存文件对话框错误

C#保存文件对话框错误,c#,.net,visual-studio-2010,C#,.net,Visual Studio 2010,我创建了一个简单的停车项目。当我注册任何用户并将其保存在access数据库中时。保存后会显示对话框并要求我保存该用户,但当显示对话框时会出现有关访问冲突异常的错误。读取或写入受保护内存我不知道如何修复。 我读了一些关于这个错误的博客和帖子,但没有提供正确的解决方案,也没有发表任何正确的想法。 下面是我的完整代码如何修复 using System; using System.Collections.Generic; using System.

我创建了一个简单的停车项目。当我注册任何用户并将其保存在access数据库中时。保存后会显示对话框并要求我保存该用户,但当显示对话框时会出现有关访问冲突异常的错误。读取或写入受保护内存我不知道如何修复。 我读了一些关于这个错误的博客和帖子,但没有提供正确的解决方案,也没有发表任何正确的想法。 下面是我的完整代码如何修复

            using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Windows.Forms;
        using System.Configuration;
        using System.Data.OleDb;
        using MessagingToolkit.QRCode.Codec;
        using MessagingToolkit.QRCode.Codec.Data;
        using System.IO;
        using NPR.Properties;

        namespace NPR
        {
        public partial class UserAdd : Form
        {
        public UserAdd()
        {
        InitializeComponent();
        }
        private int userId = 0;
        public int UserId
        {
        get { return userId; }
        set { userId = value; }
        }

        private bool isUpdate = false;
        public bool IsUpdate
        {
        get { return isUpdate; }
        set { isUpdate = value; }

        }

        private void UpdateRecord()
        {
        string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
        string cmdString = "Update users SET u_name = @name,u_car_no = @car_no, u_mobile_no = @mobile_no,u_license_no = @license_no,u_reg_date = @reg_date , u_image=@firstimage,u_car_background=@secondimage WHERE Id = @userId";
        using (OleDbConnection con = new OleDbConnection(connString))
        {
        using (OleDbCommand cmd = new OleDbCommand(cmdString, con))
        {
        con.Open();

        cmd.Parameters.AddWithValue("@name", NameTextBox.Text);
        cmd.Parameters.AddWithValue("@car_no", PlateNoTextBox.Text);
        cmd.Parameters.AddWithValue("@mobile_no", MobileTextBox.Text);
        cmd.Parameters.AddWithValue("@license_no", LicenseTextBox.Text);
        cmd.Parameters.AddWithValue("@reg_date", DateTime.Text);
        cmd.Parameters.AddWithValue("@firstimage", savePhoto());
        cmd.Parameters.AddWithValue("@secondimage", savePhoto2());
        cmd.Parameters.AddWithValue("@userId", this.userId);
        cmd.ExecuteNonQuery();
        }
        }
        }

        private void SaveRecord()
        {
        string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
        string cmdString = "INSERT INTO users (u_name,u_car_no,u_mobile_no,u_license_no,u_reg_date,u_image,u_car_background) VALUES (@name,@car_no,@mobile_no,@license_no,@reg_date,@firstimage,@secondimage)";
        using (OleDbConnection con = new OleDbConnection(connString))
        {
        using (OleDbCommand cmd = new OleDbCommand(cmdString, con))
        {
        con.Open();
        cmd.Parameters.AddWithValue("@name", NameTextBox.Text);
        cmd.Parameters.AddWithValue("@car_no", PlateNoTextBox.Text);
        cmd.Parameters.AddWithValue("@mobile_no", MobileTextBox.Text);
        cmd.Parameters.AddWithValue("@license_no", LicenseTextBox.Text);
        cmd.Parameters.AddWithValue("@reg_date", DateTime.Text);
        cmd.Parameters.AddWithValue("@firstimage", savePhoto());
        cmd.Parameters.AddWithValue("@secondimage", savePhoto2());

        cmd.ExecuteNonQuery();
        }
        }
        }

        private byte[] savePhoto()
        {
        MemoryStream ms = new MemoryStream();
        FirstpictureBox.Image.Save(ms, FirstpictureBox.Image.RawFormat);
        return ms.GetBuffer();
        }
        private byte[] savePhoto2()
        {
        MemoryStream ms = new MemoryStream();
        SecondpictureBox.Image.Save(ms, SecondpictureBox.Image.RawFormat);
        return ms.GetBuffer();
        }

        private bool IsValidated()
        {
        if (NameTextBox.Text.Trim() == string.Empty)
        {
        MessageBox.Show("Name is Required.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        NameTextBox.Focus();
        return false;
        }
        if (PlateNoTextBox.Text.Trim() == string.Empty)
        {
        MessageBox.Show("Car Plate No. is Required.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        PlateNoTextBox.Focus();
        return false;
        }
        if (MobileTextBox.Text.Trim() == string.Empty)
        {
        MessageBox.Show("Mobile No. is Required.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        MobileTextBox.Focus();
        return false;
        }
        if (LicenseTextBox.Text.Trim() == string.Empty)
        {
        MessageBox.Show("License No. is Required.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        NameTextBox.Focus();
        return false;
        }
        if (DateTime.Text.Trim() == string.Empty)
        {
        MessageBox.Show("Date is Required.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        DateTime.Focus();
        return false;
        }
        return true;

        }

        private DataTable GetUserInfoById()
        {
        DataTable dtUsersInfo = new DataTable();
        string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
        string cmdString = "SELECT * FROM users WHERE Id = @UserId";
        using (OleDbConnection con = new OleDbConnection(connString))
        {
        using (OleDbCommand cmd = new OleDbCommand(cmdString, con))
        {
        con.Open();
        cmd.Parameters.AddWithValue("@UserId", this.UserId);
        OleDbDataReader reader = cmd.ExecuteReader();
        dtUsersInfo.Load(reader);
        }
        }

        return dtUsersInfo;
        }

        private Image LoadImg(byte[] img)
        {
        MemoryStream ms = new MemoryStream(img);
        return Image.FromStream(ms);
        }

        private void button2_Click(object sender, EventArgs e)
        {
        this.Close();
        }

        private void SecondpictureBox_Click(object sender, EventArgs e)
        {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Title = "Select Car Background Image";
        ofd.Filter = "Image File(*.png;*.jpg;*.bmp;*.gif)|*.png;*.jpg;*.bmp;*.gif";
        if (ofd.ShowDialog() == DialogResult.OK)
        {
        SecondpictureBox.Image = new Bitmap(ofd.FileName);
        }
        }

        private void FirstpictureBox_Click(object sender, EventArgs e)
        {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Title = "Select User Profile Image";
        ofd.Filter = "Image File(*.png;*.jpg;*.bmp;*.gif)|*.png;*.jpg;*.bmp;*.gif";
        if (ofd.ShowDialog() == DialogResult.OK)
        {
        FirstpictureBox.Image = new Bitmap(ofd.FileName);
        }
        }

        private void button3_Click(object sender, EventArgs e)
        {
        DataTable dtUsers = GetUserInfoById();
        DataRow row = dtUsers.Rows[0];
        PlateNoTextBox.Text = row["u_car_no"].ToString();

        string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
        string cmdString = "DELETE * FROM users WHERE Id = @UserId";
        using (OleDbConnection con = new OleDbConnection(connString))
        {
        using (OleDbCommand cmd = new OleDbCommand(cmdString, con))
        {
        con.Open();
        cmd.Parameters.AddWithValue("@UserId", this.UserId);
        cmd.ExecuteNonQuery();
        }
        }
        string connString2 = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
        string camdString = "Update slots SET u_name = @name,u_car_no = @car_no,Status = 0 WHERE u_car_no = @slotId";
        using (OleDbConnection conn = new OleDbConnection(connString2))
        {
        using (OleDbCommand cmd = new OleDbCommand(camdString, conn))
        {
        conn.Open();
        cmd.Parameters.AddWithValue("@name", " ");
        cmd.Parameters.AddWithValue("@car_no", " ");
        cmd.Parameters.AddWithValue("@slotId", row["u_car_no"]);
        cmd.ExecuteNonQuery();
        conn.Close();
        conn.Dispose();
        }
        }
        MessageBox.Show("User Deleted Successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        this.Close();
        }

        private void viewDataToolStripMenuItem_Click(object sender, EventArgs e)
        {
        this.Hide();
        AllUserDetail mef = new AllUserDetail();
        mef.ShowDialog();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
        this.Hide();
        Dashboard dsh = new Dashboard();
        dsh.ShowDialog();
        }

        private void UserAdd_Load(object sender, EventArgs e)
        {
        if (this.IsUpdate)
        {
        DataTable dtUsers = GetUserInfoById();
        DataRow row = dtUsers.Rows[0];
        NameTextBox.Text = row["u_name"].ToString();
        PlateNoTextBox.Text = row["u_car_no"].ToString();
        MobileTextBox.Text = row["u_mobile_no"].ToString();
        LicenseTextBox.Text = row["u_license_no"].ToString();
        DateTime.Text = row["u_reg_date"].ToString();
        FirstpictureBox.Image = (row["u_image"] is DBNull) ? Resources.no_thumb : LoadImg((byte[])row["u_image"]);
        SecondpictureBox.Image = (row["u_car_background"] is DBNull) ? Resources.no_thumb : LoadImg((byte[])row["u_car_background"]);
        }
        }

        private void button1_Click(object sender, EventArgs e)
        {
        if (IsValidated())
        {
        try
        {
        if (this.isUpdate)
        {
        UpdateRecord();
        String plate = PlateNoTextBox.Text;
        QRCodeEncoder encoder = new QRCodeEncoder();
        Bitmap qrcode = encoder.Encode(plate);
        qrimage.Image = qrcode as Image;
        MessageBox.Show("Record Updated Successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        SaveFileDialog s = new SaveFileDialog();
        s.Title = "Save QR Code";
        s.Filter = "JPEG|*.jpg|PNG|*.png|BMP|*.bmp|GIF|*.gif";
        if (s.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
        try
        {
        qrimage.Image.Save(s.FileName);
        }
        catch (ApplicationException ex)
        {
        MessageBox.Show("ERROR:" + ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }
        }
        }
        else
        {
        SaveRecord();
        String plate = PlateNoTextBox.Text;
        QRCodeEncoder encoder = new QRCodeEncoder();
        Bitmap qrcode = encoder.Encode(plate);
        qrimage.Image = qrcode as Image;
        MessageBox.Show("Record Save Successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        SaveFileDialog s = new SaveFileDialog();
        s.Title = "Save QR Code";
        s.Filter = "JPEG|*.jpg|PNG|*.png|BMP|*.bmp|GIF|*.gif";

        if (s.ShowDialog() == DialogResult.OK)
        {
        try
        {
        qrimage.Image.Save(s.FileName);
        }
        catch (ApplicationException ex)
        {
        MessageBox.Show("ERROR:" + ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        }
        }
        this.Close();
        }
        catch (ApplicationException ex)
        {
        MessageBox.Show("ERROR:" + ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }
        }
        }
        }
        }
我在
SaveRecord()中遇到的问题函数”对话框

if (s.ShowDialog() == DialogResult.OK)   //Here is error
        {
        try
        {
        qrimage.Image.Save(s.FileName);
        }

请尝试添加
s.InitialDirectory=“D:\\”在对话框打开时阻止访问特殊目录

您保存文件的位置是否具有文件写入权限?请不要转储代码,要求其他人调试您的代码!写一个小例子来重现它!!!我的项目在本地磁盘d中,并且始终具有在此保存文件的权限我被发布,因为任何知道此异常的人都会很容易地告诉我我的错误。@user5832488 AcessViolationException,正如您可以想象的,是关于指针(或与本机代码的互操作)的一些奇怪的事情。任何了解此异常的人都无法帮助您,除非您浏览所有代码以查找愚蠢的错误或启动调试会话(发布时您应该首先与MCVE一起执行的操作)