2
Answers

inserting data into database by skipping duplicate values .

hai friends,
 
please let me know how to insert values into database with out repetition..  that means skipping the already entered values from datagridview..
 
 
here is my  code  
 
 
AD_ YEA R CAT BRANCH REGD_ NO STU DEN T YEA R GE NDE R CAS TE
2013 A CSE 132Z1A0501 VENU FIRST MALE BCB
2013 A CSE 132Z1A0502 NAVEEN FIRST MALE BCB
2013 A CSE 132Z1A0503 TANUSHREE FIRST FEMALE BCB
2013 A CSE 132Z1A0504 TANUSHKA FIRST FEMALE BCB
  the red color indicated in the table was already entered so while updating to database these entered values should not enter again.
i am uploading excel data into datagridview. from datagridview  inserting data to database 
 
 
con.Open();
int c = dataGridView1.Rows.Count;
int d = c - 1;
MessageBox.Show(dataGridView1.Rows.Count.ToString());
try
{
if (dataGridView1.Rows.Count >= 1)
{
for (int i = 0; i <= d; i++)
{
textBox3.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();//ADMIT YEAR
textBox4.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();//CAT
textBox5.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();//COURSE
textBox6.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();//REG_NO
textBox7.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();//NAME
textBox8.Text = dataGridView1.Rows[i].Cells[5].Value.ToString();//year
textBox9.Text = dataGridView1.Rows[i].Cells[6].Value.ToString();
textBox10.Text = dataGridView1.Rows[i].Cells[7].Value.ToString();
 
 
 
try
{
SqlCommand CMD = new SqlCommand("SELECT COUNT(YEAR) as Tot FROM ADD_STUDENT WHERE REGD_NO='" + textBox6.Text + "' AND YEAR='" + textBox8.Text + "' ", con);
SqlDataReader dr = CMD.ExecuteReader();
while (dr.Read())
{
int i1 = Convert.ToInt32(dr["Tot"]);
if (i1 > 0)
{
MessageBox.Show(textBox6.Text + "REGD NO ALREADY ENTERED");
}
else
{
SqlDataAdapter adapter = new SqlDataAdapter();
string sql = null;
sql = "insert into product (REGD_NO) values('"+ textBox6.Text +"')";
try
{
con.Open();
adapter.InsertCommand = new SqlCommand(sql, con );
adapter.InsertCommand.ExecuteNonQuery();
MessageBox.Show("Row inserted !! ");
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}// END OF TRY STATEMENT
catch (Exception ex)
{
MessageBox.Show(ex.Message, "error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
}
finally
{
con.Close();
}
 
 
 am getting error message like 
 
executereader requires an open and available connection. the connection's current state is open 
 
 
 
 
 
 
 
 
 

Answers (2)

2
Photo of Nitin Sontakke
NA 11.7k 2.2k 7y
To begin with in your Page_Load event, add the following WHERE clause:
 
WHERE [Pid] = 0 
Accepted
2
Photo of Nitin Sontakke
NA 11.7k 2.2k 7y
Here is a modified and perfectly working code...
 
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.   if (!this.IsPostBack)  
  4.   {  
  5.     DataTable dt = this.GetData("SELECT cId, cName FROM TopBill where [Pid] = 0");  
  6.     this.PopulateTreeView(dt, null);  
  7.   }  
  8. }  
  9.   
  10. private void PopulateTreeView(DataTable dtParent, TreeNode treeNode)  
  11. {  
  12.   foreach (DataRow row in dtParent.Rows)  
  13.   {  
  14.     TreeNode child = new TreeNode  
  15.     {  
  16.       Text = row["cName"].ToString(),  
  17.       Value = row["cId"].ToString()  
  18.     };  
  19.     if (treeNode == null)  
  20.       TreeView1.Nodes.Add(child);  
  21.     else  
  22.       treeNode.ChildNodes.Add(child);  
  23.     DataTable dtChild = this.GetData("SELECT srNo,cid, cName FROM TopBill WHERE pid = " + child.Value);  
  24.     if (dtChild.Rows.Count != 0)  
  25.       PopulateTreeView(dtChild, child);  
  26.   }  
  27. }  
Bihar might appear at wrong position, but that is because of your data and not code.
 
0
Photo of Nitin Sontakke
NA 11.7k 2.2k 7y
Where is the code?
 
0
Photo of Vivek Kumar Vishwas
NA 117 5.1k 7y
Sir, its reallry working fine Thank You so much.
Now what my requirment it to add or calculate the value from treeview nodes.
See the below picture..
 
Now suppose sir, i want to show CQty   value with each cid and i also wanto calculate total value of cqty.  How to do this.
 
Let me clear once again.
 
Supoose that there  is a one product(Pen- and its cid is 10), pen contains with it self like Cap, Refil, Ink, color, and so on.
 
the the properties of pen contains there cqty Value, so how do we find that what cqty is required to make 1 Pen.
 
i hope i cleared my  requirments.
Help me sir... 
 
 
0
Photo of Vivek Kumar Vishwas
NA 117 5.1k 7y
 Sir, Its working good till this level and showing result like below.
 
 Now suppose that  i also added some value under Preet vihar as showing in below picture.
 
 
 
 Now how do we make preet vihar as a parent for its both child node as you can see in the above picture and its not the last stage of tree view it can of n level of hierarchy.
 
 please check and help me out from this problem.
 
 
 
 
 
 
0
Photo of Vivek Kumar Vishwas
NA 117 5.1k 7y
it will only bind only parent Node not its child node. I have tried it many times.May you please download my files and check the process of creating Treeview nodes from Parent to its child at n level hierarchy.