Question

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.

Was it helpful?

Solution 2

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

OTHER TIPS

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

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top