0
string carid = Request.QueryString["carde"];
            var conString = ConfigurationManager.ConnectionStrings["CONSTRING"].ConnectionString;
            SqlConnection con = new SqlConnection(conString);
            con.Open();
            SqlCommand cmd1 = new SqlCommand("SELECT cars.*, img.img FROM cars INNER JOIN img ON cars.carid = img.carid where cars.carid= "+carid+"", con);
            SqlDataAdapter sda1 = new SqlDataAdapter(cmd1);
            DataTable dt = new DataTable();
            sda1.Fill(dt);
            cardetail.DataSource = dt;
            cardetail.DataBind();

I cant find anything wrong with this query. Can anyone help? Thanks in advance.

Codor
  • 17,447
  • 9
  • 29
  • 56
Samsam
  • 95
  • 2
  • 12

3 Answers3

0

You inner join has a table with another carid column name.

Please include table schemas if you want a more solid assessment.

Ya Wang
  • 1,758
  • 1
  • 19
  • 41
  • 1
    He's defining the columns by table names in the on-part. Cant be ambigious. Either this isnt the current code or the wildcard `*` in the select is raising it (dont think so). – C4d Nov 29 '16 at 13:00
0

I am not sure about the ambigous carid but you should use the parameters as below. Try this, and we'll see the rest.

SqlCommand cmd1 = new SqlCommand(
    "SELECT cars.*, img.img" +
    "FROM cars, img" +
    "WHERE cars.carid = img.carid AND cars.carid = @carid", con);

cmd1.Parameters.AddWithValue("@carid", carid);
Tolga Evcimen
  • 7,112
  • 11
  • 58
  • 91
  • 1
    `cars.* selectedCars` are you serious? How can multiple columns have a single name? Syntax error? – C4d Nov 29 '16 at 13:01
  • 1
    I am guessing the ambiguity is caused by that :) Hence the OP should do something about the selected names. But it's been a while I wasn't dealing with sql queries! :) admitted. I'll fix it in a min. – Tolga Evcimen Nov 29 '16 at 13:04
  • 1
    Think so too thats the only possibility. But you cant give a synonym for multiple selections. He would have to define each selection by itself. – C4d Nov 29 '16 at 13:05
0

Your wildcard * has to be the problem in here.

SELECT cars.*, img.img FROM cars INNER JOIN...

Instead of selecting everything from cars by wildcard, select them manually (which is better either way when coding). Using SELECT * when coding is a bad practice!

SELECT cars.col1, cars.col2, cars.carid, cars.., img.img FROM cars INNER JOIN...  

EDIT:

This is for sure the only possibility I can think of. However you havnt provided which database-system you are using. I just tried that on mssql and couldnt reproduce it.

Was too lazy to create tables for testing, so I went with some pseudo-tables

SELECT a.*, b.a FROM (SELECT 1 a, 2 b, 3 ab UNION SELECT 2, 2, 2) a
LEFT JOIN
(SELECT 1 a, 2 b, 3ab UNION SELECT 2, 2, 2) b
ON a.a = b.a

No error raised even with a.* where a and b have the same columns.

C4d
  • 3,183
  • 4
  • 29
  • 50
  • Thinking on the issue for a third time, the only case we can be correct on our guess is that the `cars` table has multiple columns with the same name. Is this even possible? – Tolga Evcimen Nov 29 '16 at 13:14
  • No its not. `cars.*` means `everything from the cars table`. It depends on how the database translates this. If it gets translated to `SELECT col1, col2, carid, img.img` the error raises because `carid` is presented in both tables. `cars.carId` and `cars.*` are very different things. – C4d Nov 29 '16 at 13:16
  • 1
    @TolgaEvcimen Check my edit. Reproducing not possible for me. He probably uses a strange database that cant handle this case. – C4d Nov 29 '16 at 13:21