ASP.net:插入语句和gridview

时间:2014-04-16 04:31:17

标签: c# asp.net gridview code-behind sql-insert

我有一个错误,我无法理解我在做什么错误。在遇到问题之前,让我告诉你:我有四张桌子

Tbl1
+----+----------+-----------+----------+
| ID | NAME     | ADDRESS   | SALARY   |
+----+----------+-----------+----------+
| C1 | Ramesh   | Ahmedabad |  2000.00 |
| C2 | Khilan   | Delhi     |  1500.00 |
| C3 | kaushik  | Kota      |  2000.00 |
| C4 | Chaitali | Mumbai    |  6500.00 |
| C5 | Hardik   | Bhopal    |  8500.00 |
| C6 | Komal    | MP        |  4500.00 |
| C7 | Muffy    | Indore    | 10000.00 |
+----+----------+-----------+----------+

Tbl2
+----+----------+-----------+
| ID | NAME     | ADDRESS   |   
+----+----------+-----------+
| S1 | Roy      | Kolkota   |
| S2 | Praveen  | Delhi     |
| S3 | Madhuri  | Kota      |
| S4 | Hema     | Mumbai    |
| S5 | Anu      | Mumbai    |
| S6 | Khushi   | MP        |
| S7 | Anjali   | Indore    |
+----+----------+-----------+

Tbl3
+----+----------+-----------+
| ID | NAME     | ADDRESS   |   
+----+----------+-----------+
| P1 | Mouse    | Delhi     |
| P2 | Camera   | Delhi     |
| P3 | Pen Drive| Ahmedabad |
| P4 | RAM’s    | Mumbai    |
| P5 | Keyboard | Mumbai    |
| P6 | HDMI’s   | MP        |
| P7 | Hard Disk| Indore    |
+----+----------+-----------+   

Tbl4
+----+-------+-----------+-----------+
| ID | ID    | ID        | Total Cost|
+----+-------+-----------+-----------+
| C1 | S1    | P1        |    2000   |
| C1 | S1    | P3        |    1500   |
| C2 | S2    | P4        |    1200   |
| C2 | S3    | P2        |    1800   |
| C4 | S5    | P1        |    1000   |
| C5 | S6    | P5        |    900    |
| C6 | S7    | P6        |    190    |
| C7 | S6    | P4        |    1900   |
+----+-------+-----------+-----------+

问题1:我试图在tbl2中插入一个新行但是我遇到一个错误,它说列名不存在。

例如我添加了S8 Rakesh Banglore(这是在三个不同的文本框下)。它表示列名称S8,列名称Rakesh,列名称Banglore不存在。以下是我的代码。另外我应该添加什么来为重复数据提供消息?此外,如果我添加S11或其他东西它是在S1下面所以我该怎么做才能安排表格?

的web.config

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
<appSettings>

    <add key="SQLConn" value="Data Source=online database source;Initial Catalog=database name;Persist Security Info=True;User ID=username;Password=password" />
</appSettings>
    </configuration>

背后的代码

 protected void Button1_Click1(object sender, EventArgs e)
    {
        SqlConnection SqlConn = new SqlConnection();
        SqlCommand cmd;
        SqlConn.ConnectionString = ConfigurationManager.AppSettings["SqlConn"].ToString();
        SqlConn.Open();
        string query1 = "insert into tbl2(id,name,address) values (" + txt_id.Text + ",'" + txt_name.Text + "','" + txt_address.Text + "')";

        cmd = new SqlCommand(query1, SqlConn);
        cmd.ExecuteNonQuery();
        SqlConn.Close();
    }

我上面列出的错误是&#34; SqlConn.Open();&#34;。

问题2:

如果我有一个带有两个文本框名称txt_CID.txt和txt_SID.txt的Web表单。它在网格视图中显示tbl3行。我很困惑如何用后面的代码打印它。另外,我想学习如何在没有代码的情况下打印它。

例如:

所以,如果我输入C1 S1,结果应为

| P1 | Mouse    | Delhi     |
| P3 | Pen Drive| Ahmedabad |

应该在网格视图中显示结果。

3 个答案:

答案 0 :(得分:1)

问题1:

将您的查询更改为此类

protected void Button1_Click1(object sender, EventArgs e)
{
    SqlConnection SqlConn = new SqlConnection();
    SqlCommand cmd;
    SqlConn.ConnectionString = ConfigurationManager.AppSettings["SqlConn"].ToString();
    SqlConn.Open();
    string query1 = "insert into tbl2(id,name,address) values (@id,@name,@address)";
    cmd = new SqlCommand(query1, SqlConn);
    cmd.Parameters.AddWithValue("@id", txt_id.Text);
    cmd.Parameters.AddWithValue("@name", txt_name.Text);
    cmd.Parameters.AddWithValue("@address", txt_address.Text);
    cmd.ExecuteNonQuery();
    SqlConn.Close();
}

答案 1 :(得分:0)

问题1解决方案:

你已经打开了SqlConn.Open();两次出错,

删除其中一条陈述。

并删除代码中的以下行

    cmd.Parameters.AddWithValue("@id", txt_id.Text);
    cmd.Parameters.AddWithValue("@name", txt_name.Text);
    cmd.Parameters.AddWithValue("@address", txt_address.Text);

最后,

你错过了地址(txtaddress.Text)代码解决的单引号,

        SqlConnection SqlConn = new SqlConnection();
        SqlCommand cmd;
        SqlConn.ConnectionString = ConfigurationManager.AppSettings["SqlConn"].ToString();
        SqlConn.Open();
        string query1 = "insert into tbl2(id,name,address) values (" + txt_id.Text + ",'" + txt_name.Text + "','" + txt_address.Text + "')";
        cmd = new SqlCommand(query1, SqlConn);
        cmd.ExecuteNonQuery();
        SqlConn.Close();

答案 2 :(得分:0)

问题1:

不太确定,但在您将 txtAddress和txt_id 添加到查询中时,我认为您缺少单引号。

您的查询

string query1 = "insert into tbl2(id,name,address) values (" + txt_id.Text + ",'" + txt_name.Text + "'," + txt_address.Text + ")";

应改为

string query1 = "insert into tbl2(id,name,address) values ('" + txt_id.Text + "','" + txt_name.Text + "','" + txt_address.Text + "')";

同时删除cmd.Parameters.AddWithValue行。

问题2:

我认为您只需要加入Tbl3和Tbl4,因此查询将如下所示。

此外,我假设Tbl4中的3个ID是ID_1,ID_2和ID_3。

我没有对它进行测试,因此可能存在一些语法错误。

"SELECT T3.ID, T3.NAME, T3.ADDRESS
FROM TBL3 T3
INNER JOIN TBL4 T4
ON T3.ID = T4.ID_3
WHERE T4.ID_1 = '" +  txt_CID.txt + "' AND T4.ID_2 = '" + txtSID.txt + "'";

向gridview显示数据的代码


DataSet objDataSet = new DataSet();
SqlConnection objConn = new SqlConnection();

string query = "";//Your query here

SqlCommand objComm = new SqlCommand(query, objConn);

SqlDataAdapter objDataAdapter = new SqlDataAdapter(objComm);

if (objConn.State == ConnectionState.Closed)
{
    objConn.Open();
}

objDataAdapter.Fill(objDataSet, "YourTableName");

dataGridView1.DataSource = objDataSet;

希望这有帮助