473,473 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do I perform insertions & updates to a table in SQL server using C#?

55 New Member
I'm trying to create a leave app in asp.net,I have a leaveHistory table which would store each employee's personal details and their leave history(empLeaveHistory).

This field has an xml data type.I would like to know how to create an xml document from data retrieved from a web form using C#.For some reason the command object doesn't execute the query and I'm stumped.

This is the C# method I have:

public void InsertXML(MyObjects.LeaveApplicationClass LeaveClass)
{
char c=(char) 34;
// string xmlStr = "";
string updateQryStr = "UPDATE [dbo].[EmployeeLeaveHistory] ";
//updateQryStr = updateQryStr + Environment.NewLine;
updateQryStr = updateQryStr + "SET empLeaveHistory.modify";
updateQryStr = updateQryStr + "(";

updateQryStr = updateQryStr + "INSERT";
updateQryStr = updateQryStr + "<LeaveHistory><Employee id=" + c + LeaveClass.EmployeeNum + c + "><leave><startdate>" + LeaveClass.StartDate.ToShortDateString() + "</startdate><enddate>" + LeaveClass.StartDate.ToShortDateString() + "</enddate/><leavetype>" + LeaveClass.LeaveType.ToString() + "</leavetype></leave></Employee></LeaveHistory> ";//as last into()

updateQryStr = updateQryStr + ")where EmployeeLeaveHistory.swipeNum="+LeaveClass.Employe eNum.ToString();


SqlConnection myConn = new SqlConnection();
myConn = this.CreateConnection();
if (myConn.State==ConnectionState.Closed)
{

try
{
myConn.Open();
SqlCommand cmd = new SqlCommand(updateQryStr,myConn);
//Int32 r = cmd.ExecuteNonQuery();
SqlDataReader rdr = cmd.ExecuteReader();-//ERROR OCCURS HERE
}
catch (Exception)
{

throw;
}
finally
{
if (myConn.State==ConnectionState.Open)
{
myConn.Close();
}

}
}



//return xmlStr;

}

The xml document would look like this:

<LeaveHistory>
<Employee id="460414">
<leave>
<startdate>08/08/2000</startdate>
<enddate>08/08/2000 </enddate>
<leavetype>3</leavetype>
</leave>
<leave>
<startdate>08/08/2010</startdate>
<enddate>08/08/2010 </enddate>
<leavetype>1</leavetype>
</leave>
</Employee>
</LeaveHistory>

Table:
CREATE TABLE [dbo].[EmployeeLeaveHistory](
[id] [int] IDENTITY(1,1) NOT NULL,
[swipeNum] [int] NULL,
[first_name] [varchar](50) NOT NULL,
[last_name] [varchar](50) NOT NULL,
[hire_date] [datetime] NULL,
[dob] [datetime] NULL,
[termination_date] [datetime] NULL,
[empLeaveHistory] [xml] NULL,
CONSTRAINT [PK_EmployeeProfile] PRIMARY KEY CLUSTERED
(
[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]
Aug 14 '10 #1
0 782

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Gary | last post by:
I haver a table of students - Say 100 students that I need to be able to update/delete and amend. I know I can do this one student at a time which is simple but lets say I want to see all the...
3
by: Richard Muller | last post by:
Hi All, I've got the ASP script shown below that complains as follows: Microsoft OLE DB Provider for ODBC Drivers (0x80004005) Data source name not found and no default driver specified...
2
by: fhadlaq | last post by:
I need a little help here.. I want to transfer ONLY new records AND update any modified records from Oracle into SQL Server using DTS. How should I go about it? a) how do I use global variable...
1
by: Susan Geller | last post by:
I have a table server control (System.Web.UI.WebControls.Table) on my form that has three columns. Sometimes I need the second column to be invisible, sometimes the third. How can I set a column...
1
by: Mark | last post by:
I have a Table server control with several link buttons. How do I generate the click events for each of these controls? I can't double click on them or go into the properties of the linkbuttons...
5
by: Sukhanov Volodya | last post by:
Hello, all! Does anybody knows, how I can show an image field SQL Server using VB. NET ? I have an database, which has an table with an field which has type 'image' (the type of field) I want...
0
by: Rahoove | last post by:
can anyone help me on this> I always get an error login failure message when I tried login in to MSDE, SQL server using ASP.net in VB.net. Before now, when I log in to the sample database,...
0
by: raghutumma | last post by:
Hi, I need help from you... I want to find SQL Servers which are available in Network and Databases in Each Server Using Batch file. If i run this Batch file it should find SQL Servers &...
1
by: sampalmer21 | last post by:
Is there a way I can find out if a database I have created exists in the sql server using ado.net methods? How about determining if a table exists, or any other database object for that matter? ...
0
by: Siv | last post by:
Hi, What is the best way to get the field names of a table in SQL server where you only have Windows Authentication access to a database. Previously I have used the SQLDMO object to get this, but...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.