Hi narendramalpu...,
Since you are binding the GridView from offer_register table so i am adding the Status column in the offer_register table with default value 0.
So when use clicks button updating the Status column so it will reflect in the GridView.
You can change as per your database structure and logic.
Refer below code.
SQL
ALTER TABLE offer_register
ADD [Status] INT NOT NULL DEFAULT(0)
GO
HTML
<center>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
Height="181px" Width="1300px" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4"
CellSpacing="2" ForeColor="Black" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:TemplateField HeaderText="No.">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="id" />
<asp:BoundField DataField="first_name" HeaderText="First Name" />
<asp:BoundField DataField="last_name" HeaderText="Last Name" />
<asp:BoundField DataField="pick_up" HeaderText="Pick UP" />
<asp:BoundField DataField="address_pick" HeaderText="Address Pick Up" />
<asp:BoundField DataField="drop_off" HeaderText="Drop Off " />
<asp:BoundField DataField="address_drop" HeaderText="Address Drop Off " />
<asp:BoundField DataField="when_going" HeaderText="Date" />
<asp:BoundField DataField="time" HeaderText="Time" />
<asp:BoundField DataField="passenger" HeaderText="Seat" />
<asp:BoundField DataField="seat_price" HeaderText="Price" />
<asp:BoundField DataField="mobile_number" HeaderText="Mobile Number" />
<asp:BoundField DataField="car_name" HeaderText="Car Name" />
<asp:BoundField DataField="car_number" HeaderText="Car Number" />
<asp:TemplateField>
<ItemTemplate>
<asp:textbox runat="server" ID="txtSeat" AutoPostBack="true" OnTextChanged="txtSeat_TextChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" CommandName="Select" Text="Continue" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
</center>
<br />
<asp:Label ID="Label1" runat="server" Text="" Visible="false"></asp:Label>
<asp:Label ID="Label2" runat="server" Text="" Visible="false"></asp:Label>
<asp:Label ID="Label3" runat="server" Text="" Visible="false"></asp:Label>
<asp:Label ID="Label4" runat="server" Text="" Visible="false"></asp:Label>
<asp:Label ID="Label5" runat="server" Text="" Visible="false"></asp:Label>
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Label1.Text = Session["first_name"].ToString();
Label2.Text = Session["email_id"].ToString();
Label3.Text = Session["last_name"].ToString();
Label4.Text = Session["address"].ToString();
Label5.Text = Session["mobile"].ToString();
string id = Request.QueryString["Id"];
if (!string.IsNullOrEmpty(id))
{
string str = ConfigurationManager.ConnectionStrings["carpool_connection"].ConnectionString;
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("select * from offer_register WHERE id = @Id", con);
cmd.Parameters.AddWithValue("@id", id);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
con.Close();
}
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string status = DataBinder.Eval(e.Row.DataItem, "Status").ToString();
if (status == "1")
{
(e.Row.Cells[16].Controls[0] as Button).Enabled = false;
}
if (status == "0")
{
(e.Row.Cells[16].Controls[0] as Button).Enabled = true;
}
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow g1 = GridView1.SelectedRow;
string str = ConfigurationManager.ConnectionStrings["carpool_connection"].ConnectionString;
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("insert into bookride(id,first_na,last_na,pick_u,address_pi,drop_off,address_drop,when_going,time,passenger,price,mobile_number,car_na,car_nu,name,pass_email,las_name,address1,mobile) values ('" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "','" + g1.Cells[4].Text + "','" + g1.Cells[5].Text + "','" + g1.Cells[6].Text + "','" + g1.Cells[7].Text + "','" + g1.Cells[8].Text + "','" + g1.Cells[9].Text + "','" + g1.Cells[10].Text + "','" + g1.Cells[11].Text + "','" + g1.Cells[12].Text + "','" + g1.Cells[13].Text + "','" + g1.Cells[14].Text + "','" + Label1.Text + "','" + Label2.Text + "','" + Label3.Text + "','" + Label4.Text + "','" + Label5.Text + "')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
// Update Status to 1 to identify user clicked the Button once.
cmd = new SqlCommand("UPDATE offer_register SET STATUS = 1 WHERE id = @Id", con);
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@id", g1.Cells[1].Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Request Sent.')", true);
Response.Redirect(Request.Url.AbsoluteUri);
}
protected void txtSeat_TextChanged(object sender, EventArgs e)
{
GridViewRow row = (sender as TextBox).NamingContainer as GridViewRow;
if (!string.IsNullOrEmpty(row.Cells[10].Text))
{
int passengerSeat = Convert.ToInt32(row.Cells[10].Text);
int userSeat = Convert.ToInt32((sender as TextBox).Text);
if (userSeat > passengerSeat)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('User seat must not greater than passenger seat.')", true);
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
Label1.Text = Session("first_name").ToString()
Label2.Text = Session("email_id").ToString()
Label3.Text = Session("last_name").ToString()
Label4.Text = Session("address").ToString()
Label5.Text = Session("mobile").ToString()
Dim id As String = Request.QueryString("Id")
If Not String.IsNullOrEmpty(id) Then
Dim str As String = ConfigurationManager.ConnectionStrings("carpool_connection").ConnectionString
Dim con As SqlConnection = New SqlConnection(str)
Dim cmd As SqlCommand = New SqlCommand("select * from offer_register WHERE id = @Id", con)
cmd.Parameters.AddWithValue("@id", id)
con.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
GridView1.DataSource = reader
GridView1.DataBind()
con.Close()
End If
End If
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim status As String = DataBinder.Eval(e.Row.DataItem, "Status").ToString()
If status = "1" Then
TryCast(e.Row.Cells(16).Controls(0), Button).Enabled = False
End If
If status = "0" Then
TryCast(e.Row.Cells(16).Controls(0), Button).Enabled = True
End If
End If
End Sub
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim g1 As GridViewRow = GridView1.SelectedRow
Dim str As String = ConfigurationManager.ConnectionStrings("carpool_connection").ConnectionString
Dim con As SqlConnection = New SqlConnection(str)
Dim cmd As SqlCommand = New SqlCommand("insert into bookride(id,first_na,last_na,pick_u,address_pi,drop_off,address_drop,when_going,time,passenger,price,mobile_number,car_na,car_nu,name,pass_email,las_name,address1,mobile) values ('" & g1.Cells(1).Text & "','" + g1.Cells(2).Text & "','" + g1.Cells(3).Text & "','" + g1.Cells(4).Text & "','" + g1.Cells(5).Text & "','" + g1.Cells(6).Text & "','" + g1.Cells(7).Text & "','" + g1.Cells(8).Text & "','" + g1.Cells(9).Text & "','" + g1.Cells(10).Text & "','" + g1.Cells(11).Text & "','" + g1.Cells(12).Text & "','" + g1.Cells(13).Text & "','" + g1.Cells(14).Text & "','" + Label1.Text & "','" + Label2.Text & "','" + Label3.Text & "','" + Label4.Text & "','" + Label5.Text & "')", con)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
cmd = New SqlCommand("UPDATE offer_register SET STATUS = 1 WHERE id = @Id", con)
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("@id", g1.Cells(1).Text)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('Request Sent.')", True)
Response.Redirect(Request.Url.AbsoluteUri)
End Sub
Protected Sub txtSeat_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = TryCast((TryCast(sender, TextBox)).NamingContainer, GridViewRow)
If Not String.IsNullOrEmpty(row.Cells(10).Text) Then
Dim passengerSeat As Integer = Convert.ToInt32(row.Cells(10).Text)
Dim userSeat As Integer = Convert.ToInt32((TryCast(sender, TextBox)).Text)
If userSeat > passengerSeat Then
ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('User seat must not greater than passenger seat.')", True)
End If
End If
End Sub