【问题标题】:What is the wrong with my c# code我的 C# 代码有什么问题
【发布时间】:2015-04-14 17:44:44
【问题描述】:

这是错误

“/Kibritak3”应用程序中的服务器错误。

列名“名称”无效。 列名“电话”无效。

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.Data.SqlClient.SqlException:列名“名称”无效。 列名“电话”无效。

来源错误:

在当前网络请求的执行过程中产生了一个未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。

堆栈跟踪:

its just come if i write this code
 **

if (Literal1.Text == "Data inserted successfully")
        {
            Response.Redirect("http://localhost:1038/Kibritak3/CompanyHomePage.aspx");
        }
        else {
            txtName.Text = "";
            txtEmail.Text = "";
            txtPhone.Text = "";
        }

**
and without it evrything is ok
-------------------------
full code:
    public void refress()
    {
        txtName.Text = "";
        txtEmail.Text = "";
        txtPhone.Text = "";
        txtLocation.Text = "";
        txtHistory.Text = "";
    }




    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
         SqlCommand cmd = new SqlCommand("insert into Com_INFO(Name,Email,Phone,Location,History) values('" + txtName.Text + "','" + txtEmail.Text + "','" +txtPhone.Text+ "','" +txtLocation.Text+ "','"+txtHistory.Text+"')", con);

        cmd.CommandType = CommandType.Text;

        try
        {

            con.Open();

            cmd.ExecuteNonQuery();

            Literal1.Text = "Data inserted successfully";

            con.Close();

            refress();

        }

        catch (Exception ex)
        {

            Literal1.Text = ex.Message;

        }
        if (Literal1.Text == "Data inserted successfully")
        {
            Response.Redirect("http://localhost:1038/Kibritak3/CompanyHomePage.aspx");
        }
        else {

            txtName.Text = "";
            txtEmail.Text = "";
            txtPhone.Text = "";
        }
    }
}

【问题讨论】:

  • 这不是堆栈跟踪。
  • 您的数据库表没有您认为的列。此外,为了理智,学习使用参数……尤其是在 Web 应用程序中! stackoverflow.com/q/17509169/327083
  • 没有看到您在数据库中访问的表的结构,除了您的数据库列名与您引用的名称不匹配之外,我们没有什么可以说的在您的代码中,这正是错误消息告诉您的内容。此外,J... 是正确的。您的问题中显示的代码应该永远投入生产。很危险。
  • 亲爱的 Sani ...我有一个列,我说在没有 if 语句的情况下数据保存良好
  • 在Literal1.Text = "Data inserted successfully"; 行放置一个断点,如果断点真的被命中,请告诉我们。

标签: c#


【解决方案1】:

请验证您的数据库的属性一次。 要么 不要在查询中包含表的属性。

SqlCommand cmd = new SqlCommand("插入 Com_INFO 值('" + txtName.Text + "','" + txtEmail.Text + "','" +txtPhone.Text+ "','" +txtLocation.Text+ "','"+txtHistory.Text+"')", con);

【讨论】:

  • 我们如何预测表格将只有这些列。可能还有更多
【解决方案2】:

正如错误消息(和其他贡献者)指出的那样,您的数据库中没有“姓名”和“电话”列。另外,作为旁注,永远不要在代码中编写实际的 sql 查询。将其写入存储过程,然后从代码中调用存储过程。

【讨论】:

    【解决方案3】:

    您能再次检查您的数据库吗?我认为您刚刚在数据库中添加了这些列,您需要做的就是刷新数据库。当您在程序中处理 DML 命令时,请使用参数来保护您的数据/数据库。

    【讨论】:

      猜你喜欢
      • 2017-05-02
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      相关资源
      最近更新 更多