Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class Default7 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
         if (!FileUpload1.HasFile) //Validation
        {
            Response.Write("No file Selected"); return;
        }
        else
        {
            string filename = FileUpload1.PostedFile.FileName;

            //convert the image into the byte
            byte[] imageByte = System.IO.File.ReadAllBytes(filename);

            //Insert the Data in the Table
            using (SqlConnection connection = new SqlConnection())
            {
 connection.ConnectionString = ConfigurationManager.ConnectionStrings["lateefconnectionstring"].ToString();

  connection.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = connection;

                string commandText = "Insert into users values (@Surname,@Lastame,@username,@Phone,@image,getdate,@Department())";


                cmd.CommandText = commandText;
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.Add("@Surname", SqlDbType.VarChar);
                cmd.Parameters["@Surname"].Value = TxtSurname.Text;
                cmd.Parameters.Add("@Lastame", SqlDbType.VarChar);
                cmd.Parameters["@Lastame"].Value = TxtLastName.Text;
                cmd.Parameters.Add("@username", SqlDbType.VarChar);
                cmd.Parameters["@username"].Value = TxtUsername.Text;
                cmd.Parameters.Add("@Phone", SqlDbType.VarChar);
                cmd.Parameters["@Phone"].Value = TxtPhone.Text;
                cmd.Parameters.Add("@image", SqlDbType.VarBinary);
                cmd.Parameters["@image"].Value = imageByte;
                cmd.Parameters.Add("@Department", SqlDbType.VarChar);
                cmd.Parameters["@Department"].Value = TxtDpt.Text;
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                connection.Close();

                Response.Write("Data has been Added");
            }
        }
    }
    }
Posted
Comments
Thanks7872 17-Jul-14 14:18pm    
You have not mentioned the issue and there are very less chance that you will get some reasonable answer. We can't read your mind or hard disk to see what you are doing. Further, use improve question link, remove all unnecessary code block, and clearly mention the issue.
PIEBALDconsult 17-Jul-14 14:41pm    
I think Solution 3 is correct.
I'll also point out that you don't need to specify the datatype for the parameters. You could use AddWithValue.

You have not posted the error so we're just guessing here; however, getdate will not work. It is a function so needs to be GetDate() with the ().

@Department() is also not a function, but a parameter. So use @Department.
 
Share this answer
 
v2
Instead of
string commandText = "Insert into users values (@Surname,@Lastame,@username,@Phone,@image,getdate,@Department())";


use

string commandText = "Insert into users values (@Surname,@Lastame,@username,@Phone,@image,getdate,@Department)";
 
Share this answer
 
Comments
ZurdoDev 17-Jul-14 21:44pm    
getdate won't work.

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