[求助]connection连接错误
System.Web.Services.Protocols.SoapException: 服务器无法处理请求。 ---> System.InvalidOperationException: ExecuteReader: Connection 属性尚未初始化。at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean executing)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at TeleCommServices.OperatorManageServices.CreateCard(String CardNo, Int32 CustomerID, String PUK, String PIN, String UserPwd, Decimal Balance, DateTime Expiration) in c:\inetpub\wwwroot\telecommservices\operatormanageservices.asmx.cs:line 620
--- 内部异常堆栈跟踪的结尾 ---卡号创建失败!
创建卡号成功!
卡号是:13777214072,余额是:50时间期限是:2007-12-28 12:33:47
我的服务代码如下:
[WebMethod]
public void CreateCard(string CardNo, int CustomerID, string PUK, string PIN,
string UserPwd, decimal Balance, DateTime Expiration)
{
string ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConnectionString;
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = "usp_CreateCard";
comm.CommandType = CommandType.StoredProcedure;
SqlParameter param1 = new SqlParameter("@CardNo", SqlDbType.Char, 11);
param1.Direction = ParameterDirection.Input;
param1.Value = CardNo;
comm.Parameters.Add(param1);
SqlParameter param2 = new SqlParameter("@CustomerID", SqlDbType.Int);
param2.Direction = ParameterDirection.Input;
param2.Value = CustomerID;
comm.Parameters.Add(param2);
SqlParameter param3 = new SqlParameter("@PUK", SqlDbType.Char, 6);
param3.Direction = ParameterDirection.Input;
param3.Value = PUK;
comm.Parameters.Add(param3);
SqlParameter param4 = new SqlParameter("@PIN", SqlDbType.VarChar, 8);
param4.Direction = ParameterDirection.Input;
param4.Value = PIN;
comm.Parameters.Add(param4);
SqlParameter param5 = new SqlParameter("@UserPwd", SqlDbType.VarChar, 8);
param5.Direction = ParameterDirection.Input;
param5.Value = UserPwd;
comm.Parameters.Add(param5);
SqlParameter param6 = new SqlParameter("@Balance", SqlDbType.Decimal, 5);
param6.Direction = ParameterDirection.Input;
param6.Scale = 2;
param6.Value = Balance;
comm.Parameters.Add(param6);
SqlParameter param7 = new SqlParameter("@Expiration", SqlDbType.DateTime);
param7.Direction = ParameterDirection.Input;
param7.Value = Expiration;
comm.Parameters.Add(param7);
comm.ExecuteNonQuery();
conn.Close();
}
操作界面代码如下:
private void button1_Click_1(object sender, System.EventArgs e)
{
string CardNo = textBox1.Text;
string PIN = textBox2.Text;
string PUK = textBox3.Text;
int CustomerID = int.Parse(textBox4.Text);
decimal Balance = decimal.Parse(comboBox1.SelectedItem.ToString());
string UserPwd = textBox5.Text;
DateTime Expiration;
switch (comboBox1.SelectedItem.ToString())
{
case "100":
Expiration = DateTime.Now.AddYears(1);
break;
case "50":
Expiration = DateTime.Now.AddMonths(6);
break;
case "30":
Expiration = DateTime.Now.AddMonths(3);
break;
default:
Expiration = DateTime.Now;
break;
}
try
{
service.CreateCard(CardNo, CustomerID, PUK, PIN, UserPwd, Balance, Expiration);
}
catch (Exception ex)
{
richTextBox1.AppendText(ex.Message +"卡号创建失败!\r\n");
}
richTextBox1.AppendText("创建卡号成功!\r\n");
richTextBox1.AppendText("卡号是:"+CardNo+ ",余额是:"+
Balance+"时间期限是:"+Expiration+ "\r\n");
}
请各位高手指点怎么才能修改connection的错误