2016-05-15 7 views
-1

私は車のための簡単なプロジェクトを作成しました。私は任意のユーザーを登録し、アクセスデータベースに保存します。ダイアログボックスが表示され、ダイアログボックスが表示され、ダイアログボックスが表示されているときにそのユーザーを保存するよう尋ねると、アクセス違反例外について出てきます。直し方。 私はいくつかのブログとこのエラーについての記事を読んでいますが、正しい解決策を提供しておらず、正しいアイデアも投稿していません。 以下は私の完全なコードです。どうすれば修正できますか?C#ファイルの保存ダイアログボックスのエラー

  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 , [email protected],[email protected]condimage 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

ファイルの書き込み権限を持っているファイルを保存する場所を開いたとき、特殊なディレクトリへのアクセスを防止するためにs.InitialDirectory = "D:\\";を追加してみてください? – Mostafiz

+0

あなたのコードをダンプして、他の人にあなたのコードをデバッグするよう頼んではいけません!それを再現するための小さな例を書いてください! –

+0

私のプロジェクトはローカルディスクにあり、いつもここにファイルを保存する権限があります – user5832488

答えて

関連する問題