You must Sign In to post a response.
  • Category: .NET

    How to insert values in to database when click on abutton

    when i am inserting my values into database i am getting error like
    Incorrect syntax near 'nvarchar'.
    Must declare the scalar variable "@".

    below is my code where i am wrong plz suggestme
    protected void Button3_Click(object sender, EventArgs e)
    {
    //string filename = FileUpload1.FileName;
    //string filenamea = FileUpload2.FileName;
    FileUpload img = (FileUpload)FileUpload1;
    Byte[] imgByte = null;


    if (img.HasFile && img.PostedFile != null)
    {
    //To create a PostedFile
    HttpPostedFile File = FileUpload1.PostedFile;
    //Create byte Array with file len
    imgByte = new Byte[File.ContentLength];
    //force the control to load data in array
    File.InputStream.Read(imgByte, 0, File.ContentLength);
    }
    else
    {
    HttpPostedFile Filea = FileUpload1.PostedFile;
    imgByte = new Byte[Filea.ContentLength];
    Filea.InputStream.Read(imgByte, 0, Filea.ContentLength);
    }

    FileUpload imga = (FileUpload)FileUpload2;
    Byte[] imgBytebb = null;


    if (imga.HasFile && imga.PostedFile != null)
    {
    //To create a PostedFile
    HttpPostedFile File = FileUpload1.PostedFile;
    //Create byte Array with file len
    imgBytebb = new Byte[File.ContentLength];
    //force the control to load data in array
    File.InputStream.Read(imgByte, 0, File.ContentLength);
    }
    else
    {
    HttpPostedFile Filea = FileUpload1.PostedFile;
    imgBytebb = new Byte[Filea.ContentLength];
    Filea.InputStream.Read(imgByte, 0, Filea.ContentLength);
    }
    string Degreeconvocationcertificate = CheckBox3.Checked ? "Yes" : "N0";
    string Degreeprovisionalcertificate = CheckBox4.Checked ? "Yes" : "N0";
    string Degreeconsolidatememorandumofmarks = CheckBox5.Checked ? "Yes" : "N0";
    string IntermediateMemo = CheckBox6.Checked ? "Yes" : "N0";
    string sscmemo = CheckBox7.Checked ? "Yes" : "N0";
    string pancard = CheckBox8.Checked ? "Yes" : "N0";
    string anyotheridproof = CheckBox9.Checked ? "Yes" : "N0";
    string powerbillofcurrentaddress = CheckBox10.Checked ? "Yes" : "N0";
    string relievingletter = CheckBox11.Checked ? "Yes" : "N0";
    string experienceletter = CheckBox12.Checked ? "Yes" : "N0";
    string salarycertificate = CheckBox13.Checked ? "Yes" : "N0";
    string otherApreciationorincrementletter = CheckBox14.Checked ? "Yes" : "N0";
    string cs = "Data Source=HOME;Initial Catalog=Registration;Integrated Security=True";
    SqlConnection con = new SqlConnection(cs);
    SqlCommand cmd = new SqlCommand(@"insert into [ektha employeea]([Employee_ Id],FirstName,MiddleName,lastname,[Father Name],DOB,[Dob original],[Blood Group],[Bank Account Details],[Do you have all your certificates],[Degree convocation Certificate],[Degree Provsional Certificate],[Degree consolidate memorandum of marks],[Intermediate Memo],sscmemo,pancard,[Any other photo Id proof],[Address proof power bill of current address],[present address],[permanent address],Emailid,pancardnumber,[martial status],[spouse/guardian name] ,[last employer],[experience-no of years],[Relieving letter],[Experience letter],[salary certificate],[appreciation/increment letter],other,[Last drawn salary in gross per month],[Date of joining at ektha],[references by],password,pc,[certificate image],[Educational qualification]) values(@[Employee_ Id],@FirstName,@MiddleName,@lastname,@[Father Name],@DOB,@[Dob original],@[Blood Group],@[Bank Account Details],@[Do you have all your certificates],@[Degree convocation Certificate],@[Degree Provsional Certificate],@[Degree consolidate memorandum of marks],@[Intermediate Memo],@sscmemo,@pancard,@[Any other photo Id proof],@[Address proof power bill of current address],@[present address],@[permanent address],@Emailid,@pancardnumber,@[martial status],@[spouse/guardian name] ,@[last employer],@[experience-no of years],@[Relieving letter],@[Experience letter],@[salary certificate],@[appreciation/increment letter],@other,@[Last drawn salary in gross per month],@[Date of joining at ektha],@[references by],@password,@pc,@[certificate image],@[Educational qualification]) ", con);
    cmd.Parameters.AddWithValue("@[Employee_ Id]", TextBox1.Text.Trim());
    cmd.Parameters.AddWithValue("@FirstName", TextBox2.Text.Trim());
    cmd.Parameters.AddWithValue("@MiddleName", TextBox3.Text.Trim());
    cmd.Parameters.AddWithValue("@lastname", TextBox4.Text.Trim());
    cmd.Parameters.AddWithValue("@[Father Name]", TextBox5.Text.Trim());
    cmd.Parameters.AddWithValue("@DOB", TextBox6.Text.Trim());
    cmd.Parameters.AddWithValue("@[Dob original]", TextBox7.Text.Trim());
    cmd.Parameters.AddWithValue("@[Blood Group]", TextBox8.Text.Trim());
    cmd.Parameters.AddWithValue("@[Bank Account Details]", TextBox9.Text.Trim());
    cmd.Parameters.AddWithValue("@[Do you have all your certificates]", CheckBox1.Text);
    cmd.Parameters.AddWithValue("@[Degree convocation Certificate]", Degreeconvocationcertificate);
    cmd.Parameters.AddWithValue("@[Degree Provsional Certificate]", Degreeprovisionalcertificate);
    cmd.Parameters.AddWithValue("@[Degree consolidate memorandum of marks]", Degreeconsolidatememorandumofmarks);
    cmd.Parameters.AddWithValue("@[Intermediate Memo]", IntermediateMemo);
    cmd.Parameters.AddWithValue("@sscmemo", sscmemo);
    cmd.Parameters.AddWithValue("@pancard", pancard);
    cmd.Parameters.AddWithValue("@Any other photo Id proof", anyotheridproof);
    cmd.Parameters.AddWithValue("@Address proof power bill of current address", powerbillofcurrentaddress);
    cmd.Parameters.AddWithValue("@present address", TextBox10.Text.Trim());
    cmd.Parameters.AddWithValue("@permanent address", TextBox11.Text.Trim());
    cmd.Parameters.AddWithValue("@Emailid", TextBox12.Text.Trim());
    cmd.Parameters.AddWithValue("@pancardnumber", TextBox13.Text.Trim());
    cmd.Parameters.AddWithValue("@[martial status]", TextBox14.Text.Trim());
    cmd.Parameters.AddWithValue("@[spouse/guardian name]", TextBox15.Text.Trim());
    cmd.Parameters.AddWithValue("@[last employer]", TextBox16.Text.Trim());
    cmd.Parameters.AddWithValue("@[experience-no of years]", TextBox17.Text.Trim());
    cmd.Parameters.AddWithValue("@[Relieving letter]", relievingletter);
    cmd.Parameters.AddWithValue("@[Experience letter]", experienceletter);
    //cmd.Parameters.AddWithValue("@Experience letter", experienceletter);
    cmd.Parameters.AddWithValue("@[salary certificate]", salarycertificate);
    cmd.Parameters.AddWithValue("@[appreciation/increment letter]", experienceletter);
    cmd.Parameters.AddWithValue("@other", TextBox18.Text.Trim());
    cmd.Parameters.AddWithValue("@[Last drawn salary in gross per month]", TextBox19.Text.Trim());
    cmd.Parameters.AddWithValue("@[Date of joining at ektha]", TextBox20.Text.Trim());
    cmd.Parameters.AddWithValue("@[references by]", TextBox21.Text.Trim());
    cmd.Parameters.AddWithValue("@password", TextBox22.Text.Trim());
    cmd.Parameters.AddWithValue("@Experience letter", TextBox23.Text.Trim());
    cmd.Parameters.AddWithValue("@pc", imgByte);
    cmd.Parameters.AddWithValue("@[certificate image]", imgBytebb);
    cmd.Parameters.AddWithValue("@[Educational qualification]", DropDownList1.SelectedItem.Text);

    con.Open();
    cmd.ExecuteNonQuery();

    con.Close();

    }

    }


    }

    this my datbase query
    CREATE TABLE [dbo].[ektha employeea](
    [Employee_ Id] [int] NULL,
    [FirstName] [nchar](10) NULL,
    [MiddleName] [nchar](10) NULL,
    [lastname] [nchar](10) NULL,
    [Father Name] [nchar](10) NULL,
    [DOB] [date] NULL,
    [Dob original] [nvarchar](50) NULL,
    [Blood Group] [nvarchar](50) NULL,
    [Bank Account Details] [nvarchar](50) NULL,
    [Do you have all your certificates] [nchar](10) NULL,
    [Degree convocation Certificate] [nchar](10) NULL,
    [Degree Provsional Certificate] [nchar](10) NULL,
    [Degree consolidate memorandum of marks] [nchar](10) NULL,
    [Intermediate Memo] [nchar](10) NULL,
    [sscmemo] [nchar](10) NULL,
    [pancard] [nchar](10) NULL,
    [Any other photo Id proof] [nchar](10) NULL,
    [Address proof power bill of current address] [nchar](10) NULL,
    [present address] [nvarchar](max) NULL,
    [permanent address] [nvarchar](max) NULL,
    [Emailid] [nvarchar](50) NULL,
    [pancardnumber] [nvarchar](50) NULL,
    [martial status] [nchar](10) NULL,
    [spouse/guardian name] [nvarchar](50) NULL,
    [last employer] [nchar](10) NULL,
    [experience-no of years] [nchar](10) NULL,
    [Relieving letter] [nchar](10) NULL,
    [Experience letter] [nchar](10) NULL,
    [salary certificate] [nchar](10) NULL,
    [appreciation/increment letter] [nchar](10) NULL,
    [other] [nchar](10) NULL,
    [Last drawn salary in gross per month] [nchar](10) NULL,
    [Date of joining at ektha] [nvarchar](50) NULL,
    [references by] [nvarchar](50) NULL,
    [password] [nchar](10) NULL,
    [pc] [image] NULL,
    [certificate image] [image] NULL,
    [Educational qualification] [nchar](10) NULL
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

    GO
    so where iam wrong plz help me
  • #764196

    Hi VELIDIRAVIRAM,

    It seems one of your textbox is assigning wrong/null value to cmd.
    Just try one thing, debug the application and check value in cmd after it is properly built with parameters i.e. before executing line cmd.ExecuteNonQuery();, with the help of quick watch in visual studio.
    Paste that generated query in SQL Server Management Studio and try to run it from query window.
    This way you will be able to know why your query is generating wrong.

    Regards,
    Shashikant Gurav
    shashikantgurav22@gmail.com

  • #764201

    Hi,

    This is too deficult to identfy the mistake in your code, because your query is too big, rather than writing it in built in query I suggest you to prepare stored procedure and call that in your code block that will give easy understand to others and maintainence for future, coming to your thread as per my simple look at the code I found few mistakes in your assignment please correct those and recheck again.

    Replace below lines

    cmd.Parameters.AddWithValue("@Any other photo Id proof", anyotheridproof);
    cmd.Parameters.AddWithValue("@Address proof power bill of current address", powerbillofcurrentaddress);
    cmd.Parameters.AddWithValue("@present address", TextBox10.Text.Trim());
    cmd.Parameters.AddWithValue("@permanent address", TextBox11.Text.Trim());
    cmd.Parameters.AddWithValue("@Experience letter", TextBox23.Text.Trim());


    with the below code

    cmd.Parameters.AddWithValue("@[Any other photo Id proof]", anyotheridproof);
    cmd.Parameters.AddWithValue("@[Address proof power bill of current address]", powerbillofcurrentaddress);
    cmd.Parameters.AddWithValue("@[present address]", TextBox10.Text.Trim());
    cmd.Parameters.AddWithValue("@[permanent address]", TextBox11.Text.Trim());
    cmd.Parameters.AddWithValue("@[Experience letter]", TextBox23.Text.Trim());


    There is a gap between the letters in that case you must keep your content within "[]".


    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #764205

    Hai VELIDIRAVIRAM,
    It seems that the issue is ta the below link where you forgot to keep bracket:

    cmd.Parameters.AddWithValue("@Experience letter", TextBox23.Text.Trim());

    So you can try out with as below:

    cmd.Parameters.AddWithValue("@[Experience letter]", TextBox23.Text.Trim());

    Hope it will be helpful to you.


    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


Sign In to post your comments