0

I have an error which I am not able to understand what mistake I am doing. Before going to problems, let me tell you: I have four tables which is

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   |
+----+-------+-----------+-----------+

Problem 1: I was trying to insert a new row in the tbl2 but I am having an error it says that column name does not exist.

For instance I have added S8 Rakesh Banglore (this is under three different text boxes). It says column name S8, column name Rakesh, column name Banglore does not exist. Below is my code. Also what should I add to give message for duplicate data? Also if I add S11 or something it comes under S1 so what should I do to arrange the table?

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>

code behind

 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();
    }

I am having a error listed above with line "SqlConn.Open();".

Problem 2:

If I have a web form with a two text box name txt_CID.txt and txt_SID.txt. It shows tbl3 row in a grid view. I am confused how do I print this with code behind. Also I would like to learn how can I print it without code behind.

For example:

So, If I enter C1 S1 the result should be

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

which should show me the result in grid view.

1
  • I have removed "AddWithValue" and updated what you advised in my program Now error is - An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code Additional information: Invalid column name 'S8'.
    – user3338497
    Apr 16, 2014 at 5:18

3 Answers 3

1

Problem 1 :

Change your query to something like this

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();
}
6
  • I do know that but to get the values from the all text box it should be 'string query1 = "insert into tbl2(id,name,address) values (" + txt_id.Text + ",'" + txt_name.Text + "'," + txt_address.Text + ")";'
    – user3338497
    Apr 16, 2014 at 4:50
  • No, I am afraid you are wrong. You have already assigned the value of "txt_id" to "@id" and "txt_name" to "@name" and so on.. Apr 16, 2014 at 4:52
  • An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code Additional information: Must declare the scalar variable "@id".
    – user3338497
    Apr 16, 2014 at 5:07
  • Please see the above code for more clarification. Added complete code. Apr 16, 2014 at 5:41
  • Now It worked for problem 1. Now I am concentrating on problem 2.
    – user3338497
    Apr 16, 2014 at 6:06
0

Problem 1:

Not very sure but I think you are missing single quotes when you are adding txtAddress and txt_id to the query.

Your query

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

Should be changed to

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

Also remove cmd.Parameters.AddWithValue lines.

Problem 2:

I think you just need to join Tbl3 and Tbl4 so the query will be something like the following.

Also I have assumed that the 3 ids in Tbl4 are ID_1, ID_2 and ID_3.

I have not tested it so there might be some syntax errors.

"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 + "'";

Code to show data to 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;

Hope this helps

16
  • Thank you for that I will try and let you know.
    – user3338497
    Apr 16, 2014 at 5:11
  • Doing my problem 2 but I have question for this how would I connect this to grid view to show the result?
    – user3338497
    Apr 16, 2014 at 5:19
  • Check my updated answer regarding showing result in gridview. As for your error, I think you are missing single quotes in query where you are appending txt_id.txt in your query. You need to have single quotes in place where you are appending txt_id and txtaddress both. Note the bold text in my answer.
    – samar
    Apr 16, 2014 at 5:46
  • string query ="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 + "'"; right?
    – user3338497
    Apr 16, 2014 at 6:03
  • Yes this should be the query to fetch records for the gridview.
    – samar
    Apr 16, 2014 at 6:13
0

Problem 1 Solution :

You had open SqlConn.Open(); two times it makes an error,

Remove one of the statement.

And Remove the below lines in your code

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

Finally,

You had missed out the single quote for address(txtaddress.Text) code solved,

        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
  • After getting your advise I did remove my "SqlConn.Open();" but still it gives me that error. Also I have updated my post related to "SqlConn.Open();"
    – user3338497
    Apr 16, 2014 at 4:40
  • Sure I will upload it in a min
    – user3338497
    Apr 16, 2014 at 4:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.