-
February 1st, 2011, 07:51 AM
#1
Bing API and MySQL
Hi,
I am trying to use the Bing API to retrieve the first image on whatever search query I enter and then strore the url of the image in MySQL. I am nearly there with it but I am getting an error which I am unable to fix.
Code:
BingService service = new BingService();
SearchRequest request = new SearchRequest();
request.AppId = "MYAPPID";
request.Query = filePaths[i] + " dvd";
request.Sources = new SourceType[] { SourceType.Image };
SearchResponse response = service.Search(request);
OdbcCom = new OdbcCommand("INSERT INTO movies(db_image,db_title,db_quality) values (?,?,?)", OdbcCon);
OdbcCom.Parameters.Add("@db_image", OdbcType.Text).Value = response.Image.Results[0].MediaUrl;
OdbcCom.Parameters.Add("@db_title", OdbcType.Text).Value = filePaths[i]; OdbcCom.Parameters.Add("@db_quality", OdbcType.Int).Value = 0;
OdbcCom.ExecuteNonQuery();
The above code is run about 400 times through all the movie titles.
Although I keep getting the error on the bold line above:
Code:
Object reference not set to an instance of an object.
I am still quite new to C# but I am thinking that it is running through the movies so quickly that the Bing API is unable to keep up with getting the images? Someone can tell me if I am way off base..
Can someone point me in the right direction?
I tested to see if the response was null by doing the following:
Code:
if (response.Image.Results[0].MediaUrl == null)
{
Console.Writeline("Null");
}
else
{
OdbcCom.Parameters.Add("@db_image", OdbcType.Text).Value = response.Image.Results[0].MediaUrl;
}
It still throws the error. Maybe I am doing it wrong?
Thanks
Jay
-
February 1st, 2011, 08:36 AM
#2
Re: Bing API and MySQL
Jay,
That is a good first stab. But you need to find out what is null. The following line...
Code:
SearchResponse response = service.Search(request);
What does service.Search return. Does it Always return a valid response or could it return null.
If it always returns something, will it always have an image.
So the thing that is returning null could be.
response OR
response.Image
I would like to think if you had an image you would always have a mediaURL.
Hope this helps.
-
February 1st, 2011, 09:32 AM
#3
Re: Bing API and MySQL
your first and major problem is that you're using bing instead of a real search engine.
your second problem (and where the exception is being thrown) is: you need more checks in there.
check response for null, check response.Image for null, check response.Image.Results for null, check response.Image.Results.Length > 0, check response.Image.Results[0].MediaUrl for null...
when you go blindly using deep level objects like that you're bound to get an object reference error.
Code:
BingService service = new BingService();
SearchRequest request = new SearchRequest();
request.AppId = "MYAPPID";
request.Query = filePaths[i] + " dvd";
request.Sources = new SourceType[] { SourceType.Image };
SearchResponse response = service.Search(request);
if( response != null &&
response.Image != null &&
response.Image.Results != null &&
response.Image.Results.Length > 0 &&
!string.IsNullOrEmpty(response.Image.Results[0].MediaUrl) ) {
OdbcCom = new OdbcCommand("INSERT INTO movies(db_image,db_title,db_quality) values (?,?,?)", OdbcCon);
OdbcCom.Parameters.Add("@db_image", OdbcType.Text).Value = response.Image.Results[0].MediaUrl;
OdbcCom.Parameters.Add("@db_title", OdbcType.Text).Value = filePaths[i];
OdbcCom.Parameters.Add("@db_quality", OdbcType.Int).Value = 0;
OdbcCom.ExecuteNonQuery();
}
also, I would use mysql's native provider rather than an odbc connection, but that's just me...
Last edited by MadHatter; February 1st, 2011 at 09:35 AM.
-
February 1st, 2011, 03:23 PM
#4
Re: Bing API and MySQL
Thanks for the help MadHatter, your code has worked :-)
Whats the difference between odbc and mysql connection?
Jay
-
February 1st, 2011, 09:14 PM
#5
Re: Bing API and MySQL
mysql connector uses mysql's native protocol instead of the non-native odbc.
Last edited by MadHatter; February 1st, 2011 at 09:40 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|