StaleElementReferenceException thrown ONLY in the pipeline - c#

I have a test running in our azure pipeline that fails every time it is run(it turns out it's about half the time), with a StaleElementReferenceException. Whenever the test is run locally (headless or otherwise) the test will pass.
Interestingly, it's failing while running a method that is used in multiple other tests that will not hit this issue.
I have a callstack, but I'm really not sure how to go about debugging this one! Any advice please?!
at OpenQA.Selenium.WebDriver.UnpackAndThrowOnError(Response errorResponse, String commandToExecute)
at OpenQA.Selenium.WebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.WebDriver.InternalExecute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.WebElement.Execute(String commandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.WebElement.get_Displayed()
at SeleniumExtras.WaitHelpers.ExpectedConditions.<>c__DisplayClass6_0.<ElementToBeClickable>b__0(IWebDriver driver)
at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition, CancellationToken token)
at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition)
at GembaCloud.Tests.Pages.BasePage.BasePageActions.WaitForElementToBeClickable(By element) in C:\agent\_work\53\s\GembaCloud.Tests\Pages\BasePage.cs:line 205
at GembaCloud.Tests.Pages.BasePage.BasePageActions.GetTable(By byTable, Int32 firstDataRowIndex) in C:\agent\_work\53\s\GembaCloud.Tests\Pages\BasePage.cs:line 259
at GembaCloud.Tests.Pages.GembaIntelligencePage.GembaIntelligencePageActions.GetGembaIntelligenceTable() in C:\agent\_work\53\s\GembaCloud.Tests\Pages\Home\GembaIntelligencePage.cs:line 52
at GembaCloud.Tests.Pages.GembaIntelligencePage.GembaIntelligencePageActions.ExpandGembaIntelligenceTableRow(Int32 rowNumber) in C:\agent\_work\53\s\GembaCloud.Tests\Pages\Home\GembaIntelligencePage.cs:line 79
at GembaCloud.Tests.TestClasses.UserRolePageElementAuthorisationTests.reporting_user_should_not_have_access_to_intelligence_dropdown_create_action_button() in C:\agent\_work\53\s\GembaCloud.Tests\TestClasses\UserRolePageElementAuthorisationTests.cs:line 115
Is there something obviously wrong here? Thanks!
Here's the code of the items in the callstack. GembaIntelligencePage class first:
public void ExpandGembaIntelligenceTableRow(int rowNumber)
{
this.ExpandTableRow(this.GetGembaIntelligenceTable(), rowNumber);
}
public IList<IWebElement> GetGembaIntelligenceTable()
{
return this.GetTable(_elements.byGembaIntelligenceTable, 1);
}
BasePage class second:
protected IList<IWebElement> GetTable(By byTable, int firstDataRowIndex)
{
//firstDataRowIndex is so we only return the table rows that we need
this.WaitForElementToBeClickable(byTable);
IWebElement table = _driver.FindElement(byTable);
IList<IWebElement> listOfDataTableRows = table.FindElements(By.CssSelector("tr")).Skip(firstDataRowIndex -1).ToList();
return listOfDataTableRows;
}
protected void WaitForElementToBeClickable(By element)
{
WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementToBeClickable(element));
}

Change the Timeout value to a higher value (for example from 10 change it to 30) and tell us if it works:
WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(30));

Related

Selenium throws timeout after locating the element

Has anyone experienced the problem when the webdriver locates the needed element, types the text into it, and then throws WebDriverTimeout exception saying that it takes too much time to find the exact same element that it has just sent text into?
If I wrap this block of code in try-catch which catches the timeout exception, the test goes on successfully, but it doesn't seem to be the healthy way of doing this.
I am using the c# selenium and chromedriver 2.44
Update:
The initial wait configuration and the action itself:
var driver = new ChromeDriver();
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(20);
driver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(3);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2);
...
var searchLocator = By.Id("searchByName");
driver.GetElement(searchLocator, 20, element => element.Displayed).SendKeys("test");
GetElement extension:
public static IWebElement GetElement(this IWebDriver driver, By locator, double timeout, Func<IWebElement, bool> condition)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout));
wait.IgnoreExceptionTypes(typeof(StaleElementReferenceException));
wait.Until(drv =>
{
var element = driver.FindElement(locator);
return condition(element);
});
return driver.FindElement(locator);
}
StackTrace:
Result StackTrace:
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebElement.Execute(String commandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebElement.SendKeys(String text)
at Tests.Steps.SearchSteps.WhenTheUserFillsTheSearchFieldWith(String searchFieldName, String data) in C:\...Tests\Steps\SearchSteps.cs:line 64
at lambda_method(Closure , IContextManager , String , String )
at TechTalk.SpecFlow.Bindings.BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, TimeSpan& duration)
at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.ExecuteStepMatch(BindingMatch match, Object[] arguments)
at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.ExecuteStep(IContextManager contextManager, StepInstance stepInstance)
at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnAfterLastStep()
at TechTalk.SpecFlow.TestRunner.CollectScenarioErrors()
at Tests.Features.SearchFeature.ScenarioCleanup()
at Tests.Features.SearchFeature.CheckSearch() in C:\...Tests\Features\Search.feature:line 167
Result Message:
OpenQA.Selenium.WebDriverTimeoutException : timeout
(Session info: chrome=70.0.3538.110)
(Driver info: chromedriver=2.44.609538
(b655c5a60b0b544917107a59d4153d4bf78e1b90),platform=Windows NT 6.3.9600 x86_64)
You should be able to set various timeout spans using the manage method on the driver. One of these should do the trick:
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(30);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
Also you are able to specify timeout in the ctor like:
var driver = new ChromeDriver (pathToDriver, TimeSpan.FromSeconds (30))
In the constructor case, the timeout is the command timeout that . Not sure which one is appropriate for you. Probably what you need is the implicitwait tho.

Selenium C# waiting for page to load - UnexpectedJavaScriptError

I am using selenium, IEDriver, and C# and I would like to wait for the page to load. I have this code:
/// <summary>
/// Ceka dokud neni stranka nastena
/// </summary>
public static void WaitForPageToLoad()
{
try
{
Thread.Sleep(500);
Log.Trace("Browser.WaitForPageToLoad() - Ceka dokud neni stranka nactena ...");
new WebDriverWait(Browser.Driver, new TimeSpan(0, 0, 360)).Until(d => ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState").Equals("complete"));
Thread.Sleep(1000);
}
catch (Exception ex)
{
Log.Error(ex);
throw;
}
}
but it will crash on this:
2018-01-04 15:39:27.2266 - ERROR: System.InvalidOperationException: JavaScript error (UnexpectedJavaScriptError)
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScriptCommand(String script, String commandName, Object[] args)
at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScript(String script, Object[] args)
at BaseFramework.Browser.<>c.<WaitForPageToLoad>b__10_0(IWebDriver d) in C:\TFS\PRIVPMT\Selenium\BaseFramework\Browser.cs:line 97
at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition)
at BaseFramework.Browser.WaitForPageToLoad() in C:\TFS\PRIVPMT\Selenium\BaseFramework\Browser.cs:line 97
EXCEPTION: System.InvalidOperationException: JavaScript error (UnexpectedJavaScriptError)
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScriptCommand(String script, String commandName, Object[] args)
at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScript(String script, Object[] args)
at BaseFramework.Browser.<>c.<WaitForPageToLoad>b__10_0(IWebDriver d) in C:\TFS\PRIVPMT\Selenium\BaseFramework\Browser.cs:line 97
at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition)
at BaseFramework.Browser.WaitForPageToLoad() in C:\TFS\PRIVPMT\Selenium\BaseFramework\Browser.cs:line 103
at Gamma.Tests.GammaICRM.AT82688_ICRM_SMOKE() in C:\TFS\PRIVPMT\Selenium\Gamma.UI.Tests\Gamma.Tests\GammaICRM.cs:line 72
at Gamma.Tests.GammaICRM.AT82688_ICRM_SMOKE_PerformTest() in C:\TFS\PRIVPMT\Selenium\Gamma.UI.Tests\Gamma.Tests\GammaICRM.cs:line 23
most of the time it works, but from time to time it will crash on this
next method is this:
public static void LeftClick(this IWebElement element)
{
//pockame dokud nelze na element kliknout
new WebDriverWait(Browser.Driver, new TimeSpan(0, 0, 120)).Until(ExpectedConditions.ElementToBeClickable(element));
Actions actions = new Actions(Browser.Driver);
//posuneme cursor na element
actions.MoveToElement(element).Perform();
//klikeneme na element
actions.Click().Build().Perform();
}
passed webElement is founded by XPath (webElement is always correct)
You could try calling the WebDriverWait class. You can use this to wait for an element to show up on the page (i.e the page loads if this object exists)
public static IWebElement WaitUntilElementExists(By elementLocator, int timeout = 10)
{
try
{
var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(timeout));
return wait.Until(ExpectedConditions.ElementExists(elementLocator));
}
catch (NoSuchElementException)
{
Console.WriteLine("Element with locator: '" + elementLocator + "' was not found in current context page.");
throw;
}
}
In this code the method returns wait.until then the elementexists condition on an element that is supposed to be on the page. Just have it try to find an element on the page which is supposed to be there when the page loads. If it doesn't exist for a certain amount of time it will give the NoSuchElementException (so a timeout). Catch this and give your own output
source: How to get webDriver to wait for page to load (C# Selenium project)
hope this helps at all
I've faced the same issue and the problem was that I was inside of a frame. Switching back to default content before executing this javascript resolved the error.
Hope it'll help if the problem is still valid for you.
In one word, just before calling WaitForPageToLoad() I've used _driver.SwitchTo().DefaultContent();

Invalid Operation Exception in C# Unit Test using Protractor

I have a simple test class like this:
public class MyTest
{
const string URL = "https://example.com/content/mypage.aspx";
IWebDriver driver;
NgWebDriver ngDriver;
[SetUp]
public void Setup()
{
driver = new ChromeDriver();
driver.Manage().Timeouts().SetScriptTimeout(TimeSpan.FromSeconds(10));
ngDriver = new NgWebDriver(driver);
}
[TearDown]
public void Teardown()
{
ngDriver.Quit();
}
[Test]
public void Basic()
{
ngDriver.Url = URL;
Assert.IsTrue(ngDriver.FindElement(By.CssSelector("#my")).Displayed);
}
}
and here's the HTML snippet:
<kendo-button id="my" ng-click="myCtrl.doSomething()">Do Something</kendo-button>
I'm getting the following error on the Assert.IsTrue line:
javascript error: [ng:test] http://errors.angularjs.org/1.3.15/ng/test
JavaScript stack:
Error: [ng:test] http://errors.angularjs.org/1.3.15/ng/test
at Error (native)
at https://example.com/AngularJS/1.3.15/angular.min.js:6:417
at Object.Ld [as getTestability] (https://example.com/AngularJS/1.3.15/angular.min.js:18:468)
at eval (eval at executeAsyncScript (unknown source), <anonymous>:10:13)
at eval (eval at executeAsyncScript (unknown source), <anonymous>:18:5)
at executeAsyncScript (<anonymous>:329:26)
at <anonymous>:345:29
at callFunction (<anonymous>:237:33)
at <anonymous>:247:23
at <anonymous>:248:3
(Session info: chrome=49.0.2623.87)
(Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Windows NT 6.1 SP1 x86_64) (UnexpectedJavaScriptError)
and the stack trace is:
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScriptCommand(String script, String commandName, Object[] args)
at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteAsyncScript(String script, Object[] args)
at Protractor.NgWebDriver.WaitForAngular() in c:\Users\Bruno\Projets\GitHub\bbaia\protractor-net\src\Protractor\NgWebDriver.cs:line 315
at Protractor.NgWebDriver.FindElement(By by) in c:\Users\Bruno\Projets\GitHub\bbaia\protractor-net\src\Protractor\NgWebDriver.cs:line 262
I only got the Protractor and Selenium WebDriver Nuget package. Is there something else I need to install or this is actually a code problem?
You're not actually navigating to the URL before the Assert?
Try -
[Test]
public void Basic()
{
NgDriver.Navigate().GoToUrl(URL);
Assert.IsTrue(ngDriver.FindElement(By.CssSelector("#my")).Displayed);
}
Building on what I shared below, I found that my problem was not specifying the root to the document. In my tag, I had
data-ng-app='myApp'
The code to create my protractor driver was:
ngDriver = new NgWebDriver(driver, "[ng-data='myApp']"
Protractor-net doesn't modify this direct CSS search criteria, so what worked was:
ngDriver = new NgWebDriver(driver, "[data-ng-app='myApp']"
Not an answer, but I don't have the ability to comment.
I'm seeing the same issue. Further investigation reveals that there is an exception being thrown when the NgWebDriver is instantiated. Inspecting the object shows the Location, PageSource, Title and Url members of the object created all "threw an exception of type 'System.InvalidOperationException' string {System.InvalidOperationException}". The exception only gets thrown up to the test when attempting to find an element.
My code looks like:
public DefaultPOM(IWebDriver webDriver, string baseURL)
{
driver = webDriver;
this.baseURL = baseURL;
driver.Manage().Timeouts().SetScriptTimeout(TimeSpan.FromSeconds(10));
driver.Navigate().GoToUrl(baseURL);
ngDriver = new NgWebDriver(driver, "[ng-app='myApp']");
ngDriver.Manage().Window.Maximize();
ngDriver.Navigate().GoToUrl(baseURL);
}
The inspecting the ngDriver object right after it is created shows the exceptions.
If I turn off synchronization before navigating, the only member of the driver object recording the exception is the Location.
ngDriver = new NgWebDriver(driver, "[ng-app='NCTWebPortal']");
ngDriver.IgnoreSynchronization=true;
ngDriver.Manage().Window.Maximize();
ngDriver.Navigate().GoToUrl(baseURL);
ngDriver.IgnoreSynchronization = false;

Using wait.until throws System.InvalidOperationException: No session ID specified

I have many test cases, where I need to wait until the page is loaded. I'm using explicit wait as the load times vary.
WebDriverWait _wait = new WebDriverWait(Drivers._driverInstance, new TimeSpan(0, 2, 0));
_wait.Until(D => D.Title);
It throws No session ID exception. I have a table that takes time to load in the page, so I tried using
_wait.Until(ExpectedConditions.ElementIsVisible(By.TagName("table")));
Even this throws the same error. The tests pass when I run each one individually and running all of them as suite raises this exception.
private static void InitialUpload(string filename)
{
SDDirectPage.filePath = filename;
SDDirectPage.filename = Path.GetFileNameWithoutExtension(SDDirectPage.filePath);
SDDirectPage.UploadButton.Click();
Drivers._driverInstance.SwitchTo();
SDDirectPage.FileReference = SDDirectPage.filename;
SDDirectPage.UploadTheFile();
//Check whether 404 occurred or the uploading file is smooth
if (Drivers._driverInstance.Title == "404 - File or directory not found.") //A bug at the moment, it uploads corrupted files most of the times.
{
Assert.Fail("404 error occurred. File might be corrupted or file mightnot be in the specified location..!");
return;
}
else
{
Drivers._driverInstance.SwitchTo().ParentFrame();
// SDDirectPage._wait.Until(D => D.Title);
SDDirectPage._wait.Until(ExpectedConditions.ElementIsVisible(By.TagName("table")));
Assert.AreEqual(SDDirectPage.filename + " - SmartDebit Front End Portal", Drivers._driverInstance.Title);
}
}
This is the function where I'm getting the exception. In some test cases, I have Assert.AreEqual, where I compare the titles. In those test cases, driver.Title raises the same error.
Here is the exception:
Test FullName: SDTestAutomation.SDDirectPage_Tests.FixInvalidRows_Search
Test Source: c:\Git\AutomationTest\automationtest\AutomationTest\SDTestAutomation\SDDirectPage_Tests.cs : line 249
Test Outcome: Failed
Test Duration: 0:02:19.2199494
Result Message:
Test method SDTestAutomation.SDDirectPage_Tests.FixInvalidRows_Search threw exception:
System.InvalidOperationException: No session ID specified
Result StackTrace:
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByTagName(String tagName)
at OpenQA.Selenium.By.<>c__DisplayClass1a.<TagName>b__18(ISearchContext context)
at OpenQA.Selenium.By.FindElement(ISearchContext context)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
at OpenQA.Selenium.Support.UI.ExpectedConditions.<>c__DisplayClass13.<ElementIsVisible>b__12(IWebDriver driver)
at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition)
at SDTestAutomation.SDDirectPage_Tests.InitialUpload(String filename) in c:\Git\AutomationTest\automationtest\AutomationTest\SDTestAutomation\SDDirectPage_Tests.cs:line 483
at SDTestAutomation.SDDirectPage_Tests.FixInvalidRows_Search() in c:\Git\AutomationTest\automationtest\AutomationTest\SDTestAutomation\SDDirectPage_Tests.cs:line 250
Here is the drivers class:
public class Drivers
{
static string path = #"C:\SmartDebit\SmartDebitTestAutomation\SmartDebitFramework\DriverResources\";
public static IWebDriver _driverInstance { get; set; }
public static void Initialize(string browser)
{
if (browser == "FF")
{
_driverInstance = new FirefoxDriver();
_driverInstance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5000));
}
if (browser == "IE")
{
_driverInstance = new InternetExplorerDriver(path);
_driverInstance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5000));
}
if (browser == "Chrome")
{
_driverInstance = new ChromeDriver(path);
_driverInstance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5000));
}
}
}
Code for initialising the browser instance:
[ClassInitialize]
public static void BrowserInstance(TestContext t)
{
loginPage = new LoginPage();
loginPage.Init("FF");
loginPage.Goto("url of the application");
Assert.AreEqual("Login Page", Drivers._driverInstance.Title, "Login page titles doesn't match");
}
[TestInitialize]
public void Init()
{
try
{
Login();
}
catch (Exception ex)
{
Console.WriteLine("Exception:" + ex);
Assert.Fail("ValidLogin() test failed in HomePage_Tests.cs");
loginPage.QuitBrowser();
}
}
private static void Login()
{
loginPage.LoginName = "username";
loginPage.Password = "password";
loginPage.LoginButton();
SDDirectPage._wait.Until(d=>Drivers._driverInstance.Title);
Assert.AreEqual("Home Page - Front End Portal", Drivers._driverInstance.Title, "Home page title doesn't match");
Assert.IsTrue(HomePage.loggedInUserText.Contains("username"));
}
I'm using Firefox 43.0.2
Could someone help be to overcome this situation.
Thanks.
System.InvalidOperationException: No session ID specified occurs when you don't have valid driver instance in the method.
Please check Drivers._driverInstance is properly instantiated in your code. If possible post the Drivers class' relevant portions in the question.
Refer another SO question where the same problem is discussed: Disabling browser javascript with Selenium webdriver + specflow + c# + Pageobject + pagefactory
I am basically from Java, but observing the code and exception, i am excepting WebDriverWait is defined in SDDirectPage and calling here.right?
SDDirectPage._wait.Until(ExpectedConditions.ElementIsVisible(By.TagName("table")));
When you are calling defined wait method, you are not passing webdriver instance here. So i am expecting on switching to this method driver loosing the session. For sake of confirm, can you try executing the same by commenting this wait line (and my use something equivalent to Thread.sleep(5000) in Java for once to check this issue)
Thanks

Why does LINQ to SQL think think that my new object is null when I try to add it to the database?

I am trying to add a record to my database table using LINQ to SQL and ASP.NET MVC 2.
The snippet in my controller that populates the LINQ object is this:
/* other code removed */
if (ModelState.IsValid)
{
var stream = new Genesis.Domain.Entities.Stream();
// Handle stream
// Is this stream new?
if (form.StreamID == 0)
{
// create new stream
stream.StreamUrl = form.StreamUrl;
stream.StreamName = form.StreamName;
stream.StreamBody = form.StreamBody;
stream.StreamTitle = form.StreamTitle;
stream.StreamKeywords = form.StreamKeywords;
stream.StreamDescription = form.StreamDescription;
form.StreamID = genesisRepository.CreateStream(stream); // CreateStream() returns ID as long
}
/* other code removed */
The genesisRepository.CreateStream() looks like this:
public partial class SqlGenesisRepository : IGenesisRepository
{
public long CreateStream(Stream stream)
{
streamTable.InsertOnSubmit(stream);
streamTable.Context.SubmitChanges();
return stream.StreamID;
}
}
When genesisRepository.CreateStream() gets executed, I get this error:
Updated to more accurate error and stacktrace
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 13: public long CreateStream(Stream stream)
Line 14: {
Line 15: streamTable.InsertOnSubmit(stream);
Line 16: streamTable.Context.SubmitChanges();
Line 17: return stream.StreamID;
Source File: C:\path\to\SqlGenesisRepositoryStreamPartial.cs Line: 15
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Data.Linq.Mapping.EntitySetDefSourceAccessor`2.GetValue(T instance) +18
System.Data.Linq.Mapping.MetaAccessor`2.GetBoxedValue(Object instance) +47
System.Data.Linq.StandardTrackedObject.HasDeferredLoader(MetaDataMember deferredMember) +106
System.Data.Linq.StandardTrackedObject.get_HasDeferredLoaders() +107
System.Data.Linq.StandardChangeTracker.Track(MetaType mt, Object obj, Dictionary`2 visited, Boolean recurse, Int32 level) +175
System.Data.Linq.StandardChangeTracker.Track(Object obj, Boolean recurse) +83
System.Data.Linq.StandardChangeTracker.Track(Object obj) +12
System.Data.Linq.Table`1.InsertOnSubmit(TEntity entity) +183
Genesis.Domain.Concrete.SqlGenesisRepository.CreateStream(Stream stream) in C:\Documents and Settings\bquakkelaar\Desktop\dropstuff\asp.net mvc\Genesis.0.02\Genesis.Domain\Concrete\SqlGenesisRepositoryStreamPartial.cs:15
Genesis_0_02.Controllers.AdminStreamController.StreamEdit(StreamEditModel form) in C:\Documents and Settings\bquakkelaar\Desktop\dropstuff\asp.net mvc\Genesis.0.02\Genesis.0.02\Controllers\AdminStreamController.cs:107
lambda_method(Closure , ControllerBase , Object[] ) +108
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +51
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +409
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +52
System.Web.Mvc.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a() +127
When I put a breakpoint into the function, I see that stream is not null. Some strings are null. Required strings are not null (IE: streamName = "name") and StreamID is 0.
Where am I going wrong?
Edit: How `streamTable` is instanced
I don't think there is a problem with how I instance streamTable but seeing as how I out of ideas and most people here think that it's null, here is the code that instances streamTable.
public partial class SqlGenesisRepository : IGenesisRepository
{
private Table<Stream> streamTable;
public SqlGenesisRepository(string connectionString)
{
streamTable = (new DataContext(connectionString)).GetTable<Stream>();
}
public IQueryable<Stream> Streams { get { return streamTable; } }
}
And the SqlGenesisRepository is instanced in the controller class like this:
public class AdminStreamController : Controller
{
private IGenesisRepository genesisRepository;
public AdminStreamController()
{
//genesisRepository = new FakeGenesisRepository();
genesisRepository = new SqlGenesisRepository(ConfigurationManager.ConnectionStrings["genesis"].ConnectionString);
}
/* rest of code removed for brevity */
}
Thanks to everyone for the attention to this issue.
It looks like the incorrect code wasn't in any of the code I posted here. I rephrased my question and posted it here: Is there a secret to using LINQ to SQL to add records when the object has relationships?.
The solution can be found there!
Thanks again.
You don't need to attach the object to the table since the object is new. You just need to InsertOnSubmit.
If you get a null exception after removing that line, your Stream object is likely missing a required field.
Maybe streamTable is null?
Edit
Okay, so based on the stack trace I'm thinking that you may be missing a foreign key constraint. What relationships does a Stream have? Can you create a new Stream in your database using hand-coded SQL, given only the information that you have in this code?
Have you tried explicitly declaring Stream as Genesis.Domain.Entities.Stream ?
As in:
private Table<Genesis.Domain.Entities.Stream> streamTable;
public SqlGenesisRepository(string connectionString)
{
streamTable = (new DataContext(connectionString)).GetTable<Genesis.Domain.Entities.Stream>();
}
Because "Stream" alone could be confusing the compiler, as System.IO.Stream
Also, when you put a breakpoint at
streamTable.InsertOnSubmit(stream);
...to check if streamTable is null, did you check its contents with the debugger?
That part where it says "Expanding this item will enumerate ...".
It's important because generally it lazy loads, therefore it doesn't go to the DB unless it requires a transaction.

Categories

Resources