ADO.NET :: How To Insert Record In Sql Server Database By Using Linq
Feb 8, 2011how to insert record in sql server database by using linq?
still i m using MVC 3.0 send me full detail example for insert record using Linq
how to insert record in sql server database by using linq?
still i m using MVC 3.0 send me full detail example for insert record using Linq
I use the following code to insert recode to a sql 2008 table, the field ID of DBProgram is IDENTITY, so SQL 2008 SERVER will pass a value to it automatically. How can I get the ID value of the record I just insert ?
DBProgram dbProgram = new DBProgram();
db.DBPrograms.InsertOnSubmit(dbProgram);
db.SubmitChanges();
I have a LINQ query like this:
var query1 = from x in db.FaqCategories select new { x.CategoryId, x.Category };
What I would like to get is a query with an empty record
var empty = new {CategoryId="", Category=""};
on the very top of query1. I think this can be accomplished by using the union operator, just don't know how.
I have XML file template as:
<Students>
<Student>
<StudentID></StudentID>
<Name></Name>
<Address></Address>
<EmailId></EmailId>
<StudentMobile></StudentMobile>
<ParentName></ParentName>
<ParentMobile></ParentMobile>
<Physics></Physics>
<Chemistry></Chemistry>
<Mathematics></Mathematics>
<Biology></Biology>
<RegistrationDate></RegistrationDate>
</Student>
</Students>
in my asp.net application. I want to insert the student registration entry through my web form. but want to use proper object oriented method. like by create the object as "Student" it will have required property. and want to insert new node with incremental (auto increment) Student ID. I am using c#. how can i achieve this? will Linq is best way to do this?
I have written a script to write several values to an Access Database Table, but I need to ammend i so it checks for two values (Asset and LName) to see if they match if they do it updates the record if not it writes a new record, my script so far is as follows :
[Code]....
I'm just stuck as I can't work out the best way to do it,
i'm attempting to insert cx information into a database and then mail that information to the sales team. I'm using the DetailsView Control to accomplish the SQL Insert Command -- Success I'm using a SendMail command when the Insert button is Clicked to send a hard coded email to the sales team -- Success I can not send the User inputed information into the email -- FAIL
Example Code:
[Code]....
To be redundant -- If I send "body" by itself the system works, it falls apart when I try to add Name.Text where Name is the ID of my textbox in the form.
I put in break points, the problem starts when I get to this part:
[Code]....
I checked I can get all of my values. Here how I save my value in the bll:
[Code]....
[Code]....
how can i select a record from A table and insert into B table using linq?
View 2 RepliesI need to insert a record in table which is in remote server....I am using two connection strings for local and remote....both the servers are in same workgroup only...so am able to connect the both.The problem is am filling data of local table in on dataset1 and remote server table data in another dataset dataset2..Now whil inserting a record local an error occured like "This row already belongs to another table" after googlingI found like this like "Dataset.importrow()" instead of "dataset.add()" method...then there is no errors but the inserted record is not inserting in remote server database...
View 5 RepliesI have the following stored procedure:
ALTER PROCEDURE Pro_members_Insert
@id int outPut,
@LoginName nvarchar(50),
@Password nvarchar(15),
@FirstName nvarchar(100),
@LastName nvarchar(100),
@signupDate smalldatetime,
@Company nvarchar(100),
@Phone nvarchar(50),
@Email nvarchar(150),
@Address nvarchar(255),
@PostalCode nvarchar(10),
@State_Province nvarchar(100),
@City nvarchar(50),
@countryCode nvarchar(4),
@active bit,
@activationCode nvarchar(50)
AS
declare @usName as varchar(50)
set @usName=''
select @usName=isnull(LoginName,'') from members where LoginName=@LoginName
if @usName <> ''
begin
set @ID=-3
RAISERROR('User Already exist.', 16, 1)
return
end
set @usName=''
select @usName=isnull(email,'') from members where Email=@Email
if @usName <> ''
begin
set @ID=-4
RAISERROR('Email Already exist.', 16, 1)
return
end
declare @MemID as int
select @memID=isnull(max(ID),0)+1 from members
INSERT INTO members (................................
When I run this page, signup.aspx, provide required fields and click submit, the page simply reloads and the database table does not reflect the newly-inserted record. How do I catch the error messages that might be returned from the sproc? how to change signup.aspx so that the insert occurs.
I want to insert a new record into the database through gridview.New butoon in not inside the gridview it a diifrebt button.when we click new then it show a new row which is blank. another button is save when we click save it will save the data into database
View 4 Repliesi have a Tag Table TagId TagName
Post Table PostId PostName
TagPost Table TagId PostId
How can i do a many to many insert into database in one transaction?
I have three textbox ... textbox1 and textbox2 and textbox3 I want when I choose from date in textbox1 say 1-May-2011 and to date in textbox2 say 30-May-2011 and in textbox I type 1,2,3,4,5 I want on button click ... event the values will be entererd in database from 1-May-2011 to 30-May2011 as : mentioned below :
DATABASE STRUCTURE
ID Date Items
1 1-May-2011 1,2,3,4,5
2 2-May-2011 1,2,3,4,5
3 3-May-2011 1,2,3,4,5
so on till 30_may-2011 Record will be inserted in database according to from date and to date choosen in textbox1 and textbox2 respectively.
I want to insert a new record into a table. The table has relationships with other secondary tables, For example:
Table: Stores
Table StoreCategories (a store can have many of these)
I want to insert a new store, get its ID and insert some categories in one go.
How do I go about this?
i want to insert data into database using LINQ to sql i have textboxes and when i enter data and click the button data has to be entered into the db,i have seen examples and googled but i dint get the correct solution.how to do this??
View 13 Repliesi want to insert data into database using LINQ to sql i have textboxes and when i enter data and click the button data has to be entered into the db,i have seen examples and googled but i dint get the correct solution.how to do this??If possible give me small example.
View 8 Repliesi made two table and their repectively sp
[code]....
nw firstly i insert all value which appears on Default.aspx page and then display on grid..
but primary i couldn't work insert that value then after display grid..
When I try to insert a record the Filing Date and Report Time fields are always blank in the database.
View 5 RepliesI am using gridview with some columns few are :
<asp:templatefield headertext="Title Description" sortexpression="Description">
<itemtemplate>
<asp:Label id="Description" runat="server" Text='<%# Bind("Description")%>'></asp:Label>
</itemtemplate>
<edititemtemplate>
<asp:textbox id="Description" runat="server" CssClass="form-control" text='<%# Bind("Description") %>' />
[Code] ....
Now this AddGridAddBTN Temporarily adds a data row to a grid but won't submit that data to database, however AddGridSubmitBTN Submits the data to DB through foreach loop .
Now my query is i want to provide user a functionality that when he gets focus on Abbreviation Text Box OR Description Box and he after typing some data Press ENTER KEY , This ENTER KEY would provide funcionality as a AddGridAddBTN Button , and when he presses SHIFT + ENTER , It works like AddGridSubmitBTN Button , also i want to make Abbreviation and Description Field to be filled must !
Moral is that :
ENTER KEY WOULD DO : Generate a temporary row with holding previous values as it is actually doing on AddGridAddBTN Button.
SHIFT + ENTER WOULD DO : Enter the whole grid data thorugh foreach loop as it is actually doing on AddGridSubmitBTN Button.
I don't want to refresh my page on enter or shift enter pressing . This grid is binded through SQL DATA SOURCE ...
How to insert one record in SQL Server by using the xmlhttp object and Asp.net
View 1 RepliesI am designing a social networking site that has a "wall" feature like the others out there today. The database has an alerts table that stores some user action worthy of sharing with his friends. For example, when a user updates his status, all of his friends are notified. The below table shows two status updates from two unique users. The first (AlertId 689 and 690) is submitted by AccountId 53. Since he has one frinend - AccountId 57 - that row is added to the table so when this user logs on, he will see Account 53's update on his wall. In the same manner, the other user's status update has four rows because he has three friends.
[AlertId] [AccountId] [CreateDate] [Timestamp] [AlertTypeId] [IsHidden] [Body]
689 57 2010-08-10 0x0000000000018725 10 0 HTML
690 53 2010-08-10 0x0000000000018726 10 0 HTML
691 53 2010-08-10 0x000000000001872B 10 0 HTML
692 52 2010-08-10 0x000000000001872C 10 0 HTML
693 51 2010-08-10 0x000000000001872D 10 0 HTML
694 57 2010-08-10 0x000000000001872E 10 0 HTML
Now, a user can comment on any given item, in this case a statusupdate. When AddComment is submitted, we are using ObjectRecordId (which is the primary key of the alert being commented on) in order to identify which statusupdate is being commented on (fyi - the objectId tells us its a statusupdate):
public void AddComment(string comment)
{
if (_webContext != null)
{
var c = new Comment
{
Body = comment,
CommentByAccountId = _webContext.CurrentUser.AccountId,
CommentByUserName = _webContext.CurrentUser.UserName,
CreateDate = DateTime.Now,
SystemObjectId = _view.ObjectId,
SystemObjectRecordId = _view.ObjectRecordId
};
_commentRepository.SaveComment(c);
}
_view.ClearComments();
LoadComments();
}
Now, the problem is that when a user wants to comment on a friend's status update, he will be using the AlertId (or ObjectRecordId in the Comments table) corresponding to his account in the alerts table. The result is that comments are only viewable by the commenter and none of his friends:
[CommentId] [Body] [CommentById] [CommentByName] [ObjectId] [ObjectRecordId] [Delete]
97 hello world. 57 GrumpyCat 7 690 0
Of course the solution to this is to do something similar to what I did in the alerts table - when somebody makes a comment, make a corresponding row for every friend in the comments table. But how do I access the AlertIds of all of my friend's status updates in the Alerts table and map them to the ObjectRecordId column in the comments table? Since I can only access the status updates corresponding to my account (and their corresponding alertids), I don't know what the alertids are for the same statusupdate in my friend's accounts.
I would like to check and see if a record exists before doing an insert. Here is the code I have tried:
[Code]....
I am using a SP to insert into tables base on the parameters but getting syntax errors on:
Msg 156, Level 15, State 1, Procedure Dyno_InsertNewItem, Line 27
I can't for the life of me remember how to do this and I'm sure someone will answer this pretty quickly. I need to insert or update a record based on if it exists so in the SP I need to do:
SELECT * FROM table1 WHERE LinkedID = @ID Then I need to check if any rows were returned and apply it to an if statement which is where my mind is blocked.
I'm having trouble with an SQL statement. I want to have one statement that checks to see if it a record exists. If it does, then update it and if it doesnt then insert a new one in the server. Here is what I have so far:
[Code]....