Hi akhter,
Refer below sample code.
You need to pass the RId to the Insert method as per your code.
HTML
<asp:Label ID="lblRId" runat="server" Text="1" />
Cheque Amount<asp:TextBox ID="chqamount" runat="server" Text="500"></asp:TextBox>
<br />
Cheque No :<asp:TextBox ID="chequno" runat="server" Text="123"></asp:TextBox>
<asp:Button ID="btn_Adj" runat="server" Text="Adjust_Amount" OnClick="AdjustAmount" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText=" ID">
<ItemTemplate>
<asp:Label ID="I_ID" runat="server" Text='<%#Bind("I_ID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=" Amount">
<ItemTemplate>
<asp:Label ID="I_Amt" runat="server" Text='<%#Bind("I_Amt")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cheque_NO">
<ItemTemplate>
<asp:TextBox ID="C_NO" runat="server" ReadOnly="true" Text='0'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=" Cheque_Amount">
<ItemTemplate>
<asp:TextBox ID="C_Amt" runat="server" ReadOnly="true" Text='0'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:DropDownList ID="ddlStatus" runat="server">
<asp:ListItem Text="Pending" Value="1" Selected="True"></asp:ListItem>
<asp:ListItem Text="Received" Value="2" />
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnSave" Text="Save" runat="server" OnClick="OnSave" />
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.AddRange(new System.Data.DataColumn[2] {
new System.Data.DataColumn("I_ID", typeof(int)),
new System.Data.DataColumn("I_Amt", typeof(int)) });
dt.Rows.Add(21, 339);
dt.Rows.Add(22, 622);
dt.Rows.Add(23, 226);
dt.Rows.Add(26, 339);
dt.Rows.Add(27, 339);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void AdjustAmount(object sender, EventArgs e)
{
int chequeAmount = Convert.ToInt32(chqamount.Text);
foreach (GridViewRow row in GridView1.Rows)
{
int id = Convert.ToInt32((row.FindControl("I_ID") as Label).Text);
int amount = Convert.ToInt32((row.FindControl("I_Amt") as Label).Text);
TextBox txtCheque = row.FindControl("C_NO") as TextBox;
TextBox txtAmount = row.FindControl("C_Amt") as TextBox;
DropDownList ddl = row.FindControl("ddlStatus") as DropDownList;
if (chequeAmount >= amount)
{
txtCheque.Text = chequno.Text;
txtAmount.Text = amount.ToString();
chequeAmount = chequeAmount - amount;
}
else if (chequeAmount < amount && chequeAmount > 0)
{
txtCheque.Text = chequno.Text;
txtAmount.Text = chequeAmount.ToString();
chequeAmount = 0;
}
else
{
txtCheque.Text = "0";
txtAmount.Text = "0";
}
ddl.ClearSelection();
if (amount == Convert.ToInt32(txtAmount.Text))
{
ddl.Items.FindByText("Received").Selected = true;
}
else
{
ddl.Items.FindByText("Pending").Selected = true;
}
}
}
protected void OnSave(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
int id = Convert.ToInt32((row.FindControl("I_ID") as Label).Text);
int amount = Convert.ToInt32((row.FindControl("I_Amt") as Label).Text);
TextBox txtCheque = row.FindControl("C_NO") as TextBox;
TextBox txtAmount = row.FindControl("C_Amt") as TextBox;
DropDownList ddl = row.FindControl("ddlStatus") as DropDownList;
if (!string.IsNullOrEmpty(txtCheque.Text) && !string.IsNullOrEmpty(txtAmount.Text))
{
Insert(Convert.ToInt32(lblRId.Text), id, Convert.ToInt32(txtCheque.Text), Convert.ToInt32(txtAmount.Text), ddl.SelectedItem.Text);
}
}
}
private void Insert(int rID, int iID, int chequeNo, int chequeAmount, string status)
{
string query = "INSERT INTO tbl_Received VALUES(@RID, @IId, @ChequeNo, @ChequeAmount, @Status, @RDate)";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@RID", rID);
cmd.Parameters.AddWithValue("@IId", iID);
cmd.Parameters.AddWithValue("@ChequeNo", chequeNo);
cmd.Parameters.AddWithValue("@ChequeAmount", chequeAmount);
cmd.Parameters.AddWithValue("@Status", status);
cmd.Parameters.AddWithValue("@RDate", DateTime.Now);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dt As System.Data.DataTable = New System.Data.DataTable()
dt.Columns.AddRange(New System.Data.DataColumn(1) {
New System.Data.DataColumn("I_ID", GetType(Integer)),
New System.Data.DataColumn("I_Amt", GetType(Integer))})
dt.Rows.Add(21, 339)
dt.Rows.Add(22, 622)
dt.Rows.Add(23, 226)
dt.Rows.Add(26, 339)
dt.Rows.Add(27, 339)
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub AdjustAmount(ByVal sender As Object, ByVal e As EventArgs)
Dim chequeAmount As Integer = Convert.ToInt32(chqamount.Text)
For Each row As GridViewRow In GridView1.Rows
Dim id As Integer = Convert.ToInt32((TryCast(row.FindControl("I_ID"), Label)).Text)
Dim amount As Integer = Convert.ToInt32((TryCast(row.FindControl("I_Amt"), Label)).Text)
Dim txtCheque As TextBox = TryCast(row.FindControl("C_NO"), TextBox)
Dim txtAmount As TextBox = TryCast(row.FindControl("C_Amt"), TextBox)
Dim ddl As DropDownList = TryCast(row.FindControl("ddlStatus"), DropDownList)
If chequeAmount >= amount Then
txtCheque.Text = chequno.Text
txtAmount.Text = amount.ToString()
chequeAmount = chequeAmount - amount
ElseIf chequeAmount < amount AndAlso chequeAmount > 0 Then
txtCheque.Text = chequno.Text
txtAmount.Text = chequeAmount.ToString()
chequeAmount = 0
Else
txtCheque.Text = "0"
txtAmount.Text = "0"
End If
ddl.ClearSelection()
If amount = Convert.ToInt32(txtAmount.Text) Then
ddl.Items.FindByText("Received").Selected = True
Else
ddl.Items.FindByText("Pending").Selected = True
End If
Next
End Sub
Protected Sub OnSave(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In GridView1.Rows
Dim id As Integer = Convert.ToInt32((TryCast(row.FindControl("I_ID"), Label)).Text)
Dim amount As Integer = Convert.ToInt32((TryCast(row.FindControl("I_Amt"), Label)).Text)
Dim txtCheque As TextBox = TryCast(row.FindControl("C_NO"), TextBox)
Dim txtAmount As TextBox = TryCast(row.FindControl("C_Amt"), TextBox)
Dim ddl As DropDownList = TryCast(row.FindControl("ddlStatus"), DropDownList)
If Not String.IsNullOrEmpty(txtCheque.Text) AndAlso Not String.IsNullOrEmpty(txtAmount.Text) Then
Insert(Convert.ToInt32(lblRId.Text), id, Convert.ToInt32(txtCheque.Text), Convert.ToInt32(txtAmount.Text), ddl.SelectedItem.Text)
End If
Next
End Sub
Private Sub Insert(ByVal rID As Integer, ByVal iID As Integer, ByVal chequeNo As Integer, ByVal chequeAmount As Integer, ByVal status As String)
Dim query As String = "INSERT INTO tbl_Received VALUES(@RID, @IId, @ChequeNo, @ChequeAmount, @Status, @RDate)"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query)
cmd.Parameters.AddWithValue("@RID", rID)
cmd.Parameters.AddWithValue("@IId", iID)
cmd.Parameters.AddWithValue("@ChequeNo", chequeNo)
cmd.Parameters.AddWithValue("@ChequeAmount", chequeAmount)
cmd.Parameters.AddWithValue("@Status", status)
cmd.Parameters.AddWithValue("@RDate", DateTime.Now)
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub