I have a .net program in which datas are to be inserted through gridview. If I am adding the same data twice then I need to show error message that data repeated. How can I do this. I have used one code but it shows error.
for (int i = 0; i < GridView1.Rows.Count; i++)
{
TextBox txtSlno = (TextBox)GridView1.Rows[i].Cells[1].FindControl("txtSlno");
if (txtSlno.Text != "" || txtSlno.Text != null)
{
if (txtSlno.Text == dt.Rows[i]["SlNo"].ToString())
{
Label1.Text = "already exist";
break;
}
else
{
Label1.Text = "";
}
}
}
It shows error there is no row at position zero.
Dennis MyersPosted Oct 4, 2017, 11:57 AM
Vishnu SureshPosted Oct 8, 2015, 1:42 AM
Priyaranjan K SPosted Oct 7, 2015, 1:20 PM
Vishnu SureshPosted Oct 7, 2015, 7:06 AM
Priyaranjan K SPosted Oct 7, 2015, 6:51 AM
Vishnu SureshPosted Oct 7, 2015, 6:40 AM
Priyaranjan K SPosted Oct 7, 2015, 6:27 AM
Vishnu SureshPosted Oct 7, 2015, 6:13 AM
Priyaranjan K SPosted Oct 7, 2015, 5:15 AM
Vishnu SureshPosted Oct 7, 2015, 4:56 AM
{
Control control = null;
if (GridView1.FooterRow != null)
{
control = GridView1.FooterRow;
}
else
{
control = GridView1.Controls[0].Controls[0];
}
string SlNo = (control.FindControl("txtSlNo") as TextBox).Text;
string Code = (control.FindControl("txtcode") as TextBox).Text;
string details = (control.FindControl("txtdetails") as TextBox).Text;
string Qty = (control.FindControl("txtqty") as TextBox).Text;
string UPrice = (control.FindControl("txtuprice") as TextBox).Text;
using (SqlConnection con = new SqlConnection("Data Source=xxxxx;Initial Catalog=xxxxx;User ID=xx;Password=xxxxx"))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO [qtable] VALUES(@Code,@details,@Qty,@UPrice)";
cmd.Parameters.AddWithValue("@SlNo", SlNo);
cmd.Parameters.AddWithValue("@Code", Code);
cmd.Parameters.AddWithValue("@details", details);
cmd.Parameters.AddWithValue("@Qty", Qty);
cmd.Parameters.AddWithValue("@UPrice", UPrice);
con.Open();
cmd.ExecuteNonQuery();
GridView1.DataBind();
BindData();
con.Close();
}
Priyaranjan K SPosted Oct 7, 2015, 4:33 AM
Vishnu SureshPosted Oct 7, 2015, 4:13 AM
Priyaranjan K SPosted Oct 5, 2015, 7:36 AM
Rajeesh MenothPosted Oct 5, 2015, 6:53 AM
Vishnu SureshPosted Oct 5, 2015, 6:42 AM
Priyaranjan K SPosted Oct 5, 2015, 5:03 AM
Rajeesh MenothPosted Oct 5, 2015, 4:56 AM