这是导致错误的代码:
void FillGridView()
{
if (sqlCon.State == ConnectionState.Closed)
sqlCon.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCon);
sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
DataTable dtbl = new DataTable();
sqlDa.Fill(dtbl);
sqlCon.Close();
gvContact.DataSource = dtbl;
gvContact.DataBind();
}
这是连接数据库的全部代码。我从数据库的属性中获取了连接字符串。
public partial class Contact : System.Web.UI.Page
{
SqlConnection sqlCon = new SqlConnection(@"Data Source=ANILRANA-TOSH;Initial Catalog=cruddb;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
btnDelete.Enabled = false;
FillGridView();
}
}
protected void btnClear_Click(object sender, EventArgs e)
{
Clear();
}
public void Clear()
{
hfContactID.Value = "";
txtName.Text = txtMobile.Text = txtAddress.Text = "";
lblSuccessMessage.Text = lblErrorMessage.Text = "";
btnSave.Text = "Save";
btnDelete.Enabled = false;
}
protected void btnSave_Click(object sender, EventArgs e)
{
if (sqlCon.State == ConnectionState.Closed)
sqlCon.Open();
SqlCommand sqlCmd = new SqlCommand("ContactCreateOrUpdate", sqlCon);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue("@ContactID", (hfContactID.Value==""?0:Convert.ToInt32(hfContactID.Value)));
sqlCmd.Parameters.AddWithValue("@Name", txtName.Text.Trim());
sqlCmd.Parameters.AddWithValue("@Mobile",txtMobile.Text.Trim());
sqlCmd.Parameters.AddWithValue("@Address", txtAddress.Text.Trim());
sqlCmd.ExecuteNonQuery();
sqlCon.Close();
FillGridView();
string ContactID = hfContactID.Value;
Clear();
if (hfContactID.Value == "")
lblSuccessMessage.Text = "Saved Successfully";
else
lblSuccessMessage.Text = "Updated Successfully";
}
void FillGridView()
{
if (sqlCon.State == ConnectionState.Closed)
sqlCon.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCon);
sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
DataTable dtbl = new DataTable();
sqlDa.Fill(dtbl);
sqlCon.Close();
gvContact.DataSource = dtbl;
gvContact.DataBind();
}
protected void lnk_OnClick(object sender, EventArgs e)
{
int ContactID = Convert.ToInt32((sender as LinkButton).CommandArgument);
if (sqlCon.State == ConnectionState.Closed)
sqlCon.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter("ContactViewByID", sqlCon);
sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
sqlDa.SelectCommand.Parameters.AddWithValue("@ContactID", ContactID);
DataTable dtbl = new DataTable();
sqlDa.Fill(dtbl);
sqlCon.Close();
hfContactID.Value = ContactID.ToString();
txtName.Text = dtbl.Rows[0]["Name"].ToString();
txtMobile.Text = dtbl.Rows[0]["Mobile"].ToString();
txtAddress.Text = dtbl.Rows[0]["Address"].ToString();
btnSave.Text = "Update";
btnDelete.Enabled = true;
}
protected void btnDelete_Click(object sender, EventArgs e)
{
if (sqlCon.State == ConnectionState.Closed)
sqlCon.Open();
SqlCommand sqlCmd = new SqlCommand("ContactDeleteByID", sqlCon);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue("@ContactID", Convert.ToInt32(hfContactID.Value));
sqlCmd.ExecuteNonQuery();
sqlCon.Close();
Clear();
FillGridView();
lblSuccessMessage.Text = "Deleted Successfully";
}
}
答案 0 :(得分:0)
您永远都不会指定要调用什么存储过程来填充数据集。
更改这些行:
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCon);
sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
到
SqlDataAdapter sqlDa = new SqlDataAdapter("dbo.YourStoredProcedureName", sqlCon);
sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
,然后您还将为使用字符串(要执行的命令)的SqlDataAdapter
和SqlConnection
调用正确构造函数。