PDA

View Full Version : خطای دستور ()ExecuteNonQuery



bisotun
سه شنبه 08 آذر 1390, 00:58 صبح
سلام
یه سایت طراحی کردم و یک صفحه مربوط به سفارشات دارم ، در این صفحه کدهای مربوط به sql رو نوشتم ولی هنگام اجرا از دستور ()ExecuteNonQuery خطا میگیره .مشکل چیه؟


public partial class sefareshat : System.Web.UI.Page
{
public SqlConnection conect = new SqlConnection("Data Source=.;Initial Catalog=sepandsystem;Integrated Security=True");
string codeadmin;
protected void Page_Load(object sender, EventArgs e)
{
SqlCommand com = new SqlCommand("insert into tb_student (namesch,maghta,tedadstudent,tedadclass,namemodir, mobile,email,tel,city1,city2,address)values('" + TextBox1.Text + "','" +DropDownList2.Text+"','" + TextBox2.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + DropDownList1.Text +"','" + TextBox9.Text + "','" + TextBox3.Text + "',)", conect);

conect.Open();
com.ExecuteNonQuery();
conect.Close();
TextBox1.Text = "";
DropDownList2.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
TextBox6.Text = "";
TextBox7.Text = "";
TextBox8.Text = "";
TextBox9.Text = "";
DropDownList2.Text = "";

}
}

Hybrid
سه شنبه 08 آذر 1390, 01:02 صبح
سلام دوست عزیز ، چرا اینقدر به خودت زحمت میدی چرا از برنامه نویسی پارامتری استفاده نمیکنی
؟؟؟؟ موفق باشید....


Dim sqlconnection1 As New SqlConnection
Dim StringFormat6 As String
StringFormat6 = String.Format("Data Source=.\SQLEXPRESS;AttachDbFilename={0}\bank.mdf; Integrated Security=True;Connect Timeout=30;User Instance=True", Application.StartupPath.ToString())
sqlconnection1.ConnectionString = StringFormat6

Dim sqlDataAdapter1 As New SqlDataAdapter
sqlDataAdapter1.InsertCommand = New SqlCommand("insert into person(prsl_name,prsl_familyname) values(@prsl_name,@prsl_familyname)", sqlconnection1)
With sqlDataAdapter1
.InsertCommand.Parameters.AddWithValue("prsl_name", txtName.Text)
.InsertCommand.Parameters.AddWithValue("prsl_familyname", txtFamilyName.Text)
End With
If sqlDataAdapter1.InsertCommand.Connection.State <> ConnectionState.Open Then
sqlDataAdapter1.InsertCommand.Connection.Open()
End If
sqlDataAdapter1.InsertCommand.ExecuteNonQuery()
MsgBox("اطلاعات با موفقیت ثبت شدند")
sqlDataAdapter1.InsertCommand.Connection.Close()

این یه مثاله ....

bisotun
سه شنبه 08 آذر 1390, 01:28 صبح
مرسی ممنون،من از کدهای شما استفاده کردم ولی دوباره خطا میگیره

SqlConnection conect = new SqlConnection();
string StringFormat6 = null;
StringFormat6 = string.Format("Data Source=.;Initial Catalog=sepandsystem;Integrated Security=True");
conect.ConnectionString = StringFormat6;

SqlDataAdapter da = new SqlDataAdapter();
da.InsertCommand = new SqlCommand("insert into tb_student(namesch,maghta,tedadstudent,tedadclass, namemodir,mobile,email,tel,city1,city2,address) values (@namesch,@maghta,@tedadstudent,@tedadclass,@namem odir,@mobile,@email,@tel,@city1,@city2,@address)", conect);
var w = da;
w.InsertCommand.Parameters.AddWithValue("namesch", TextBox1.Text);
w.InsertCommand.Parameters.AddWithValue("namesch", DropDownList2.Text);
w.InsertCommand.Parameters.AddWithValue("namesch", TextBox2.Text);
w.InsertCommand.Parameters.AddWithValue("namesch", TextBox4.Text);
w.InsertCommand.Parameters.AddWithValue("namesch", TextBox5.Text);
w.InsertCommand.Parameters.AddWithValue("namesch", TextBox6.Text);
w.InsertCommand.Parameters.AddWithValue("namesch", TextBox7.Text);
w.InsertCommand.Parameters.AddWithValue("namesch", TextBox8.Text);
w.InsertCommand.Parameters.AddWithValue("namesch", DropDownList1.Text);
w.InsertCommand.Parameters.AddWithValue("namesch", TextBox9.Text);
w.InsertCommand.Parameters.AddWithValue("namesch", TextBox3.Text);

if(da.InsertCommand.Connection.State != conect.Open )
{
da.InsertCommand.Connection.Open();
}

da.InsertCommand.EndExecuteNonQuery();
da.InsertCommand.EndExecuteNonQuery();

par4parvaz
سه شنبه 08 آذر 1390, 08:49 صبح
سلام
لطف کنید خود خطا رو هم بزارید

bisotun
چهارشنبه 09 آذر 1390, 00:11 صبح
هنگام اجرا در مرورگر این کدها رو نشون میده:

http://up9.iranblog.com/images/6frlz72fpyzrbiyw.jpg

meisam3322
چهارشنبه 09 آذر 1390, 00:29 صبح
مرسی ممنون،من از کدهای شما استفاده کردم ولی دوباره خطا میگیره

SqlConnection conect = new SqlConnection();
string StringFormat6 = null;
StringFormat6 = string.Format("Data Source=.;Initial Catalog=sepandsystem;Integrated Security=True");
conect.ConnectionString = StringFormat6;

SqlDataAdapter da = new SqlDataAdapter();
da.InsertCommand = new SqlCommand("insert into tb_student(namesch,maghta,tedadstudent,tedadclass, namemodir,mobile,email,tel,city1,city2,address) values (@namesch,@maghta,@tedadstudent,@tedadclass,@namem odir,@mobile,@email,@tel,@city1,@city2,@address)", conect);
var w = da;
w.InsertCommand.Parameters.AddWithValue("namesch", TextBox1.Text);
w.InsertCommand.Parameters.AddWithValue("namesch", DropDownList2.Text);
w.InsertCommand.Parameters.AddWithValue("namesch", TextBox2.Text);
w.InsertCommand.Parameters.AddWithValue("namesch", TextBox4.Text);
w.InsertCommand.Parameters.AddWithValue("namesch", TextBox5.Text);
w.InsertCommand.Parameters.AddWithValue("namesch", TextBox6.Text);
w.InsertCommand.Parameters.AddWithValue("namesch", TextBox7.Text);
w.InsertCommand.Parameters.AddWithValue("namesch", TextBox8.Text);
w.InsertCommand.Parameters.AddWithValue("namesch", DropDownList1.Text);
w.InsertCommand.Parameters.AddWithValue("namesch", TextBox9.Text);
w.InsertCommand.Parameters.AddWithValue("namesch", TextBox3.Text);

if(da.InsertCommand.Connection.State != conect.Open )
{
da.InsertCommand.Connection.Open();
}

da.InsertCommand.EndExecuteNonQuery();
da.InsertCommand.EndExecuteNonQuery();


با سلام، توی کد شما چندین بار داخل فیلد namesch اطلاعات ذخیره شده است.

bisotun
چهارشنبه 09 آذر 1390, 00:35 صبح
اونا رو درست کردم ولی دوباره همون طوره

Saman Hashemi
چهارشنبه 09 آذر 1390, 08:31 صبح
به نظرم این قسمت اضافیه چون خود SqlDataAdapter باز و بسته کردن ارتباط مدیریت میکنه...!
if(da.InsertCommand.Connection.State != conect.Open )

{

da.InsertCommand.Connection.Open();

}

bisotun
چهارشنبه 09 آذر 1390, 15:22 عصر
SqlConnection conect = new SqlConnection();
string StringFormat6 = null;
StringFormat6 = string.Format("Data Source=.;Initial Catalog=sepandsystem;Integrated Security=True");
conect.ConnectionString = StringFormat6;

SqlDataAdapter da = new SqlDataAdapter();
da.InsertCommand = new SqlCommand("insert into tb_student(namesch,maghta,tedadstudent,tedadclass, namemodir,mobile,email,tel,city1,city2,address) values (@namesch,@maghta,@tedadstudent,@tedadclass,@namem odir,@mobile,@email,@tel,@city1,@city2,@address)", conect);

var w = da;
w.InsertCommand.Parameters.AddWithValue("namesch", TextBox1.Text);
w.InsertCommand.Parameters.AddWithValue("maghta", DropDownList2.Text);
w.InsertCommand.Parameters.AddWithValue("tedadstudent", TextBox2.Text);
w.InsertCommand.Parameters.AddWithValue("tedadclass", TextBox4.Text);
w.InsertCommand.Parameters.AddWithValue("namemodir", TextBox5.Text);
w.InsertCommand.Parameters.AddWithValue("mobile", TextBox6.Text);
w.InsertCommand.Parameters.AddWithValue("email", TextBox7.Text);
w.InsertCommand.Parameters.AddWithValue("tel", TextBox8.Text);
w.InsertCommand.Parameters.AddWithValue("city1", DropDownList1.Text);
w.InsertCommand.Parameters.AddWithValue("city2", TextBox9.Text);
w.InsertCommand.Parameters.AddWithValue("address", TextBox3.Text);
da.InsertCommand.EndExecuteNonQuery();


یک خطا میگیره No overload for method 'EndExecuteNonQuery' takes '0' arguments

Saman Hashemi
چهارشنبه 09 آذر 1390, 15:54 عصر
متد اشتباه استفاده کردی...!
باید از ExecuteNonQuery استفاده کنی نه EndExecuteNonQuery...!

bisotun
چهارشنبه 09 آذر 1390, 16:22 عصر
مرسی ممنون درست شد