0

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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

SqlDataSource1.InsertParameters["ID"].DefaultValue = "1"; SqlDataSource1.InsertParameters["NAME"].DefaultValue = "John"; Changing the code with double quotes doesn't solve it. Still getting that 'Property access must assign to the property or use its value.' error.
Do you use this code in proper method scope? Can you show whole code file in which error is?
You need to add your code in code behind file, not in aspx file. Place it in Default.aspx.vb file.
<%@ Page aspcompat=true Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="TestDemo._Default" %> <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> <% SqlDataSource1.InsertParameters["ID"].DefaultValue = "2" SqlDataSource1.InsertParameters["NAME"].DefaultValue = "John" %> </asp:Content> This is all there is. The <div> tag which I gave in the question goes just before the <% tag for the SqlDataSource. The comment space here isn't letting me paste the entire thing.
Can you please elaborate? What exactly to write in the .aspx.vb file? As of now the contents there are Public Class _Default Inherits Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load End Sub Protected Sub DataList1_SelectedIndexChanged(sender As Object, e As EventArgs) End Sub End Class Thanks for your response.
|
0

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.

1 Comment

SqlDataSource1.InsertParameters["ID"].DefaultValue = "1"; SqlDataSource1.InsertParameters["NAME"].DefaultValue = "John"; Changing the code with double quotes doesn't solve it. Still getting that 'Property access must assign to the property or use its value.' error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.