سلام ...
ببخشید من تمام کارهای جلسه 19 و 20 دوره مقدماتی تا پیشرفته سی شارپ رو انجام دادم و برای قسمت 19 موفقیت آمیز بود یعنی تونستم اطلاعات بانک رو توی جدول نمایش بدم اما وقتی کدهای جلسه 20 رو مینویسم نمیتونه اطلاعات رو وارد sql server کنه و همون پیامی رو که خودم برای false شدنش نوشتم نشون میده یعنی عملیات با شکست مواجه شد .
تمام کد هایی که تو برنامه نوشتم درسته چون با برنامه ای که آقای مدائنی نوشتن هم امتحان کردم نشد (البته توی برنامه آقای مدائنی اسم بانک رو تغییر دادم و اسم بانک خودم رو گذاشتم )
ممنون میشم مشکلم رو حل کنید
سلام. کدها ؟
کد های Interface به نام ICantactsRepozatory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace Contacts_asli
{
interface ICantactsRepozatory
{
DataTable SellectAll();
DataTable Sellectrow(int contactid);
bool Insert(string name,string family , string mobile , string email ,string age , string address);
bool Update(int contactId ,string name, string family, string mobile, string email, string age, string address);
bool Delete(int cotactId);
}
}
کدهای کلاسی که توی پوشه Servies هست به نام ContactRepository
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace Contacts_asli
{
class ContactRepository : ICantactsRepozatory
{
private string connectionstring = "Data Source =.;Initial Catalog = Contacts ; User ID = ** ;Password =***********";
public bool Delete(int cotactId)
{
throw new NotImplementedException();
}
public bool Insert(string name, string family, string mobile, string email, string age, string address)
{
SqlConnection connection = new SqlConnection(connectionstring);
try
{
string sql = "insert into MyContacts (Name,Family,Mobile,Emaile,Age,Address) valus (@name,@family,@mobile,@emaile,@age,@address)";
SqlCommand command = new SqlCommand(sql, connection);
command.Parameters.AddWithValue("@name", name);
command.Parameters.AddWithValue("@family", family);
command.Parameters.AddWithValue("@mobile", mobile);
command.Parameters.AddWithValue("@emaile", email);
command.Parameters.AddWithValue("@age", age);
command.Parameters.AddWithValue("@address", address);
connection.Open();
command.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
finally
{
connection.Close();
}
}
public DataTable SellectAll()
{
string qury = "select * from MyContacts";
SqlConnection connection = new SqlConnection(connectionstring);
SqlDataAdapter adapter = new SqlDataAdapter(qury, connection);
DataTable data = new DataTable();
adapter.Fill(data);
return data;
}
public DataTable Sellectrow(int contactid)
{
throw new NotImplementedException();
}
public bool Update(int contactId, string name, string family, string mobile, string email, string age, string address)
{
throw new NotImplementedException();
}
}
}
کد های فرم اصلی به نام Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Contacts_asli
{
public partial class Form1 : Form
{
ICantactsRepozatory Repozatory;
public Form1()
{
InitializeComponent();
Repozatory = new ContactRepository();
}
private void Form1_Load(object sender, EventArgs e)
{
BindGride();
}
private void BindGride()
{
dgContacts.AutoGenerateColumns = false;
dgContacts.DataSource = Repozatory.SellectAll();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
BindGride();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
frmAddOrEdit frm = new frmAddOrEdit();
frm.ShowDialog();
if (frm.DialogResult == DialogResult.OK)
{
BindGride();
}
}
}
}
کدهای فرم افزودن شخص به نام frmAddOrEdit
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Contacts_asli
{
public partial class frmAddOrEdit : Form
{
ICantactsRepozatory Repozatory;
public frmAddOrEdit()
{
InitializeComponent();
Repozatory = new ContactRepository();
}
private void frmAddOrEdit_Load(object sender, EventArgs e)
{
}
bool ValidateInputs()
{
if (txtname.Text == "")
{
MessageBox.Show("لطفا نام را وارد کنید!", "ارور", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
if (txtfamily.Text == "")
{
MessageBox.Show("لطفا نام خانوادگی را وارد کنید!", "ارور", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
if (txtmobile.Text == "")
{
MessageBox.Show("لطفا موبایل را وارد کنید!", "ارور", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
if (txtage.Text == "")
{
MessageBox.Show("لطفا سن را وارد کنید!", "ارور", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
if (txtemail.Text=="")
{
MessageBox.Show("لطفا ایمیل را وارد کنید!", "ارور", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}
private void btnsubmit_Click(object sender, EventArgs e)
{
if (ValidateInputs())
{
bool isSuccess = Repozatory.Insert(txtname.Text, txtfamily.Text, txtmobile.Text, txtemail.Text, txtage.Text, txtaddres.Text);
if (isSuccess == true)
{
MessageBox.Show("عملیات با موفقیت انجام شد", "عملیات موفق", MessageBoxButtons.OK, MessageBoxIcon.Information); ;
DialogResult = DialogResult.OK;
}
else
{
MessageBox.Show("عملیات با شکست مواحه شد", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error); ;
}
}
}
}
}
اطلاعات اشخاصی که توی sql هستن رو به درستی توی فرم اصلی نشون میده فقط موقع افزودن شخص جدید پیام زیر میاد
اینم عکس sql من
سلام. کدها زیاده و نمیشه خط به خط خوند، دیباگ کنید