I am trying to generate form from my database.
CREATE TABLE [dbo].[FieldsMaster](
[FieldId] [int] IDENTITY(1,1) NOT NULL,
[FieldControlName] [varchar](50) NULL,
[FieldDataType] [varchar](20) NULL,
[FieldControlType] [varchar](30) NULL,
[DisplayLabel] [varchar](200) NULL,
[FieldSize] [int] NULL,
[FieldRegularExpression] [varchar](200) NULL,
[FieldOptions] [varchar](500) NULL,
[OptionalQuery] [varchar](200) NULL,
[REMessage] [varchar](150) NULL,
[FieldRequired] [bit] NULL,
[FRMessage] [varchar](150) NULL
)
Generate some data for the table
insert into FieldsMaster (FieldControlName,FieldDataType,FieldControlType,DisplayLabel,FieldSize,FieldRegularExpression,
FieldOptions,OptionalQuery,REMessage,FieldRequired,FRMessage,IsActive)
values
('txtOtherInfo','varchar','TextArea','Other Information',400,NULL,
NULL,NULL,NULL,0,NULL)
insert into FieldsMaster (FieldControlName,FieldDataType,FieldControlType,DisplayLabel,FieldSize,FieldRegularExpression,
FieldOptions,OptionalQuery,REMessage,FieldRequired,FRMessage,IsActive)
values
('txtTitle','varchar','TextBox','Title',100,NUll,
NULL,NULL,NULL,1,'Please enter title')
Now this form with it fields is stored in my sql db. I want to generate a form in mvc. How the model should be formed/ how to generate form in view?
I am really confused here. Your help is really appreciated. Thanks
To have more idea, below form should be displayed based on the fields in FieldsMaster
Other Information(as label) : TextArea (With Id as txtOtherInfo)
Title (as Label) : TextBox (With Id as txtTitle)
If you want to add form directly from database, you can do.
First you need to do into model an Enum like these (for email):
public int EmailTemplateId { get; set; }
public int Type { get; set; }
public string Name { get; set; }
public enum Emailtype
{
Base = 0,
Recovery = 1,
NewAccount = 2,
ChangePassword = 3,
BlockAccount = 4,
UnlockAccount = 5,
ForgetPassword = 6
}
So each one of thee represent a different template, and in your template view you have something like
<body>
//some code of your template
{-container-}
</body>
As you can see {-container-} is the property you change for each template. so you made new containers that you´be insert into sql like
<tr>
<td>Hi {name}. Your password is {password}</td>
</tr>
You don´t need tag here because you have it into your first template and this code
And in controller:
public void NewAccount(string name, string email,string password, string user)
{
var from = model.From;
var loginUrl = model.LoginUrl;
const string title = "New Register - Stackoverflow";
EmailsService sendEmail = new EmailsService
{
Subject = title,
To = email,
From = from
};
}
//There is our first template (Base template)
var templateBase = Get(Emailtype.Base);
//This one is template who changes (container)
var templateText = Get(Emailtype.NewAccount);
//Here you replace Name property of class for your container code
var body = templateBase.Name.Replace("{-container-}", templateText.Name);
sendEmail.EmailBody = body.Replace("{Name}", name)
.Replace("{Title}", title)
.Replace("{User}", user)
.Replace("{Password}", password)
.Replace("{Link}", loginUrl);
sendEmail.Send();
}
Finally in your sql you can insert code of your container... it show like that:
EmailTemplateId=1
Type= 1 //(Recovery ones)
Name= <tr> <td>Hi {name}. Your password is {password}</td> </tr>
Hope it helps!
Related
I'm making a data import feature from excel using maatwebsite. I successfully imported the data into the database, but the year data that should have been 01/01/2022 and 31/12/2022 instead became 01/01/1970 and 01/01/1970.
this is the data that i will import to the system:
data excel
and the following is the data that was successfully imported to the database:
data in database
and this is the code for AlokasicutiImport:
<?php
namespace App\Imports;
use Carbon\Carbon;
use App\Models\Alokasicuti;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\ToModel;
class AlokasicutiImport implements ToModel
{
public function startRow(): int
{
return 2;
}
/**
* #param array $row
*
* #return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
if(isset($row[0]) && isset($row[2]))
{
if(!Alokasicuti::where('id_karyawan',$row[0])->where('id_jeniscuti',$row[2])->exists())
{
$data = [
'id_karyawan' => $row[0] ?? null,
'id_settingalokasi'=> $row[1] ?? null,
'id_jeniscuti' => $row[2] ?? null,
'durasi' => $row[3] ?? null,
'mode_alokasi' => $row[4] ?? null,
'tgl_masuk' => Carbon::parse($row[5])->format("Y-m-d") ?? null,
'tgl_sekarang' => Carbon::parse($row[6])->format("Y-m-d") ?? null,
'aktif_dari' => Carbon::parse($row[7])->format("Y-m-d") ?? null,
'sampai' => Carbon::parse($row[8])->format("Y-m-d") ?? null,
];
Alokasicuti::create($data);
}else{
Log::info('id pegawai dan tanggal absensi sudah ada');
}
} else{
Log::info('Row 1 kosong');
}
}
}
this is the controller that I use to import the data:
public function importexcel(Request $request)
{
Excel::import(new AlokasicutiImport, request()->file('file'));
return redirect()->back();
}
what are the causes sir? Please help
The Carbon::parse() method cannot guess every input format correctly. In this case, you should use Carbon::createFromFormat() and define a specific input format.
Carbon::createFromFormat()
'tgl_masuk' => Carbon::createFromFormat('d/m/Y', $row[5])->format('Y-m-d') ?? null,
The format may need to be adjusted from what is shown above if the date is being interpreted differently than shown in Excel. You should check this first.
Requires that my function is implemented access to the databaseand certain operations were carried out.
Such as:
1) Check or read the message.
2) Checking and issuing all communications between users by date.
3) Find the latest posts from all users.
But I though there is material in the tutorial I could not figure out how to do. All I could show you. Therefore, I want to ask you to see my code and see what's up there and help me out. You do not need to completely write code. Just if it's not hard for you to sink your sleeves and calmly give examples let me write these three functions on LINQ and EntityFramework
MY CODE:
public bool CheckIfUnreadMessagesArePresent(int userId)
{
IList<MessageInfo> messageInfo = new List<MessageInfo>();
MessageInfo messageIn = new MessageInfo();
var sql = #"SELECT * FROM MessageInfo
WHERE wasRead = 0 AND Id = #userId ORDER BY date DESC";
var meassage = dataBaseContext.Database.SqlQuery<MessageInfo>(sql, new { userId = messageIn.Id });
return messageInfo.Any(messageIn.WasRead == true);
}
public IList<UserInfo> FetchMessagesFromConversation(int userID1, int userID2)
{
IList<UserInfo> messageInfo = new List<UserInfo>();
MessageInfo messageIn = new MessageInfo();
var sql = #"SELECT * FROM MessageInfo
WHERE userID1 = 1 AND userID2 = 3 OR userID1 = 3 AND userID2 = 1 order by date DESC";
var meassage = dataBaseContext.Database.SqlQuery<MessageInfo>(sql, new { userID1 = messageIn.SenderId,userID2 = messageIn.ReceiverId});
messageInfo.Insert(message);
return messageInfo;
}
public IList<MessageInfo> FetchLastMessageFromEachConversation((int userID))
{
IList<UserInfo> messageInfo = new List<UserInfo>();
MessageInfo messageIn = new MessageInfo();
var sql = #"SELECT * FROM MessageInfo
WHERE date IN (#SELECT max(date)
FROM MessageInfo WHRERE date IN (
SELECT max(date)
FROM MessageInfo
GROUP BY messageIn.ID, messageIn.ReceiverId
ORDER BY date DESC, messageIn.ID, messageIn.ReceiverId)
GROUP BY if (messageIn.ID < messageIn.ReceiverId,
concat(messageIn.ID, " - ", messageIn.ReceiverId), concat(messageIn.ReceiverId, " - ", messageIn.ID)))";
var meassage = dataBaseContext.Database.SqlQuery<MessageInfo>(sql, new { userID1 = messageIn.SenderId, userID2 = messageIn.ReceiverId });
return messageInfo;
}
My DataBase
CREATE TABLE [dbo].[UserInfo](
[Id] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Login] [varchar](50) NOT NULL,
[Password] [varchar](50) NOT NULL,
[FirstName] [nchar](10) NOT NULL,
[LastName] [nchar](10) NOT NULL,
[Email] [varchar](50) NOT NULL,
[RegistrationDate] [datetime] NOT NULL,
)
GO
CREATE TABLE [dbo].[Messages](
[Id] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[SenderId] [int] NOT NULL,
[ReceiverId] [int] NOT NULL,
[Message] [varchar](500) NOT NULL,
[date] [datetime] NOT NULL,
[WasRead][bit] NOT NULL
CONSTRAINT FK_UserInfoSender FOREIGN KEY (SenderId)
REFERENCES [UserInfo](Id),
CONSTRAINT FK_UserInfoReceiver FOREIGN KEY (ReceiverId)
REFERENCES [UserInfo](Id)
)
GO
I have an IQueryable from the database, which I map to view model like this:
IQueryable<ItemList> itemsList = query.Select(x =>
new ItemList()
{
Phone = x.Phone,
Email = x.Email,
Fax = x.Fax,
State = x.StateId.HasValue ? new GenericBox() { Text = x.State.Name , Value = x.State.Id } : null,
Type = x.Type,
ChildItems = x.Contact.ChildItems.OrderBy(a => a.Order).Select(a => new GenericBox() { Text = a.Child.FirstName + " " + a.Child.LastName, Value = a.ChildId })
});
The GenericBox class is like this:
public class GenericBox
{
public string Text { get; set; }
public Guid Value { get; set; }
}
Then I transform this IQueryable to pure SQL and execute it with a SqlCommand.
The problem lies in the properties State and ChildItems.
I need to set alias for State properties Text and Value. Because IQueryable set them in the SQL with random generated names:
SELECT
[Project3].[Id] AS [Id1],
[Project3].[Phone] AS [Phone],
[Project3].[Email] AS [Email],
[Project3].[Fax] AS [Fax],
[Project3].[C2] AS [C2],
[Project3].[Name1] AS [Name1],
[Project3].[StateId] AS [StateId],
[Project3].[Type] AS [Type],
[Project3].[C5] AS [C3],
[Project3].[C3] AS [C4],
[Project3].[AgentId] AS [AgentId]
As you can see in the SELECT Name1 is for the State.Text from my class and C4 is for ChildItem.Text.
I need in the SELECT the alias for column Name1 to be Text for example. And the alias for Column C4 I want to be again Text.
I have a link like this: C:\folder\folder\image.jpg . How can I save image to database in SQL Server?
If it was a HttpPostedFileBase I would knew how to save it, but now I have only a link.
it's a link because I exctracted src tags from tiniMCE editor field .
Thank you.
Have a look at Download and Upload Images from SQL Server via ASP.Net MVC
Code is also available on codeproject.com
Example from msdn social:
table:
CREATE TABLE [dbo].[FileStoreDemo](
[id] [int] NOT NULL,
[name] [nvarchar](100) NOT NULL,
[content] [varbinary](max) NULL,
CONSTRAINT [pk_filestoredemo_id] PRIMARY KEY CLUSTERED ([id] ASC))
code:
string filename = Server.MapPath("/Content/Desert.jpg")
using (fooEntities fe = new fooEntities())
{
FileStoreDemo fsd = fe.FileStoreDemoes.CreateObject();
fsd.name = new FileInfo(filename).Name;
fsd.content = File.ReadAllBytes(filename);
fe.FileStoreDemoes.AddObject(fsd);
fe.SaveChanges();
}
http://social.msdn.microsoft.com/Forums/en/sqlgetstarted/thread/1857938d-b74a-4954-bbde-734dfef08039
[HttpPost]
public ActionResult Create(string fileTitle)
{
try
{
HttpPostedFileBase file = Request.Files[0];
byte[] imageSize = new byte[file.ContentLength];
file.InputStream.Read(imageSize, 0, (int)file.ContentLength);
Image image = new Image()
{
Name = file.FileName.Split('\\').Last(),
Size = file.ContentLength,
Title = fileTitle,
ID = 1,
Image1 = imageSize
};
db.Images.AddObject(image);
db.SaveChanges();
return RedirectToAction("Detail");
}
catch(Exception e)
{
ModelState.AddModelError("uploadError", e);
}
return View();
}
I am new to NHibernate and am running into some issues getting the Automap functionality to work properly. Here are a couple of issues I am having.
The getting started wiki for Fluent NHibernate (http://wiki.fluentnhibernate.org/Getting_started) defines a sample with store, product, and employee classes--as well as the mapping for those classes. I replaced the manual mapping with AutoMapping and used Fluent NHibernate to generate the schema. Every thing generated properly. However, when the application attempted to save sample store, product, and employee objects, I received an error "TransientObjectException was Unhandled: object references an unsaved transient instance - save the transient instance before flushing. Type: FluentExample.Entities.Employee, Entity: FluentExample.Entities.Employee".
The automap looks like:
.Mappings(m=>
m.AutoMappings.Add(
AutoMap.AssemblyOf<FluentExample.Entities.Employee>(type => type.Namespace == "FluentExample.Entities")))
The object creation code (straight from the wiki) looks like the following. I should mention that the object creation works fine when using the manual fluent mapping.
// create a couple of Stores each with some Products and Employees
var barginBasin = new Store { Name = "Bargin Basin" };
var superMart = new Store { Name = "SuperMart" };
var potatoes = new Product { Name = "Potatoes", Price = 3.60 };
var fish = new Product { Name = "Fish", Price = 4.49 };
var milk = new Product { Name = "Milk", Price = 0.79 };
var bread = new Product { Name = "Bread", Price = 1.29 };
var cheese = new Product { Name = "Cheese", Price = 2.10 };
var waffles = new Product { Name = "Waffles", Price = 2.41 };
var daisy = new Employee { FirstName = "Daisy", LastName = "Harrison" };
var jack = new Employee { FirstName = "Jack", LastName = "Torrance" };
var sue = new Employee { FirstName = "Sue", LastName = "Walkters" };
var bill = new Employee { FirstName = "Bill", LastName = "Taft" };
var joan = new Employee { FirstName = "Joan", LastName = "Pope" };
// add products to the stores, there's some crossover in the products in each
// store, because the store-product relationship is many-to-many
AddProductsToStore(barginBasin, potatoes, fish, milk, bread, cheese);
AddProductsToStore(superMart, bread, cheese, waffles);
// add employees to the stores, this relationship is a one-to-many, so one
// employee can only work at one store at a time
AddEmployeesToStore(barginBasin, daisy, jack, sue);
AddEmployeesToStore(superMart, bill, joan);
// save both stores, this saves everything else via cascading
session.SaveOrUpdate(barginBasin);
session.SaveOrUpdate(superMart);
transaction.Commit();
When attempting to use the AutoMap functionality on one of my own classes, a class is
created, but for some reason I get errors when I attempt to actually
insert a record. The main error message says "AssertionFailure was
unhandled: null value". Here is a sample of my class, the config/
mapping, the error, and the create table script. (Note: The attributes
in the class are for use with ASP.NET MVC and have nothing to do with
NH.)
namespace Credit.Data.Entities
{
[Serializable]
public class EthnicityType
{
public EthnicityType()
{
}
[DisplayName("Id")]
[Required(ErrorMessage = "Id is required.")]
public virtual Guid Id { get; private set; }
[DisplayName("Title")]
[Required(ErrorMessage = "Title is required.")]
[StringLength(80, ErrorMessage = "Title must be less than 80 characters.")]
public virtual string Title { get; set; }
[DisplayName("Description")]
[StringLength(255, ErrorMessage = "Description must be less than 255 characters.")]
public virtual string Description { get; set; }
[DisplayName("Is Active")]
[Required(ErrorMessage = "Is Active is required.")]
public virtual bool IsActive { get; set; }
}
}
Here is the Fluent NHibernate configuration.
private static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(c => c.FromConnectionStringWithKey ("CreditConnectionString"))
.UseReflectionOptimizer()
.AdoNetBatchSize(25)
.DefaultSchema("dbo")
.Cache(c => c
.UseQueryCache()
.ProviderClass<HashtableCacheProvider>())
.ShowSql())
.Mappings(m =>
m.AutoMappings.Add(AutoMap.AssemblyOf<Credit.Data.Entities.EthnicityType>(type => type.Namespace == "Credit.Data.Entities")))
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
And the error. Yuk!
NHibernate.AssertionFailure was unhandled
Message="null identifier"
Source="NHibernate"
StackTrace:
at NHibernate.Engine.EntityKey..ctor(Object identifier, String
rootEntityName, String entityName, IType identifierType, Boolean
batchLoadable, ISessionFactoryImplementor factory, EntityMode
entityMode)
at NHibernate.Engine.EntityKey..ctor(Object id,
IEntityPersister persister, EntityMode entityMode)
at
NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate
(Object entity, EntityKey key, IEntityPersister persister, Boolean
useIdentityColumn, Object anything, IEventSource source, Boolean
requiresImmediateIdAccess)
at
NHibernate.Event.Default.AbstractSaveEventListener.PerformSave(Object
entity, Object id, IEntityPersister persister, Boolean
useIdentityColumn, Object anything, IEventSource source, Boolean
requiresImmediateIdAccess)
at
NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId
(Object entity, String entityName, Object anything, IEventSource
source, Boolean requiresImmediateIdAccess)
at
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId
(SaveOrUpdateEvent event)
at
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient
(SaveOrUpdateEvent event)
at
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.PerformSaveOrUpdate
(SaveOrUpdateEvent event)
at
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate
(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.FireSaveOrUpdate
(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.SaveOrUpdate(Object obj)
at FluentExample.Program.PopulateRecordTest(ISessionFactory
sessionFactory) in C:\Code\FluentExample\FluentExample\Program.cs:line
52
at FluentExample.Program.BootstrapNH() in C:\Code\FluentExample
\FluentExample\Program.cs:line 32
at FluentExample.Program.Main() in C:\Code\FluentExample
\FluentExample\Program.cs:line 24
at System.AppDomain._nExecuteAssembly(Assembly assembly, String
[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
And the table schema--just for kicks.
USE [Credit]
GO
/****** Object: Table [dbo].[EthnicityType] Script Date:
08/30/2009 04:59:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[EthnicityType](
[ID] [uniqueidentifier] NOT NULL,
[Title] [nvarchar](80) NOT NULL,
[Description] [nvarchar](255) NULL,
[IsActive] [bit] NOT NULL
CONSTRAINT [PK_EthnicityType_Title] PRIMARY KEY NONCLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY
= OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[EthnicityType] ADD CONSTRAINT
[DF_EthnicityType_ID] DEFAULT (newid()) FOR [ID]
GO
ALTER TABLE [dbo].[EthnicityType] ADD CONSTRAINT
[DF_EthnicityType_IsActive] DEFAULT ((1)) FOR [IsActive]
GO
I've tried a number of things to get automapping working in my environment but just have not yet been fully successful. Some variations I have tried include
Using a static map and allowing Fluent Nhibernate to recreate my table.
Changing the private set on Id to public
Creating a primarykeyconvention to try and set the Guid in case that was the issue and adding this to my mapping.
Primary Key Convention:
public class PrimaryKeyConvention : IIdConvention
{
public void Apply(IIdentityInstance instance)
{
instance.GeneratedBy.GuidComb();
//instance.GeneratedBy.Native();
}
}
Any advice or feedback is very much appreciated.
The Fluent NHibernate example entities are in the Examples.FirstProject.Entities namespace, while you're limiting the AutoMap to FluentExample.Entities (in type => type.Namespace == "FluentExample.Entities").
The first error message points to that by stating "references an unsaved transient instance [...] FluentExample.Entities.Employee"
You should update the AutoMap code to point to the correct interface. If you have classes in multiple interfaces, you can modify the criteria to include an or.