嘿伙计们还在忙着我发现的注册表格文章,我按照他们的步骤插入了他发布的代码,但我似乎得到错误“在当前上下文中不存在”我做错了什么或是他的代码一个问题?
http://www.c-sharpcorner.com/uploadfile/rohatash/simple-user-login-in-Asp-Net-using-C-Sharp/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class StudentLogin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnRegister_Click(object sender, EventArgs e)
{
string strcon = "Data Source=.;uid=sa;pwd=Password$2;database=master";
SqlConnection con = new SqlConnection(strcon);
SqlCommand com = new SqlCommand("VC-Temps", con);
com.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("StudCode", TextBox3.Text);
SqlParameter p2 = new SqlParameter("Password", TextBox4.Text);
SqlParameter p3 = new SqlParameter("FirstName", TextBox5.Text);
SqlParameter p4 = new SqlParameter("LastName", TextBox6.Text);
SqlParameter p5 = new SqlParameter("Telephone", TextBox7.Text);
SqlParameter p6 = new SqlParameter("Course", TextBox8.Text);
SqlParameter p7 = new SqlParameter("Availability", DropDownList1.Text);
SqlParameter p8 = new SqlParameter("JobSkill", DropDownList2.Text);
SqlParameter p9 = new SqlParameter("Experience", DropDownList3.Text);
com.Parameters.Add(p1);
com.Parameters.Add(p2);
com.Parameters.Add(p3);
com.Parameters.Add(p4);
com.Parameters.Add(p5);
com.Parameters.Add(p6);
com.Parameters.Add(p7);
com.Parameters.Add(p8);
com.Parameters.Add(p9);
con.Open();
com.ExecuteNonQuery();
}
}
错误是:错误3当前上下文中不存在名称'CommandType'C:\ Website \ StudentLogin.aspx.cs 21 27 C:\ Website \
答案 0 :(得分:4)
您需要提供完整的命名空间或添加using System.Data.SqlClient
。
右键点击CommandType
并从Resolve
菜单中选择一项...
你也可以按 Ctrl + 。,而carret是在有问题的单词上。
编辑:需要参考System.Data
,请先检查。
答案 1 :(得分:0)
我收到此错误,因为我在using语句中有分号。我花了一段时间才看到它。
using (System.Data.SqlClient.SqlCommand cmd = new SqlCommand(sql, sqlConn)); <=no no
{
cmd.CommandType = CommandType.Text;
答案 2 :(得分:0)
通过明确包含以下内容以及using System.Data.SqlClient
来解决我的问题:
using System.Data;
原来using System.Data.SqlClient
还不够。