Table Name: Event
Event.event_id (primary key)
Event.venue_id
Event.artist_id
Event.date_time
Table Name: Venue
Venue.venue_id
Venue.name
Venue.city
Venue.state
I want to be able to display them but i get an error with the following code
SqlConnection conn = new SqlConnection(connect_string);
string query ="SELECT Event.date_time, Event.venue_id, Venue.city, Venue.name,Venue.state"
+ "FROM Event, Venue WHERE Event.artist_id=" +art_id + "AND Event.venue_id=Venue.venue_id ORDER BY Event.date_timeASC";
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
DataSet ds = new DataSet();
adp.Fill(ds,"Event"); //Error below falls on this line of code.
foreach (DataRow dr in ds.Tables["Event"].Rows)
{
date_label.Text += dr["Event.date_time"].ToString()+ "<br>";
}
How do i do this join and be able to output the information of the Event.date_time, Venue.city, Venue.name, Venue.state
I get the following error
The column prefix 'Event' does not match with a table name or alias name used in the query. The column prefix 'Event' does not match with a table name or alias name used in the query. The column prefix 'Venue' does not match with a table name or alias name used in the query. The column prefix 'Venue' does not match with a table name or alias name used in the query. The column prefix 'Venue' does not match with a table name or alias name used in the query. The column prefix 'Event' does not match with a table name or alias name used in the query. The column prefix 'Event' does not match with a table name or alias name used in the query. The column prefix 'Venue' does not match with a table name or alias name used in the query.
(1) It would look like"
SELECT Event.date_time, Event.venue_id, Venue.city, Venue.name,Venue.state"
+ "FROM Event INNER JOIN Venue ON Event.venue_id=Venue.venue_id WHERE Event.artist_id=@.artId ORDER BY Event.date_timeASC";
(2) Notice I have used @.artId.You should be using parameterized queriesto prevent SQL Injection attack ( a simple google search on this willgive you enough information).
|||Your string:
string query ="SELECT Event.date_time, Event.venue_id, Venue.city, Venue.name,Venue.state"
+ "FROM Event, Venue WHERE Event.artist_id=" +art_id + "AND Event.venue_id=Venue.venue_id ORDER BY Event.date_timeASC";
Should be:
string query ="SELECT Event.date_time, Event.venue_id, Venue.city, Venue.name,Venue.state "
+ "FROM Event, Venue WHERE Event.artist_id=" +art_id + "AND Event.venue_id=Venue.venue_id ORDER BY Event.date_timeASC";
(I added a space at the end of the first line. The "Venue.state" and "FROM" were concatenated)
|||Thanks for pointing that out scampbell12001.
No comments:
Post a Comment