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 Myers9 reputationPosted Oct 4, 2017, 7:57 AM EDT
Rajeesh MenothRank #118 · 18.4K reputationPosted Oct 5, 2015, 12:56 AM EDT
Priyaranjan K SRank #45 · 32.5K reputationPosted Oct 5, 2015, 1:03 AM EDT
Vishnu Suresh34 reputationPosted Oct 5, 2015, 2:42 AM EDT
Rajeesh MenothRank #118 · 18.4K reputationPosted Oct 5, 2015, 2:53 AM EDT
Priyaranjan K SRank #45 · 32.5K reputationPosted Oct 5, 2015, 3:36 AM EDT
Vishnu Suresh34 reputationPosted Oct 7, 2015, 12:13 AM EDT
Priyaranjan K SRank #45 · 32.5K reputationPosted Oct 7, 2015, 12:33 AM EDT
Vishnu Suresh34 reputationPosted Oct 7, 2015, 12:56 AM EDT
{
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 SRank #45 · 32.5K reputationPosted Oct 7, 2015, 1:15 AM EDT
Vishnu Suresh34 reputationPosted Oct 7, 2015, 2:13 AM EDT
Priyaranjan K SRank #45 · 32.5K reputationPosted Oct 7, 2015, 2:27 AM EDT
Vishnu Suresh34 reputationPosted Oct 7, 2015, 2:40 AM EDT
Priyaranjan K SRank #45 · 32.5K reputationPosted Oct 7, 2015, 2:51 AM EDT
Vishnu Suresh34 reputationPosted Oct 7, 2015, 3:06 AM EDT
Priyaranjan K SRank #45 · 32.5K reputationPosted Oct 7, 2015, 9:20 AM EDT
Vishnu Suresh34 reputationPosted Oct 7, 2015, 9:42 PM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts