|
 |
aspx_beginners thread: Datagrid updates only if no apostrophes in Textbox
Message #1 by "Rich Lipscomb" <rlipscomb@h...> on Fri, 17 May 2002 05:31:53
|
|
I have a datagrid which renders fine and I can edit, delete, and update
ok, unless there is an apostrophe in one of the textboxes,
e.g., "Men's". "Men" is ok, but "Men's" causes the following when
Cmd.ExecuteNonQuery() fires:
Syntax error (missing operator) in query expression ''Men's'.
Here's the code. Any ideas on
sub MyDataGrid_Update(sender as object, e as DataGridCommandEventArgs)
Dim strname, strCategory, strDesc, sqlstr As String
dim strID as string
cn.open()
strID = MyDataGrid.DataKeys(CInt(e.Item.ItemIndex))
strName = CType(e.Item.FindControl("txtMinName"), TextBox).Text
strCategory = CType(e.Item.FindControl("ddlCategory"),
DropDownList).SelectedItem.Text
strDesc = CType(e.Item.FindControl("txtMinDesc"), TextBox).Text
sqlstr = "UPDATE Ministry SET " & _
"MinName = '" & strName & "', " & _
"Category = '" & strCategory & "', " & _
"MinDesc = '" & strDesc & "' " & _
"WHERE min_ID = " & strID
CMD = New OLEDBCommand(sqlstr, cn)
Cmd.ExecuteNonQuery()
cn.close()
MyDataGrid.edititemindex = -1
BindGrid("Category")
end sub
Message #2 by "Rich Lipscomb" <rlipscomb@h...> on Fri, 17 May 2002 17:51:05
|
|
I'm trying this approach to solve the problem. When the following code
fires, the new values do not update the old values and the datagrid still
shows the old values. What do I need to change/add in the following code?
sub MyDataGrid_Update(sender as object, e as DataGridCommandEventArgs)
Dim sqlstr As String = "UPDATE Ministry SET MinName = @MinName, Category =
@Category, MinDesc = @MinDesc WHERE min_ID = @Id"
Cmd = New OLEDBCommand(sqlstr, cn)
cmd.Parameters.Add(New OleDbParameter("@Id", OleDbType.Integer, 4))
cmd.Parameters.Add(New OleDbParameter("@MinName", OleDbType.Char, 80))
cmd.Parameters.Add(New OleDbParameter("@Category", OleDbType.Char, 255))
cmd.Parameters.Add(New OleDbParameter("@MinDesc", OleDbType.LongVarWChar,
4000))
Cmd.Parameters("@Id").Value = MyDataGrid.DataKeys(CInt(E.Item.ItemIndex))
Cmd.Parameters("@MinName").Value = CType(e.Item.FindControl("txtMinName"),
TextBox).Text
Cmd.Parameters("@Category").Value = CType(e.Item.FindControl
("ddlCategory"), DropDownList).SelectedItem.Text
Cmd.Parameters("@MinDesc").Value = CType(e.Item.FindControl("txtMinDesc"),
TextBox).Text
Cmd.Connection.Open()
Cmd.ExecuteNonQuery()
Cmd.Connection.Close()
MyDataGrid.edititemindex = -1
BindGrid("Category")
end sub
--
> I have a datagrid which renders fine and I can edit, delete, and update
o> k, unless there is an apostrophe in one of the textboxes,
e> .g., "Men's". "Men" is ok, but "Men's" causes the following when
C> md.ExecuteNonQuery() fires:
> Syntax error (missing operator) in query expression ''Men's'.
> Here's the code. Any ideas on
> sub MyDataGrid_Update(sender as object, e as DataGridCommandEventArgs)
> Dim strname, strCategory, strDesc, sqlstr As String
d> im strID as string
>
c> n.open()
s> trID = MyDataGrid.DataKeys(CInt(e.Item.ItemIndex))
s> trName = CType(e.Item.FindControl("txtMinName"), TextBox).Text
s> trCategory = CType(e.Item.FindControl("ddlCategory"),
D> ropDownList).SelectedItem.Text
s> trDesc = CType(e.Item.FindControl("txtMinDesc"), TextBox).Text
> sqlstr = "UPDATE Ministry SET " & _
"> MinName = '" & strName & "', " & _
"> Category = '" & strCategory & "', " & _
"> MinDesc = '" & strDesc & "' " & _
"> WHERE min_ID = " & strID
>
C> MD = New OLEDBCommand(sqlstr, cn)
C> md.ExecuteNonQuery()
c> n.close()
> MyDataGrid.edititemindex = -1
B> indGrid("Category")
e> nd sub
Message #3 by "Minh T. Nguyen" <nguyentriminh@y...> on Fri, 17 May 2002 10:52:04 -0700
|
|
Rich,
If you replace all apostrophes with two apostrophes, it will
correctly execute your query and go into the database as one apostrophe.
The other alternative is using SqlParameters, which you have
already started (to which I don't know the answer, though).
Minh.
-----Original Message-----
From: Rich Lipscomb [mailto:rlipscomb@h...]
Sent: Friday, May 17, 2002 5:32 AM
To: aspx_beginners
Subject: [aspx_beginners] Datagrid updates only if no apostrophes in
Textbox
I have a datagrid which renders fine and I can edit, delete, and update
ok, unless there is an apostrophe in one of the textboxes,
e.g., "Men's". "Men" is ok, but "Men's" causes the following when
Cmd.ExecuteNonQuery() fires:
Syntax error (missing operator) in query expression ''Men's'.
Here's the code. Any ideas on
sub MyDataGrid_Update(sender as object, e as DataGridCommandEventArgs)
Dim strname, strCategory, strDesc, sqlstr As String
dim strID as string
cn.open()
strID = MyDataGrid.DataKeys(CInt(e.Item.ItemIndex))
strName = CType(e.Item.FindControl("txtMinName"), TextBox).Text
strCategory = CType(e.Item.FindControl("ddlCategory"),
DropDownList).SelectedItem.Text
strDesc = CType(e.Item.FindControl("txtMinDesc"), TextBox).Text
sqlstr = "UPDATE Ministry SET " & _
"MinName = '" & strName & "', " & _
"Category = '" & strCategory & "', " & _
"MinDesc = '" & strDesc & "' " & _
"WHERE min_ID = " & strID
CMD = New OLEDBCommand(sqlstr, cn)
Cmd.ExecuteNonQuery()
cn.close()
MyDataGrid.edititemindex = -1
BindGrid("Category")
end sub
Message #4 by "Rich Lipscomb" <rlipscomb@h...> on Sat, 18 May 2002 04:45:58
|
|
Yes, I definitely need to pursue the "SqlParameters" method since I can't
expect different users to remember to use 2 apostrophes all the time. I'm
still working on the code below - any ideas on how to get it to work would
be greatly appreciated - I'm sure I'm overlooking something really simple.
sub MyDataGrid_Update(sender as object, e as DataGridCommandEventArgs)
Dim sqlstr As String = "UPDATE Ministry SET MinName = @MinName, Category =
@Category, MinDesc = @MinDesc WHERE min_ID = @Id"
Cmd = New OLEDBCommand(sqlstr, cn)
cmd.Parameters.Add(New OleDbParameter("@Id", OleDbType.Integer, 4))
cmd.Parameters.Add(New OleDbParameter("@MinName", OleDbType.Char, 80))
cmd.Parameters.Add(New OleDbParameter("@Category", OleDbType.Char, 255))
cmd.Parameters.Add(New OleDbParameter("@MinDesc", OleDbType.LongVarWChar,
4000))
Cmd.Parameters("@Id").Value = MyDataGrid.DataKeys(CInt(E.Item.ItemIndex))
Cmd.Parameters("@MinName").Value = CType(e.Item.FindControl("txtMinName"),
TextBox).Text
Cmd.Parameters("@Category").Value = CType(e.Item.FindControl
("ddlCategory"), DropDownList).SelectedItem.Text
Cmd.Parameters("@MinDesc").Value = CType(e.Item.FindControl("txtMinDesc"),
TextBox).Text
Cmd.Connection.Open()
Cmd.ExecuteNonQuery()
Cmd.Connection.Close()
MyDataGrid.edititemindex = -1
BindGrid("Category")
end sub
--
If you replace all apostrophes with two apostrophes, it will
correctly execute your query and go into the database as one apostrophe.
The other alternative is using SqlParameters, which you have
already started (to which I don't know the answer, though).
Minh.
-----Original Message-----
From: Rich Lipscomb [mailto:rlipscomb@h...]
Sent: Friday, May 17, 2002 5:32 AM
To: aspx_beginners
Subject: [aspx_beginners] Datagrid updates only if no apostrophes in
Textbox
I have a datagrid which renders fine and I can edit, delete, and update
ok, unless there is an apostrophe in one of the textboxes,
e.g., "Men's". "Men" is ok, but "Men's" causes the following when
Cmd.ExecuteNonQuery() fires:
Syntax error (missing operator) in query expression ''Men's'.
Here's the code. Any ideas on
sub MyDataGrid_Update(sender as object, e as DataGridCommandEventArgs)
Dim strname, strCategory, strDesc, sqlstr As String
dim strID as string
cn.open()
strID = MyDataGrid.DataKeys(CInt(e.Item.ItemIndex))
strName = CType(e.Item.FindControl("txtMinName"), TextBox).Text
strCategory = CType(e.Item.FindControl("ddlCategory"),
DropDownList).SelectedItem.Text
strDesc = CType(e.Item.FindControl("txtMinDesc"), TextBox).Text
sqlstr = "UPDATE Ministry SET " & _
"MinName = '" & strName & "', " & _
"Category = '" & strCategory & "', " & _
"MinDesc = '" & strDesc & "' " & _
"WHERE min_ID = " & strID
CMD = New OLEDBCommand(sqlstr, cn)
Cmd.ExecuteNonQuery()
cn.close()
MyDataGrid.edititemindex = -1
BindGrid("Category")
end sub
Message #5 by "Minh T. Nguyen" <nguyentriminh@y...> on Sat, 18 May 2002 01:03:03 -0700
|
|
Rich,
Oh no, I usually write code to do the replacement automatically,
so the user doesn't even know that under the hood all his/her
apostrophes are doubled. But again, using the SqlParameters is the
"official" way.
Minh.
-----Original Message-----
From: Rich Lipscomb [mailto:rlipscomb@h...]
Sent: Saturday, May 18, 2002 4:46 AM
To: aspx_beginners
Subject: [aspx_beginners] RE: Datagrid updates only if no apostrophes in
Textbox
Yes, I definitely need to pursue the "SqlParameters" method since I
can't
expect different users to remember to use 2 apostrophes all the time.
I'm
still working on the code below - any ideas on how to get it to work
would
be greatly appreciated - I'm sure I'm overlooking something really
simple.
sub MyDataGrid_Update(sender as object, e as DataGridCommandEventArgs)
Dim sqlstr As String = "UPDATE Ministry SET MinName = @MinName, Category
=
@Category, MinDesc = @MinDesc WHERE min_ID = @Id"
Cmd = New OLEDBCommand(sqlstr, cn)
cmd.Parameters.Add(New OleDbParameter("@Id", OleDbType.Integer, 4))
cmd.Parameters.Add(New OleDbParameter("@MinName", OleDbType.Char, 80))
cmd.Parameters.Add(New OleDbParameter("@Category", OleDbType.Char, 255))
cmd.Parameters.Add(New OleDbParameter("@MinDesc",
OleDbType.LongVarWChar,
4000))
Cmd.Parameters("@Id").Value
MyDataGrid.DataKeys(CInt(E.Item.ItemIndex))
Cmd.Parameters("@MinName").Value
CType(e.Item.FindControl("txtMinName"),
TextBox).Text
Cmd.Parameters("@Category").Value = CType(e.Item.FindControl
("ddlCategory"), DropDownList).SelectedItem.Text
Cmd.Parameters("@MinDesc").Value
CType(e.Item.FindControl("txtMinDesc"),
TextBox).Text
Cmd.Connection.Open()
Cmd.ExecuteNonQuery()
Cmd.Connection.Close()
MyDataGrid.edititemindex = -1
BindGrid("Category")
end sub
--
If you replace all apostrophes with two apostrophes, it will
correctly execute your query and go into the database as one apostrophe.
The other alternative is using SqlParameters, which you have
already started (to which I don't know the answer, though).
Minh.
-----Original Message-----
From: Rich Lipscomb [mailto:rlipscomb@h...]
Sent: Friday, May 17, 2002 5:32 AM
To: aspx_beginners
Subject: [aspx_beginners] Datagrid updates only if no apostrophes in
Textbox
I have a datagrid which renders fine and I can edit, delete, and update
ok, unless there is an apostrophe in one of the textboxes,
e.g., "Men's". "Men" is ok, but "Men's" causes the following when
Cmd.ExecuteNonQuery() fires:
Syntax error (missing operator) in query expression ''Men's'.
Here's the code. Any ideas on
sub MyDataGrid_Update(sender as object, e as DataGridCommandEventArgs)
Dim strname, strCategory, strDesc, sqlstr As String
dim strID as string
cn.open()
strID = MyDataGrid.DataKeys(CInt(e.Item.ItemIndex))
strName = CType(e.Item.FindControl("txtMinName"), TextBox).Text
strCategory = CType(e.Item.FindControl("ddlCategory"),
DropDownList).SelectedItem.Text
strDesc = CType(e.Item.FindControl("txtMinDesc"), TextBox).Text
sqlstr = "UPDATE Ministry SET " & _
"MinName = '" & strName & "', " & _
"Category = '" & strCategory & "', " & _
"MinDesc = '" & strDesc & "' " & _
"WHERE min_ID = " & strID
CMD = New OLEDBCommand(sqlstr, cn)
Cmd.ExecuteNonQuery()
cn.close()
MyDataGrid.edititemindex = -1
BindGrid("Category")
end sub
|
|
 |