ASP.NET database Insert command

Hello,

I have been using code similar to the one shown below for inserting data into the SQL Server database 'Students' table from ASP.NET C# web form.

Define Sql data source:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SampleConnectionString %>"
InsertCommand="INSERT INTO [Students] ([FirstName], [Surname], [Age]) VALUES (@FirstName, @Surname, @Age)"

Open in new window


Insert parameter:
<InsertParameters>
            <asp:ControlParameter ControlID="Firstname" Name="FirstName" PropertyName="DisplayText"
                Type="String" />
            <asp:ControlParameter ControlID="Surname" Name="Surname" PropertyName="DisplayText"
                Type="String" />
            <asp:ControlParameter ControlID="Age" Name="Age" PropertyName="DisplayText"
                Type="String" />
</InsertParameters>

Open in new window


And C# code for inserting data into the database table 'Students':
SqlDataSource1.Insert();

Open in new window


The codes shown above will insert the form data entered by the user into the 'Students' table by reading the displayed texts (typed texts) on the text boxes Firstname, Surname and Age.

My Question:
If I want to do a simple calculation such as:
Multiply the age typed in by the user in the 'Age' text box by 2
then insert the result into the 'Students' table 'Age' column/field, how do I do it?
ZKM128Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Randy DownsOWNERCommented:
Maybe you can adapt this. In your case you onlyneed to multiply one variable & your SQL is evidently defined as SqlDataSource1.

SQL = "insert into " & DateTime.Now.ToString("ddd_dd_MMM_yyyy") _
                                          & "(ID, Name, Calories, Of_Which_Fat, Protein, Carbohydrate, Of_which_sugars," _
                                          & "Fat, Of_Which_Saturated, Of_Which_Monounsaturates, Of_Which_Polyunsaturates," _
                                          & "Fibre, Sodium) select ID, Name, Calories * 1.5, Of_Which_Fat, Protein* 1.5, " _
                                          & "Carbohydrate* 1.5, Of_which_sugars* 1.5,Fat* 1.5, Of_Which_Saturated* 1.5, Of_Which_Monounsaturates* 1.5," _
                                          & "Of_Which_Polyunsaturates* 1.5, Fibre* 1.5, Sodium* 1.5 From NutrientTable where id =" & listBox.SelectedValue

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZKM128Author Commented:
If I need to do some conditional statement operations then insert the result into the database table, how do I do it?

For example, if I need to insert the word "Senior HS" into the "High School" column if the "Age" value typed into the "Age" text box is greater than 15 how do I do it? How do I carry out the conditional operation in C# and apply the result to the SQL insert parameter value in my script?
0
Randy DownsOWNERCommented:
You can use an if  then statement that changes High_School column to "Senior HS". You can leave it unchanged in the else statement if you want or manipulate it. Do the if statement before your insert.

    if (Age > 15)
    {
        High_School = "Senior HS";
    }
    else
    {
        Console.WriteLine("The character is uppercase.");
    }

Open in new window

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

ZKM128Author Commented:
So, exactly how do I assign the calculated result "Senior HS" to the SQL InsertParameter for inserting into the 'High School' column in the 'Students' table ?

I have tried:
string varSchool;

if (Age.DisplayText > 15)
            {
                varSchool= "Senior HS";
            }
            else
            {
                varSchool= "Below 15";
            }

SqlDataSource1.InsertParameters.Add(varSchool, TypeCode.String);

Open in new window


But the last line of the code generates the error. I need to know the correct syntax for adding the parameter.
0
Randy DownsOWNERCommented:
I assume that this is defined in earlier in your procedure. What its definition?

SqlDataSource1

Open in new window

0
ZKM128Author Commented:
I have changed the code:
SqlDataSource1.InsertParameters.Add(varSchool, TypeCode.String);

Open in new window


to this one:
SqlDataSource1.InsertParameters.Add("High_School", TypeCode.String, varSchool.ToString());

Open in new window


and it worked.

Thank you Mr Downs for your help.
0
Randy DownsOWNERCommented:
My pleasure. Don't forget to mark the correct answer. Include your final code (multiple Answers) for anyone that runs across a similar problem.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.