0

Why my object is null on inserting an HTML input in SQL Database on an ASP.NET website Hosted on ASP.NET web host. My web host is hostbreak.com The code is working fine on localhost using Visual Studios 2019.

Actually I must input this data through HTML input field.

This is the Simple HTML input which is causing NullReferenceException.

My IAUTO.aspx code

<input ID="someid" runat="server" value="somevalue" />

My IAUTO.aspx.cs code

public void Loaddata()
{
    SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["someConnectionString"].ConnectionString);

    conn.Open();
    SqlCommand insertCommand = new SqlCommand("INSERT INTO automobiletable(someid)"
    + "VALUES (@a1)", conn);
    insertCommand.Parameters.AddWithValue("@a1", someid.Value.ToString());

I have tried the following method also but getting error In IAUTO.aspx file

<asp:HiddenField ID="someid" runat="server" value="somevalue" />

& In IAUTO.aspx.cs file

//code is same as above

Please help me with this. Maybe my web host offers asp.net only not HTML. I am getting this error

Server Error in '/' Application.
Object reference not set to an instance of an object.
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:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
   ecoio.IAuto.Submit_Click(Object sender, EventArgs e) in J:\EIO\ecoio\ecoio\ecoio\IAuto.aspx.cs:260
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9781022
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +204
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3770.0

Thanks

A Plea: I really need help. I really appreciate stackoverflow.com. They helped me a lot. That's a great service.

Aslam Ranjha
  • 63
  • 1
  • 9
  • This Question is not a duplicate. Your suggestion is something else. I know what is NULLREFERENCE. My Question is different it is about inserting an HTML input in sql database on an ASP.NET website on ASP.NET webhost. Kindly, open this question again. someone might help me. I really need help. I really appreciate stackoverflow.com. They helped me a lot. That's a great service. – Aslam Ranjha May 01 '21 at 20:05
  • Sorry but he didn't do anything unusual. NullReferenceException is typically marked as duplicate in SO. Once you understand there's an object that was not initialised, the workaround is obvious. What is in your code line number 260 in file IAuto.aspx? This should be rewritten to qualify as a question. May be if you ask why my object is null and show your code, but I don't see that information. – derloopkat May 01 '21 at 20:26
  • Thanks Sir @DanielManta, I've updated my question as your suggestion. Just 1 line of code is causing error which is described in my question. – Aslam Ranjha May 01 '21 at 20:58
  • Make sure there's a key for "someConnectionString" in connectionStrings section of your web.config. If the key exists in localhost and it doesn't at the remote server, that would raise NullReferenceException when trying to read property `ConnectionString` of a null reference. – derloopkat May 01 '21 at 22:16
  • In Web.config this the only key but there is no key in connectionStrings – Aslam Ranjha May 01 '21 at 22:19
  • Here is my connection string //I've kept some info private here – Aslam Ranjha May 01 '21 at 22:20
  • is same in Visual Studio and my Web Host. I personally think my WEBHOST is providing me ASP.NET only hosting with no support of HTML. Can this be the reason? – Aslam Ranjha May 01 '21 at 22:24
  • Since you use `WebConfigurationManager` it's possible that is getting the connection strings through [configuration inheritance](https://stackoverflow.com/questions/698157/whats-the-difference-between-the-webconfigurationmana). – derloopkat May 01 '21 at 22:26
  • @DanielManta Thanks for your precious time. I make the change and let you know. – Aslam Ranjha May 01 '21 at 22:36
  • I've put the connection string manually instead of WebConfigurationManager but my problem is still not solved. – Aslam Ranjha May 01 '21 at 22:53
  • Are you getting the same error? – derloopkat May 01 '21 at 22:59
  • I have restarted IIS but still getting the error. My whole form with asp.net fields is working\submitting successfully except for this only HTML input field '' – Aslam Ranjha May 01 '21 at 23:54
  • Yes the same error – Aslam Ranjha May 01 '21 at 23:57
  • Is there anyway to convert this html input data to asp.net text string before inserting into the Database? – Aslam Ranjha May 02 '21 at 00:02
  • It is Solved. My target .Net Framework was 4.7 and My web Host was offering maximum of .Net Framework 4.5. Now I have changed my project to .Net 4.5 and my form is working 100% successfully. – Aslam Ranjha May 02 '21 at 18:07

0 Answers0