Raziyeh Karimi

Raziyeh Karimi

  • NA
  • 5
  • 8.2k

The SqlParameterCollection only accepts non-null SqlParamete

May 6 2013 4:42 PM
hi me code is this

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class member : System.Web.UI.Page
{
public string GetConnectionString()
{

//sets the connection string from your web config file "ConnString" is the name of your Connection String

return System.Configuration.ConfigurationManager.ConnectionStrings["ing"].ConnectionString;

}
protected void Page_Load(object sender, EventArgs e)
{
}
private void ExecuteInsert(string stdid,string name,string lastname,string jens,string fathername,string kodmali,string saril,string school,string maghtae,string reshte,string paye,string address,string mobil,string stdtel,string Email,string imegstd,string anjomana,string anjomanb )
{
SqlConnection conn = new SqlConnection(GetConnectionString());
SqlCommand cmd = new SqlCommand("INSERT INTO student"+ "( stdid,name,lastname,jens,fathername,kodmali,saril,school,maghtae,reshte,paye,address,mobil,stdtel,Email,imegstd,anjomana,anjomanb )"
+
"values" + " (@stdid,@name,@lastname,@jens,@fathername,@kodmali,@saril,@school,@maghtae,@reshte,@paye,@address,@mobil,@stdtel,@Email,@imegstd,@anjomana,@anjomanb)", conn);

try
{
cmd.CommandType =
CommandType.Text;
conn.Open();

// SqlCommand cmd = new SqlCommand(sql, conn);

cmd.Parameters.Add(
new SqlParameter(@stdid, SqlDbType.VarChar, 10).Value= stdid) ;
cmd.Parameters.Add(
new SqlParameter(@name, SqlDbType.VarChar, 20).Value = name);
cmd.Parameters.Add(
new SqlParameter(@lastname, SqlDbType.VarChar, 30).Value = lastname);
cmd.Parameters.Add(
new SqlParameter(@jens, SqlDbType.VarChar, 10).Value = jens);
cmd.Parameters.Add(
new SqlParameter(@fathername, SqlDbType.VarChar, 20).Value = fathername);
cmd.Parameters.Add(
new SqlParameter(@kodmali, SqlDbType.VarChar, 10).Value = kodmali);
cmd.Parameters.Add(
new SqlParameter(@saril, SqlDbType.VarChar, 10).Value = saril);
cmd.Parameters.Add(
new SqlParameter(@school, SqlDbType.VarChar, 30).Value = school);
cmd.Parameters.Add(
new SqlParameter(@maghtae, SqlDbType.VarChar, 10).Value = maghtae);
cmd.Parameters.Add(
new SqlParameter(@reshte, SqlDbType.VarChar, 20).Value = reshte);
cmd.Parameters.Add(
new SqlParameter(@paye, SqlDbType.VarChar, 10).Value = paye);
cmd.Parameters.Add(
new SqlParameter(@address, SqlDbType.VarChar, 50).Value = address);
cmd.Parameters.Add(
new SqlParameter(@mobil, SqlDbType.VarChar, 10).Value = mobil);
cmd.Parameters.Add(
new SqlParameter(@stdtel, SqlDbType.VarChar, 10).Value = stdtel);
cmd.Parameters.Add(
new SqlParameter(@Email, SqlDbType.VarChar, 20).Value = Email);
cmd.Parameters.Add(
new SqlParameter(@imegstd, SqlDbType.Image, 20).Value = imegstd);
cmd.Parameters.Add(
new SqlParameter(@anjomana, SqlDbType.VarChar, 20).Value = anjomana);
cmd.Parameters.Add(
new SqlParameter(@anjomanb, SqlDbType.VarChar, 20).Value = anjomanb);

cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
protected void Btn1_Click(object sender, EventArgs e)
{
}
protected void Butn2_Click(object sender, EventArgs e)
{
ExecuteInsert(Tx4.Text, Tx1.Text,Tx2.Text,DropDownList1.SelectedItem.Text,Tx6.Text,Tx3.Text,Tx5.Text,Tx7.Text,DropDownList2.SelectedItem.Text,Tx18.Text,Tx19.Text,Tx10.Text,Tx9.Text,Tx8.Text,Tx11.Text,Tx14.Text,DropDownList3.SelectedItem.Text,DropDownList4.SelectedItem.Text);
Label17.Text =
"fff";

}
}
 
but i see this The SqlParameterCollection only accepts non-null SqlParameteThe SqlParameterCollection only accepts non-null SqlParamete

Answers (3)