Click here to Skip to main content
15,922,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void Btn_Register_Click(object sender, EventArgs e)
    {
        Employee_Register();
    }
    public void Employee_Register()
    {
        try
        {
            int id = Convert.ToInt32(Txt_E_Id.Text);
            string name = Txt_E_Name.Text;
            int age = Convert.ToInt32(Txt_E_Age.Text);
            string gender = "";
            if (RB_Male.Checked)
                gender = RB_Male.Text;
            if (RB_Female.Checked)
                gender = RB_Female.Text;
            string address = Txt_Address.Text;
            string phone = Txt_Phone.Text;
            string qualification = Txt_Qualification.Text;
            string attachment = "~/img/" + imm;
            string designation = DDL_Designation.Text;
            string date1 = Txt_J_Date.Text;
            DateTime dat = DateTime.Parse(date1.ToString());
            string date = dat.ToString("MM/dd/yyyy");
            float salary = float.Parse(Txt_Salary.Text);
            float allowance = float.Parse(Txt_Allavance.Text);
            string cworking = DDL_C_Working.Text;
            string image = Image1.ImageUrl;
            
            SqlCommand cmd = new SqlCommand("insert into EmployeeRegistration(EmpId,EmpName,Age,Gender,Address,Phone,Qualification,Attachment,Image,Designation,JoiningDate,Salary,Allowance,CurrentWorking) values(@empid,@empnam,@age,@gen,@addr,@phon,@qual,@atta,@imag,@desi,@join,@sal,@allow,@curren)", DbConnection.mCon);
            cmd.Parameters.Add("@empid", SqlDbType.Int).Value = id;
            cmd.Parameters.Add("@empnam", SqlDbType.VarChar).Value = name;
            cmd.Parameters.Add("@age", SqlDbType.Int).Value = age;
            cmd.Parameters.Add("@gen", SqlDbType.VarChar).Value = gender;
            cmd.Parameters.Add("@addr", SqlDbType.VarChar).Value = address;
            cmd.Parameters.Add("@phon", SqlDbType.VarChar).Value = phone;
            cmd.Parameters.Add("@qual", SqlDbType.VarChar).Value = qualification;
            cmd.Parameters.Add("@atta", SqlDbType.VarChar).Value = attachment;
            cmd.Parameters.Add("@imag", SqlDbType.VarChar).Value = Image1.ImageUrl;
            cmd.Parameters.Add("@desi", SqlDbType.VarChar).Value = designation;
            cmd.Parameters.Add("@join", SqlDbType.VarChar).Value = date;
            cmd.Parameters.Add("@sal", SqlDbType.Float).Value = salary;
            cmd.Parameters.Add("@allow", SqlDbType.Float).Value = allowance;
            cmd.Parameters.Add("@curren", SqlDbType.VarChar).Value = cworking;
            cmd.ExecuteNonQuery();
            Lbl_Msg.Visible = true;
            Lbl_Msg.Text = "[" + name + "]" + "Registration Successfully Added !";
            Txt_E_Name.Text = Txt_E_Age.Text = Txt_Address.Text = Txt_Phone.Text = Txt_Qualification.Text = DDL_Designation.Text = DDL_C_Working.Text = Txt_J_Date.Text = Txt_Salary.Text = Txt_Allavance.Text = "";
            RB_Male.Checked = false;
            RB_Female.Checked = false;
            Image1.ImageUrl = null;
            
        }
        catch
        {
            string name = Txt_E_Name.Text;
            Lbl_Msg.Visible = true;
            Lbl_Msg.Text = "[" + name + "]" + "Registration Failed !";
        }
    }
    protected void Btn_Cancel_Click(object sender, EventArgs e)
    {
        
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        string date = Calendar1.SelectedDate.ToShortDateString();
        DateTime dt1 = DateTime.Parse(date.ToString());
        string dt = dt1.ToString("MM/dd/yyyy");
        Txt_J_Date.Text = dt.ToString();
    }
    protected void Btn_Upload_Click(object sender, EventArgs e)
    {
        img_upload = "~/img/" + FileUpload1.FileName;
        FileUpload1.SaveAs(MapPath(img_upload));
        Image1.ImageUrl = img_upload;
    }
    protected void Btn_Upload1_Click(object sender, EventArgs e)
    {
        img_upload1 = "~/img/" + FU_Attachment.FileName;
        FU_Attachment.SaveAs(MapPath(img_upload1));
        imm = FU_Attachment.PostedFile.FileName;
        Lbl_Msg1.Text = "File name: " + FU_Attachment.PostedFile.FileName + "<br>" + FU_Attachment.PostedFile.ContentLength + " kb<br>" + "Content type: " + FU_Attachment.PostedFile.ContentType;
        
   }
Posted
Updated 16-Jan-13 1:00am
v4
Comments
Zafar Sultan 16-Jan-13 6:35am    
Whats the problem you are facing?
ravuravu 16-Jan-13 6:40am    
when i insert it, it goes to catch statement
ravuravu 16-Jan-13 6:41am    
no error occuring,but it goes to catch
ravuravu 16-Jan-13 6:46am    
data is not inserted to database
[no name] 16-Jan-13 7:23am    
Can you tell me at which line you are getting the exception?

write this code before "cmd.ExecuteNonQuery();" this line

DbConnection.mCon.Open();

if again u get error then chenge your "chtch" statement write this code line and u reply me back Lbl_Msg.Text ....

C#
catch(Exception ex)
        {

            
            Lbl_Msg.Visible = true;
            Lbl_Msg.Text =ex.Message.ToString();
            
        }
 
Share this answer
 
Comments
ravuravu 17-Jan-13 6:08am    
i clear it .the problem is in database
Shambhoo kumar 18-Jan-13 12:22pm    
Ok..
Try running the insert query with all the parameters in SQL Server Management Studio.
That should atleast give you an idea if the query you have created accepts all the data in the current format.
 
Share this answer
 
Comments
ravuravu 16-Jan-13 6:59am    
I can't solve this help me pls
Satyendra Kumar(Analyst Programmer) 16-Jan-13 7:18am    
Hi,
Please debug your code and check the line by line and check from which line you are getting the error. After that view the inner message of the Exception. One thing I saw in your query that you were trying to save Id, which must be auto incremented. Please check Id column as well.
Thanks
Member 9581488 16-Jan-13 11:45am    
check datatype of your variables. they both should match in SQL server as well as in C# when you pass it to query.

try running the query in SQL Server with all the values that you are passing from C#. It will show you an error there if its wrong.
Check your connection state... i thing you have not open the connection,
open connection before line

cmd.ExecuteNonQuery();
 
Share this answer
 
define connection string, and open connection first, then try to execute other.
 
Share this answer
 

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