SQL Server :: Inserting A File Into DB?
Jan 31, 2011I am inserting a file using fileupload control below is my code[Code]....
but i am getting the error at cmd.executereader() like Incorrect syntax near ')'.
I am inserting a file using fileupload control below is my code[Code]....
but i am getting the error at cmd.executereader() like Incorrect syntax near ')'.
I have got a page in which there is a file upload option where I have to upload/import the csv file. What I want to do is to check if the correct format of file is uploaded for instance if any other than csv file is uploaded, the system should give an error message. Also what I need to do is to check certain fields of the csv file for instance there are some mandatory fields in the csv file which should be there like name , postcode, How can I check that these fields are not empty . After performing these task, the system should automatically upload the csv file onto the sql sever 2008.
View 3 RepliesNOTE: Code and names simplified for post. Elimination of try catch and other best practices also limited for post.
I have a simple SqlCE database one table consisting of 4 columns (specialvalue, name, state)
I am using a streamreader from a file.txt that contains 3000 lines of csv data, looping the below.
s[] = {id,name,specialvalue,state) //(SAMPLE DATA-displaying names)
while(!sr.EndOfStream)
{
[Code]....
This code works as expected but I would like to increase the performance since it takes over 30 sec to complete. I will be moving to a stored procedure later but would like to find out how to optimize the current code. This is more for experience than anything. It is a project that I am working on for codeplex.
I am writing code to insert values from an xml file. I check for a duplicate before inserting and delete if there is a match. Is there a more effecient way to prevent duplicates before insert a record? my code: private void LoadFData() { SqlConnection oConn = new SqlConnection(ConnectionString); SqlCommand oCommand = new SqlCommand(); oCommand.Connection = oConn; oCommand.CommandType = CommandType.Text; oConn.Open(); // Load committee master files string[] strFiles = System.IO.Directory.GetFiles("fff", "ffff.dta", System.IO.SearchOption.AllDirectories); for (int i=0; i
View 2 RepliesI have requirement that User can Upload the Excel Sheet Data to sql server Database at once.
View 16 RepliesI am using reading data from excel file and inserting into sql server using following article, it seems to be working correctly except when I do page refresh normally, it again execute the the btnUpload_click. I want that once the record imported and if user refresh the page again it should ask for enter file name again. let me know how do I resolve this? [URL]
View 8 RepliesI need to test the counts shown in SSRS report. The report reads the counts from one of the tables.
I need to verify the counts shown in the report. For this I need to insert bulk records in the table (with different userid, datetime, etc).
How can I do this? I really don't want to do this manually.
I am trying to display a customer's statement of account. Unfortunately, the table always appear on the top of header of my page. I display the table through the code behind file of my page. Attach is the modified image of table that displays on the top of my header.
View 22 Repliesi am pulling data from database to the pdf file but i need to show those data in page by page. Here i am using iTextSharp for pdf. Is there any page break options. I did try document.NewPage(); But it didn't work out.
View 3 RepliesI have an ASP.NET project which has already some custom fields in it's web.config file. I've added in it three more fields and used them in my changes, and it's working great.
But where it needs to be used, there aren't these thre fields in the web.config (this was expected, I've just added by hand for tests and developing).
How do I check the existance of fields in web.config, and in case of not finding it, how do I add it permanently to the web.config ?
If I create a data tabe in C# like so:
datatable t = new DataTable;
DataColum C1 = new DataColum;//ID column
t.Columns.Add(C1);
//the same with column C2
Then I fill up the table with some data. Then the goal is to either insert this data or update data in an SQLtable in SQL. If the data is inserted, the ID column in t would be empty, if updated, it would not be. So in the insertion case, the sql query needs to make sure that unique IDs are assigned during insertion, in the update case, it just has to look for the right IDs.How to go about this? Asking because I am only familiar with updating and inserting single lines into a table. One could loop in C# over the datatable t of course and insert or update line by line, but this would probably be slow. Is there a way to write an SQL query like this somehow:
Insert Into SQLTable Values (@t)
?
And Update like this:
Update SQLTable SET ... Where C1 = @t.C1
Or something like that.
I know that SQL server can create automatic update and insert queries and that you can then type things in C# like
Adapter.Update(t);
But, this has proven to be super slow on our server, for unknown reasons. I have asked on this forum and got some answers from experts, but was not able to srot out the problem. How can we write a custom SQL query that allow updating and inserting a whole table, instead of just one line at a time?
When i am inserting my data i am getting this error
An explicit value for the identity column in table 'emp' can only be specified when a column list is used and IDENTITY_INSERT is ON.
i have set Identitty specification and is identity of the table as yes so i am getting this problem whenevr i set it to no i am not getting any error,can anyone explain me why i am getting this error??
I have an SQL DB which contains 2 tables. The first one 'advs' has some information about an advertisement and it got an ID field. The second one, "photos", contains all the paths of photos, this one also has an ID field.
Each advertisement from the 'advs' Table has three photos in the PHOTOS table. now obviously the photos related to the 'ad' must have the same ID.
What I'm trying to do is a Form to add the information about each advertisement and its' photos. This form should enter the SAME ID number to both tables AUTOMATICALLY after adding.
Students can subscribe for any project via a webform. Table 'project ' contains all the proposed project and table 'student' contain the name of the student and the project-id.Now, suppose there is a limit of 4 students for project 'A' and there are already 3 subscribed students for that project. One more student can choose that project. The code-behind checks whether the limit is not reached (by counting the amount students for that project in table 'student') before inserting that student in the table 'student'.
My problem is that when two students fills the webform for the same project and click on the 'save-button' exactly at the same time, the code has no time to check the limit and both students are inserted into table 'student'. Is there a way to lock the table or something in order to preventing this?
[Code]....
I want to be able to check if a value exists before inserting a new record in the table. I am taking a nvchar value from a textbox and want to compare it to existing values in the table. My codebehind is:
[Code]....
It is also important to filter down by CompanyID which is held in a Profile table. So the value can be equal in records where the CompanyID does not equal the loggedin Profile.CompanyID. So a user with a CompanyID of 2 can have a value in the table for SubType as "laptop" but a user with CompanyID of 3 can still enter "laptop" if there is no match for "laptop" and companyId = 3.
for ex. my amount is $ 1,234,343.00
and i want to insert the numbers ONLY in my database (1234343) how to that in c# .net???
I need to insert a Boolean value into an SQL Server datatable. I'm having a problem with what I'm trying. I don't see the option for a Boolean data type in SQL Server so I used Binary(1). Is this the right data type for storing Boolean in SQL Server?
The lines:
cmd.Parameters.Add(New SqlParameter("@Active", SqlDbType.Binary, 1))
cmd.Parameters("@Active").Value = False
generate the following error on the execute command:
InvalidCastException: Invalid cast from 'System.Boolean' to 'System.Byte[]'
Is there an example somewhere that covers how to store a boolean in an SQL Server table?
I have a Time(7) feild in my table that needs a default value of 0. How do I set this?
I tried to do it like this:
Dim DFTSVal as Timespan = 0
then insert the new record? But that was a no-go
The topic of sql server deadlock has been discussed many times, however, I was unsure that even two simultaneous inserts on a table can end up in a deadlock situation. Scenario: While testing our application (SQL Server 2005 as backend, ASP.net 3.5) we inserted records into a table simultaneously (simplified overview) and that resulted into a deadlock for more than 70% of users.
I could not get a hang of this as how an insert is being deadlock as this is not a case of multiple resources. After a detailed analysis (of reproducing the bug by two users) I found that both the processes were holding a RangeS-S lock on the primary key index of the table and were trying to convert this into RangeI-N lock, that resulted into a deadlock and one transaction being killed.
Question: Can we avoid or reduce these kind of deadlocks as this is not a case of change in order of access of resources? Cant' we force the transaction to get exclusive lock initially so that it blocks the other process and avoid deadlock? What (adverse) effects that may have? Also could some one explain more about RangeI-N lock.
Isolation Level for this was "Serializable".
I am wanting to insert a record into a table in SQL then retrieve its key at the same time, the key is a int that is automatically generated, I read something about the '@@Identity' function, is this what I need? How could I then bind this to a label in the ASP.net page?
View 12 Repliesi am working in the vb.net and inserting the date into DATETIME field sql db. During duging am checking it shows correct date but when insert into db it show "1/1/1900 00:00:00" . I dont know whats the problem. Code:
Dim ObjDob As DateTime = CDate(Me.ddlDates.SelectedValue & "/" & Me.ddlMonths.SelectedValue & "/" & Me.ddlYears.SelectedValue).ToString("dd/MM/yyyy")
I have a webform page that has a formview, gridview and two sqldatasources. The formview has many fields and an edit button. When a user attempts to edit the formview i want to create a seperate log record that contains datetime, username, and couple other fields from the formview. I want this log record to appear below the formview in a simple gridview. I know theoretically i can create a sql trigger but dont know how to pass the data into it. So i believe the logic is to have c# do it in the code behind during "itemUpdating" except i am struggling with stringing the values from the formview and using them in the sql insert statement, not to mention i am a asp.net newbie! i created this sql statement in itemUpdating, but this as far as my experience can take me.
con.ConnectionString = cmd.Connection = con;
cmd.CommandText = SqlConnection con =
new
SqlConnection();
"Data Source=LocalSqlServer;Initial Catalog=wilsonhs;Integrated Security=SSPI";SqlCommand
cmd = new
SqlCommand();"INSERT INTO [detentionLog] ([updated], [updatedBy], [ViolationID]) VALUES (@updated,
@updatedBy, @ViolationID)"; cmd.ExecuteNonQuery();
We have a page where you can insert a record. On the page we have validation to prevent duplicate IP addresses from being submitted for the same location. Now on the procedure side i have a condition that is checked, if you supply data that already exists for a location, then it will be updated. If its not then its inserted.. this all works fine.. issue now that i noticed is that we have a table that is part of the process, if you select say 3 from a drop down for number of emails, then when you submit the page, i have a loop for each of those emails and they are inserted into the email table as individual records.. works great also.. BUT here is my problem.. if i submit say an existing set of data and this time i provide say 1 email vs 3 from the previous time, i need to handle that in the procedure, so that the 3 are deleted and only 1 is now inserted and linked to that record..
Hope this makes sense..
Since im doing a foreach loop based on that dropdown selection, now when i look at my table im getting only 1 record in the email table... since the logic is within the procedure, when the page calls the procedure, it finds tha the record exists, deletes all the records, then only inserts the last record.
Example.
I submit the page with 3 emails today.. everything works and goes in as expected, there are 3 emails and record is created without issues..
Tomorrow someone else comes in from my team and decides to enter the same record data, thats not a problem, because there is an update statement if it already exists.. BUT this time they entered only 2 emails.. the 2 new emails are inserted / updated in the table, but the 3rd email is still in there.. its not removed.. so i tried to add a if exists condition to my procedure, but since the procedure is called in my foreach loop, the first loop sees the records, deletes them and inserts the first email, then on the 2nd foreach loop it deletes that last entry and inserts the next, so by the time it goes thru it has deleted everything it inserted and only captures the last record..
Just tried it now.. and since there was already 1 record in the table, i chose to add 4.. the end result should be 4, but i now have 5 records in there.. the first one and the 4 new ones..
I recently moved a .net site from one machine to another, now for some reason one of the stored procedures is throwing an exception when attempting to insert!
Exception Details: System.Data.SqlClient.SqlException: An explicit value for the identity column in table 'dbo.tbl_Events' can only be specified when a column list is used and IDENTITY_INSERT is ON
BTW, the column in question does have the identity set to Yes in management studio
I was using originally SQL 2005, now its on SQLexpress 2008
stored procedure:
[code]....
I have to insert a multiple records under the single table.So i have to execute the command in the frontend for every records.
Inorder to avoid i write the query like this
insert into table1(field1,field2) values('1','Test1'),('2','Test2');
and also i tried
insert into table1(field1,field2)
SELECT "1","Test1"
Union all
SELECT "2","Test2"
But I am getting syntax error.
How to insert a multiple records under the single query?