Click here to Skip to main content
13,053,026 members (59,475 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
System.Data.SqlClient.SqlException was unhandled by user code
  Message="Incorrect syntax near ')'."
  Source=".Net SqlClient Data Provider"
  ErrorCode=-2146232060
  Class=15
  LineNumber=1
  Number=102
  Procedure=""
  Server="\\\\.\\pipe\\3A2A659F-F9B2-41\\tsql\\query"
  State=1
  StackTrace:
       at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
       at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
       at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
       at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
       at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
       at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
       at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
       at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
       at _Default.Button1_Click(Object sender, EventArgs e) in c:\Documents and Settings\kachu\My Documents\Visual Studio 2008\WebSites\newimginsert\Default.aspx.cs:line 44
       at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 

this is my code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;
 
public partial class _Default : System.Web.UI.Page 
{
   //   SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDBFileName=|DataDirectory|\\ImageDB.mdf; integrated security=true;user instance=true;");
    SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDBFileName=|DataDirectory|\\ImageDB.mdf; integrated security=true;user instance=true;");
 
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            //byte[] img = new byte[FileUpload1, postedfile.contentLength];
            byte[] img = new byte[FileUpload1.PostedFile.ContentLength];
            HttpPostedFile myimg = FileUpload1.PostedFile;
            myimg.InputStream.Read(img, 0, FileUpload1.PostedFile.ContentLength);
            SqlCommand cmd = new SqlCommand("insert into myimage(ImageName,Image) values(@imagename,)", con);
            
            SqlParameter imgname = new SqlParameter("@imagename", SqlDbType.VarChar,25);
            imgname.Value = TextBox1.Text;
            cmd.Parameters.Add(imgname);
            
            SqlParameter uploading = new SqlParameter("@image", SqlDbType.Image);
            uploading.Value = img;
            cmd.Parameters.Add(uploading);
            
            con.Open();
            
            cmd.ExecuteNonQuery();
            
            con.Close();
            
            Label1.Text = "image uploaded........";
 

        }
        else
            Label1.Text = "can't upload file.........";
    }
}

n i am following video
http://www.youtube.com/watch?v=HYdsHttQzDw


plz help me thanks in advance and my prob is i want to insert and retrieve image from sql and to retrieve image to gridview i am not getting correct code so working on it from past 4 days but still not able to do..:-(


[edit]Code blocks added - OriginalGriff[/edit]
Posted 5-Apr-12 21:07pm
Updated 5-Apr-12 21:14pm
v2
Comments
Member 13047328 16-Apr-17 2:15am
   
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Web.Configuration

Partial Class AdminProd
Inherits System.Web.UI.Page
Dim conn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=E:\988\stationary_project\App_Data\dbstet.mdf;Integrated Security=True;User Instance=True")
Dim da As New SqlDataAdapter("select * from Product order by Item_Id", conn)
Dim ds As New DataSet
Dim dt As New DataTable
Dim m As Integer
Dim img1, usr As String
Protected Sub page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim da2 As New SqlDataAdapter("select Cat_name from Category", conn)
Dim ds2 As New DataSet
ds2.Clear()
da2.Fill(ds2, "Category")
ddlcode.Items.Clear()
ddlcode.Items.Add("Select")
For j = 0 To ds2.Tables("Category").Rows.Count - 1
ddlcode.Items.Add(ds2.Tables("Category").Rows(j).Item(0))
Next
End Sub




Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Session.Count > 0 Then
usr = Session("username").ToString
Else
Response.Redirect("login.aspx")

End If
conn.Open()
ds.Clear()
da.Fill(ds, "Product")
GridView1.DataSource = ds
GridView1.DataBind()
GridView1.AllowPaging = True
GridView1.PageSize = 5
End Sub



Protected Sub ddlcode_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlcode.TextChanged
Dim da22 As New SqlDataAdapter("select Cat_code from Category where Cat_name='" & ddlcode.SelectedItem.ToString & "'", conn)
Dim ds22 As New DataSet
ds22.Clear()
da22.Fill(ds22, "Category")
If (dt.Rows.Count > 0) Then
lblccode.Text = ds22.Tables("Category").Rows(0).Item(0)

End If



End Sub

Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
GridView1.PageIndex = e.NewPageIndex
GridView1.DataSource = ds
GridView1.DataBind()
End Sub




Protected Sub btnadd_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnadd.Click
lblid.Text = ""
lblccode.Text = ""

txtnm.Text = ""
txtprice.Text = ""


Image2.ImageUrl = ""

dt = New DataTable
dt.Clear()
da.Fill(dt)
Dim n As Integer = dt.Rows.Count - 1

If dt.Rows.Count = 0 Then
m = 1
Else
m = Val(dt.Rows(n).Item(0).ToString) + 1
End If

lblid.Text = m
ddlcode.ClearSelection()
btnadd.Enabled = False
btnsave.Enabled = True
btnup.Enabled = False
btndel.Enabled = False


End Sub

Protected Sub btnsave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsave.Click
' If ddlcode.SelectedItem.Text = "Select" Then
'MsgBox("Please select category")
'ElseIf lblccode.Text = "" Then
'MsgBox("Please select category again")
'ElseIf txtnm.Text = "" Then
'MsgBox("Please enter product name")
'ElseIf txtprice.Text = "" Then
'MsgBox("Please enter product price")
'ElseIf FileUpload2.FileName = "" Then
'MsgBox("Please upload product image")
'Else


Dim img1 As String = Path.GetFileName(FileUpload2.PostedFile.FileName)
FileUpload2.SaveAs(Server.MapPath("~/image/" & img1))
img1 = "image/" & img1
Dim S As String
S = "insert into Product values(" & lblid.Text & ",'" & txtnm.Text & "'," & lblccode.Text & "," & txtprice.Text & ",'" & img1 & "')"

Dim CMD As New SqlCommand(S, conn)
CMD.ExecuteNonQuery()
ds.Clear()
da.Fill(ds, "Product")

GridView1.DataSource = ds
GridV
Member 13047328 16-Apr-17 2:17am
   
i have above code btnsave but it don't parse if condition to else part plz help
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Your query is incorrect. Try to copy the query from here and run it in SQL Server Management Studio. It will give you an idea on what syntax is incorrect.
  Permalink  
Comments
Monjurul Habib 6-Apr-12 5:13am
   
5!
Abhinav S 6-Apr-12 6:29am
   
Thank you Monjurul.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Take out the comma.
SqlCommand cmd = new SqlCommand("insert into myimage(ImageName,Image) values(@imagename,)", con);
Becomes
SqlCommand cmd = new SqlCommand("insert into myimage(ImageName,Image) values(@imagename)", con);



[edit]
You might actually want to add the other parameter instead:
SqlCommand cmd = new SqlCommand("insert into myimage(ImageName,Image) values(@imagename,@image)", con);

[/edit]
  Permalink  
v2
Comments
karthikh87 6-Apr-12 3:31am
   
i had forgot to write "@image" after comma sir.. and thanks for the great support
SqlCommand cmd = new SqlCommand("insert into myimage(ImageName,Image) values(@imagename,@image)", con);

and i wish and request for the same support al the time and its great to be in this forum
thanks once again and if possible can u please give the code to display image in grid view as i am not getting from many days m sorry if i am expecting more m very new to this development sir..
OriginalGriff 6-Apr-12 3:35am
   
You're welcome!
Have a look at this article - it does just that.
http://www.codeproject.com/Articles/268123/Display-Store-and-Retrieve-Image-Data-from-Databas
Monjurul Habib 6-Apr-12 5:13am
   
5!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Dear friend,

Please check this line
SqlCommand cmd = new SqlCommand("insert into myimage(ImageName,Image) values(@imagename,)", con);


Please remove bold semicolon

may be your problem will solve.


best regards,
Anil Avhad
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Incorrect query.
check the syntax and run it in SQL..

you are just passing only one value ..plz check it

SqlCommand cmd = new SqlCommand("insert into myimage(ImageName,Image) values(@imagename,)", con);
  Permalink  
v3

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 16 Apr 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100