TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Aktham Mahmoud
NA
720
37.8k
what is a first a priority?
Jul 8 2020 11:55 PM
Hi
I've GrideView has (CRUD) operation using SQLDATASOURCE tool to connect with table, I'm trying to update some columns by rowcommand , but still updating get the value from controls in gridview.
See that code below
SQLDATACOURCE in code:
<asp:SqlDataSource ID=
"SqlDataSource2"
runat=
"server"
ConnectionString=
"<%$ ConnectionStrings:ConnectionString %>"
DeleteCommand=
"DELETE FROM [Products] WHERE [Id] =@Id"
SelectCommand=
"SELECT [Id], [P_Name] WHERE ([Id] = @Id)"
UpdateCommand=
"UPDATE [Products] SET [P_Name] = @P_Name WHERE [Id] = @Id"
>
<DeleteParameters>
<asp:Parameter Name=
"Id"
Type=
"Int32"
/>
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name=
"P_Name"
Type=
"String"
/>
<asp:Parameter Name=
"Id"
Type=
"Int32"
/>
</UpdateParameters>
</asp:SqlDataSource>
GRIDVIEW Code
<asp:GridView ID=
"GridView1"
runat=
"server"
AutoGenerateColumns=
"False"
DataKeyNames=
"Id"
DataSourceID=
"SqlDataSource2"
EmptyDataText=
"No Products in Selected Category"
OnRowCommand=
"GridView1_RowCommand"
>
<Columns>
<asp:TemplateField HeaderText=
"Name"
SortExpression=
"P_Name"
>
<EditItemTemplate>
<asp:TextBox ID=
"TB_NameE"
runat=
"server"
Text=
'<%# Bind("P_Name") %>'
></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=
"Label4"
runat=
"server"
Text=
'<%# Eval("P_Name") %>'
></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID=
"LinkButton1"
runat=
"server"
CausesValidation=
"False"
class
=
"btn btn-outline-success"
CommandName=
"Edit"
Text=
"Edit"
></asp:LinkButton>
<asp:LinkButton ID=
"btndelete"
runat=
"server"
CausesValidation=
"False"
class
=
"btn btn-outline-danger"
CommandName=
"Delete"
OnClientClick=
"return confirm('Are you sure you want to delete this event?')"
Text=
"Delete"
></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID=
"LinkButton1"
runat=
"server"
CausesValidation=
"True"
class
=
"btn btn-outline-primary"
CommandName=
"Update"
Text=
"Update"
></asp:LinkButton>
<asp:LinkButton ID=
"LinkButton2"
runat=
"server"
CausesValidation=
"False"
class
=
"btn btn-outline-warning"
CommandName=
"Cancel"
Text=
"Cancel"
></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind code :
protected
void
GridView1_RowCommand(
object
sender, GridViewCommandEventArgs e)
{
using
(SqlConnection sqlcon =
new
SqlConnection(connectionString))
if
(e.CommandName ==
"Update"
)
{
try
{
GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
int
rowid = gvr.RowIndex;
//Open Connection with D.B
sqlcon.Open();
string
query =
"UPDATE Products SET P_Name = @P_Name WHERE (Id = @Id)"
;
{
SqlCommand sqlcmd =
new
SqlCommand(query, sqlcon);
sqlcmd.Parameters.AddWithValue(
"@P_Name"
,
"A1"
);
//=============================================
//I Disabled that row just to check if update table is working by add last value "A1", but still get it from textbox
//sqlcmd.Parameters.AddWithValue("@P_Name", (GridView1.Rows[rowid].FindControl("TB_NameE") as TextBox).Text);
sqlcmd.Parameters.AddWithValue(
"@Id"
, Convert.ToInt32(GridView1.DataKeys[rowid].Value.ToString()));
sqlcmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
GridView1.DataBind();
}
}
//End Try
catch
(Exception ex)
{
LBgvFi.Text = ex.Message;
}
}
}
my question is: what is a first a priority?
1) textbox has DataBind("Name");
Or
2) Codebehind in c#
but why is still update a column by Textbox value. Until if I implement it by rowcommand event.
Thanks
Reply
Answers (
3
)
UserManager and SignInManager not run correctly
Looking for free Database for hosting on cloud