我为停车场创建了一个简单的项目。当我注册任何用户并将其保存在访问数据库中。保存对话框后显示并要求我保存该用户但是当显示对话框时出现问题有关访问冲突异常的错误。读取或写入受保护的内存我不知道怎么修。 我阅读了一些关于此错误的博客和帖子,但没有提供正确的解决方案,也没有发布任何正确的想法。 下面是我的完整代码,我该如何解决。
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);
}
答案 0 :(得分:-1)
请尝试添加s.InitialDirectory = "D:\\";
以防止在对话框打开时访问特殊目录