简体   繁体   中英

Inserting into a database with ASP.NET causes error

I am working with VS 2017 for the first time.

I am trying to insert some values into a database. I already made the connection (checked with select statement) with the database, but now I am stuck in the insert portion.

I have a SqlDataSource defined as:

<div>
   <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="  <%$ ConnectionStrings:Sphinxx_Conn %>" 
        ProviderName="<%$ ConnectionStrings:Sphinxx_Conn.ProviderName %>"  
        InsertCommand="INSERT INTO &quot;DEMO&quot; (&quot;ID&quot;, &quot;NAME&quot;) VALUES (:ID, :NAME)">
        <InsertParameters>
            <asp:Parameter Name="ID" Type="String" />
            <asp:Parameter Name="NAME" Type="String" />
        </InsertParameters>
   </asp:SqlDataSource>
</div>

Now the following snippet:

 SqlDataSource1.InsertParameters["ID"].DefaultValue = '1'
 SqlDataSource1.InsertParameters["NAME"].DefaultValue = 'John'

Underlines the 'SqlDataSource1.InsertParameters' part and shows an error:

Property access must assign to the property or use its value.

What exactly am I doing wrong?

You are passing invalid data because you are using single quotes, use double quotes instead:

 SqlDataSource1.InsertParameters["ID"].DefaultValue = "1";
 SqlDataSource1.InsertParameters["NAME"].DefaultValue = "John";

On the side note, if you started working and getting to know web development with Visual Studio, i recommend you to look at MVC. Web Forms are old tech that is no longer supported.

There are 2 errors within your Code Snippet of assigning values.

  1. You are setting a char value instead of string , replace single quoted ' values with double quotes ". Instead of '1' do this "1" same with John too.
  2. You have not used ; to terminate your statement, so put semicolons in the end of your statements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM