I want to upload a text file and get the data of that text file into a table of database . I m using Sql Server 2000 , and working in c#. kindly can you help me doing this and if possible can you give me the code for this

Dani AI

Generated

Short diagnosis tied to the thread: was right to push the work onto the server and was right to check permissions. The 405 error seen in 's WebClient attempt means the HTTP method used by the client was rejected by IIS — most static file URLs only accept GET, so an UploadFile POST to a plain filepath (for example a .txt URL) will be refused. See the WebClient.UploadFile behavior (WebClient.UploadFile) and the HTTP 405 explanation (405 Method Not Allowed).

Recommended workflow (reliable for inserting text-file rows into SQL Server):

  • Provide an HTTP upload endpoint on the web server (an .ashx handler, Web API action or an ASP.NET page/form using multipart/form-data). The handler reads the uploaded stream and parses lines.
  • Parse the text into a DataTable or an IDataReader-compatible stream, then push into the database with a bulk mechanism for speed (SqlBulkCopy) or with parameterized INSERTs for small sets. SqlBulkCopy is in .NET and works well with SQL Server (SqlBulkCopy).

Example server-side pattern (handler receives file, parse lines, bulk insert):

// HttpPostedFile file provided by upload endpoint
var table = new DataTable();
table.Columns.Add("Col1", typeof(string)); // add needed columns

using (var sr = new StreamReader(file.InputStream))
{
    string line;
    while ((line = sr.ReadLine()) != null)
        table.Rows.Add(line); // split/transform as required
}

using (var conn = new SqlConnection(connString))
using (var bulk = new SqlBulkCopy(conn))
{
    bulk.DestinationTableName = "dbo.TargetTable";
    bulk.WriteToServer(table);
}

Alternatives and gotchas:

  • If the goal is to push a file directly to a specific IP without an upload endpoint, use FTP (ftp://) or enable a server-side upload handler; HTTP PUT only works when the server supports WebDAV/PUT. WebClient against an HTTP static file will give 405. If using SQL Server bulk import, remember BULK INSERT expects the file to be accessible to the database server process (BULK INSERT).

Troubleshooting checklist: ensure the server endpoint accepts POST, check IIS handler mappings and WebDAV, confirm authentication/credentials and folder permissions, adjust maxRequestLength / requestFiltering for large files, validate and sanitize file contents, and always use parameterized SQL or stored procedures when inserting data.

Recommended Answers

All 5 Replies

Use Control called "Upload File" in Web-Based application (ASP.NET) then read uploaded file save its text into DB.

thanks for the reply i have uploaded the text file but i cannot download the data of that text file into the table of my database . kindly can you tell me how to read the data of the text file from internet and save its data into database .

You've the file in your hand, use File.ReadAllText("FilePathOnYourServer") which returns string, take it and using Insert statement your string'll be in your DB.

sir I uploaded the file using vb . But I wanna upload using C# .I m writing the following code private void btnUpload_Click(object sender, EventArgs e)

{
            WebClient Client = new WebClient();
            Client.UploadFile("http://localhost/imp.txt","C:\\Documents and Settings\\Maverick\\Desktop\\imp.txt");
        }

but an error message is comming

"The remote server returned an error: (405) Method Not Allowed."
kindly tell me what to do

Check the credential and permissions to do server calls, you can modifiy or add creadentials attributes into the server side method to avoid such trouble

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.