Hi
Error - The name objCnn does not exists in the current context.
protected void Page_Load(object sender, EventArgs e)
{
string objCnn = ConfigurationManager.ConnectionStrings["Cnn"].ConnectionString;
MyOwnVideos11();
}
protected void MyOwnVideos11()
{
using (SqlConnection conn = new SqlConnection(objCnn))
{
conn.Open();
foreach (var data in response.Data.items.OrderBy(x => x.snippet.publishedAt))
{
string videoId = data.id.videoId;
string title = data.snippet.title;
string description = data.snippet.description;
string query = @"
INSERT INTO Videos (VideoId, VideoTitle, Description)
VALUES (@VideoId, @VideoTitle, @Description, )";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("@VideoId", videoId);
cmd.Parameters.AddWithValue("@VideoTitle", title);
cmd.Parameters.AddWithValue("@Description", description);
cmd.ExecuteNonQuery();
}
}
}
}
Thanks
Emily FosterPosted Mar 9, 2025, 12:14 PM
Hello! It looks like the error "The name objCnn does not exist in the current context" is occurring because the variable `objCnn` is defined in the `Page_Load` method, and it's not accessible in the `MyOwnVideos11` method where you are trying to use it.
One way to resolve this issue is to declare `objCnn` at the class level so that it's accessible throughout the class. Here's how you can modify your code:
By declaring `objCnn` at the class level, it can now be accessed by both `Page_Load` and `MyOwnVideos11` methods, resolving the "objCnn does not exist in the current context" error. This way, the connection string is available wherever you need it within the class. Let me know if you have any more questions or need further assistance!