Writing to Database using SQL

Good afternoon,

I'm trying to figure out how to insert the following code into Radzen:

image

I want to execute this code when clicking a button.

I'm kinda confused how to do this since I'm not seeing where to insert SQL-code (if that is even possible).

I've tried to update the values using the "Set property" handler but it's not working for me.

image

I think I'm doing something wrong but I can't figure out exactely what. Can you guys help me?

Thanks!

Hello @ChrisI!

I highly recommend reading the following tutorials on database operations with Radzen:

Hi @ChrisI,

Radzen generates application with Entity Framework. All CRUD operations are listed in a service using EF methods for insert, update, delete and read - there is no built-in way to execute raw SQL. If you still think this is what you want you can create custom method in your page and use EF FromSqlRaw method:

Hello Chrisl,
One possible solution if you dont want to use EF it's this one:

  1. in your .cs file of your razor page you need to create a method like this:

Public mymethod (myparameters....if needed)
{
string connectionstring="myconnectionstring .............................."
using (SqlConnection con = new SqlConnection(connectionstring))
{
using (SqlCommand cmd = new SqlCommand("", con))
{
cmd.CommandText=" your update query";
cmd.ExecuteNonQuery();
}
}
}

  1. with radzen create a button and in the Click event you can select "call custom method".

Hope this solved your problem.
Leo.

@keter83, thanks! Is this also working for a layout page instead of a normal page? I think I need to use the following selected file, right?

image

Looking forward to hearing from you :slight_smile:

Hello, I am not expert of Radzen , probably @korchev can confirm you better. but from my test it works perfectly . the trick is to create a Method in the .cs file and call it from the radzen component.
the SQL part it is the same that i wrote you before. I already use it and it works very fast. dont forget to add the using Microsoft.data , or system.data . best regards . Leo

1 Like

If you want to add custom code to a layout you should add it to the .razor.cs file. You can find more info in the Invoke custom code article.