Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Dear Friends

i am trying to upload the file from my computer and want tp stored of replace the existing data in database .. But i am getting this error
Need your help

My Stored procedure

SQL
alter procedure Usb_fileupload   
               
(                
@Sub_Code varchar(50),      
@Activedate varchar(50)                
)                
as                
begin               
    if((select count(*) from tblActiveSubBroker where Sub_Code=@Sub_Code)<1  and @Sub_Code<>'')          
       
insert                
 into                
tblActiveSubBroker                
(   
Sub_Code,      
Activedate                
)                
values                
(    

@Sub_Code,      
convert(datetime,@Activedate,103) 
)                
  end 


and My .cs code
C#
protected void btnsave_Click1(object sender, EventArgs e)
    {
        Label1.Visible = true;
        string tbname = "tblActiveSubBroker"; //dataset .. used 2 stored. then move dataset to .. grid..
        string filename = Path.GetFileName(FileUpload1.FileName);
        try
        {
            string fileName = string.Empty;
            if (FileUpload1.HasFile)
            {

                fileName = FileUpload1.PostedFile.FileName;

                string file_ext = Path.GetExtension(fileName);
                if (file_ext.Trim().ToLower() == ".csv")
                {
                    FileUpload1.Controls.Clear();

                    string line = null; int i = 0;
                    string imgMap;
                    imgMap = ConfigurationSettings.AppSettings["imageFolderMap"].ToString();

                    //  save file on server then upload serfile on database and delete server file 
                     strPath = imgMap + Path.GetFileName(fileName);
                    if (!Directory.Exists(imgMap))
                    {

                        Directory.CreateDirectory(imgMap);
                    }
                    //else
                    //{
                    //    Directory.Delete(imgMap);
                    //    Directory.CreateDirectory(imgMap);

                    //}

                    FileUpload1.PostedFile.SaveAs(strPath);


                    using (StreamReader sr = File.OpenText(strPath))
                    {
                        while ((line = sr.ReadLine()) != null)
                        {
                            string[] data = line.Split(',');
                            if (data.Length > 0)
                            {
                                if (i == 0)
                                {

                                    foreach (object item in data)
                                    {
                                        {

                                            dt.Columns.Add(new DataColumn());

                                        }
                                    }

                                    i++;
                                }
                                DataRow row = dt.NewRow();
                                row.ItemArray = data;
                                dt.Rows.Add(row);

                            }
                        }
                     }






                     int count = dt.Rows.Count;
                     int index = 0;
                     //int currentindex = 0;


                     for (index = 0; index < count; index++)
                     {
                         string code = dt.Rows[index][1].ToString();
                         string date = dt.Rows[index][2].ToString();
                         con = new SqlConnection("Data Source=192.168.0.187;Initial Catalog=mis;Persist Security Info=True;User ID=sa;Password=ms");
                         cmd.Connection = con;
                         con.Open();

                         SqlCommand cmd1 = new SqlCommand("Usb_fileupload", con);
                         cmd1.CommandType = System.Data.CommandType.StoredProcedure;
                         cmd1.CommandTimeout = 10000;
                         SqlDataAdapter da = new SqlDataAdapter(cmd1);
                         cmd1.Parameters.Clear();
                         cmd1.Parameters.AddWithValue("@Sub_Code", SqlDbType.VarChar).Value = code;
                        
                         cmd1.Parameters.AddWithValue("@Activedate", SqlDbType.VarChar).Value = date;
                         cmd1.ExecuteNonQuery();
                         con.Close();
                         Label1.Text = "Data Inserted successfully";



                     }

              }
                else
                {

                    Label1.Text = "Error in Inserting Data";

                }
            }


        }
        catch (Exception Ex)
        {
           
            Label1.Visible = true;
            Label1.Text = "Error in Inserting Data";
           
        }
        finally
        {

            con.Close();
            dt.Clear();

            System.IO.File.Exists(strPath);
            System.IO.File.GetAccessControl(strPath);
            System.IO.File.Delete(strPath);
            Label1.Visible = true;
            Label1.Text = "Data Inserted Successfully";
        }


    }

    }




I am getting the error on date

conversion failed when converting datetime from character string
Posted
Updated 16-Jul-12 19:34pm
v2
Comments
Sebastian T Xavier 17-Jul-12 1:35am    
Please do spell check.

Hello change this line as like ....

cmd1.Parameters.AddWithValue("@Activedate", SqlDbType.DateTime).Value = date;
 
Share this answer
 
Comments
pratham2587 18-Jul-12 1:50am    
still issue is not resolved PL need help to solve the problem ..As i am new to Asp.net PLease help

regards
OPratham
Thanks in advance
sanwar_mal_jat 18-Jul-12 2:21am    
Hello thanks for rewrite.
You can try it as like...........
protected void btnsave_Click1(object sender, EventArgs e)
{
Label1.Visible = true;
string tbname = "tblActiveSubBroker"; //dataset .. used 2 stored. then move dataset to .. grid..
string filename = Path.GetFileName(FileUpload1.FileName);
try
{
string fileName = string.Empty;
if (FileUpload1.HasFile)
{

fileName = FileUpload1.PostedFile.FileName;

string file_ext = Path.GetExtension(fileName);
if (file_ext.Trim().ToLower() == ".csv")
{
FileUpload1.Controls.Clear();

string line = null; int i = 0;
string imgMap;
imgMap = ConfigurationSettings.AppSettings["imageFolderMap"].ToString();

// save file on server then upload serfile on database and delete server file
strPath = imgMap + Path.GetFileName(fileName);
if (!Directory.Exists(imgMap))
{

Directory.CreateDirectory(imgMap);
}
//else
//{
// Directory.Delete(imgMap);
// Directory.CreateDirectory(imgMap);

//}

FileUpload1.PostedFile.SaveAs(strPath);


using (StreamReader sr = File.OpenText(strPath))
{
while ((line = sr.ReadLine()) != null)
{
string[] data = line.Split(',');
if (data.Length > 0)
{
if (i == 0)
{

foreach (object item in data)
{
{

dt.Columns.Add(new DataColumn());

}
}

i++;
}
DataRow row = dt.NewRow();
row.ItemArray = data;
dt.Rows.Add(row);

}
}
}






int count = dt.Rows.Count;
int index = 0;
//int currentindex = 0;


for (index = 0; index < count; index++)
{
string code = dt.Rows[index][1].ToString();
string date = dt.Rows[index][2].ToString(); // Your Code

con = new SqlConnection("Data Source=192.168.0.187;Initial Catalog=mis;Persist Security Info=True;User ID=sa;Password=ms");
cmd.Connection = con;
con.Open();

SqlCommand cmd1 = new SqlCommand("Usb_fileupload", con);
cmd1.CommandType = System.Data.CommandType.StoredProcedure;
cmd1.CommandTimeout = 10000;
SqlDataAdapter da = new SqlDataAdapter(cmd1);
cmd1.Parameters.Clear();
cmd1.Parameters.AddWithValue("@Sub_Code", SqlDbType.VarChar).Value = code;

cmd1.Parameters.AddWithValue("@Activedate", SqlDbType.VarChar).Value =Convert.ToDateTime( date);
cmd1.ExecuteNonQuery();
con.Close();
Label1.Text = "Data Inserted successfully";



}

}
//Past here renaming code
sanwar_mal_jat 18-Jul-12 2:24am    
And use your stored procedure as like .................

alter procedure Usb_fileupload

(
@Sub_Code varchar(50),
-- @Activedate varchar(50) Your Code
@Activedate datetime
)
as
begin
if((select count(*) from tblActiveSubBroker where Sub_Code=@Sub_Code)<1 and @Sub_Code<>'')

insert
into
tblActiveSubBroker
(
Sub_Code,
Activedate
)
values
(

@Sub_Code,
--convert(datetime,@Activedate,103) your code
@Activedate
)
end
pratham2587 18-Jul-12 2:57am    
thanks sanwar mal jat

But it is still showing same error Pls help
sanwar_mal_jat 18-Jul-12 3:02am    
can u tell me result of date in this code ----- string date = dt.Rows[index][2].ToString();
You are sending your date as a varchar

C#
cmd1.Parameters.AddWithValue("@Activedate", SqlDbType.VarChar).Value = date;


and putting @tblActiveSubBroker into your procedure as varchar. Then you try to convert it to a date on insert to table.
This is not a good approach.You should do the converting in your code before sending it to sql.

C#
var myDate = DateTime.Parse("whatever");
cmd1.Parameters.AddWithValue("@Activedate", SqlDbType.DateTime).Value = myDate ;
 
Share this answer
 
Hi,

Can you change the type of @Activedate to datetime and see whether it is getting solved. You need to make corresponding type changes in your C# code also.

Regards
Sebastian
 
Share this answer
 
v2
Comments
pratham2587 17-Jul-12 1:42am    
Tried but showing same error....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900