Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
im new to .Net this is the code which my friend gave for updating a table in sql database using c#,asp.net code:
C#
{
  string dpt = (string)Session["deptmnt"];
  string con = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
                SqlConnection con1 = new SqlConnection(con);
                con1.Open();
 string qry = @"select * from faculty where ((fid like '%" + dpt + "%') and (fid='"+Label17.Text+"'))";
                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = new SqlCommand(qry, con1);
                    
                    string upd = @"update faculty SET fname = '" + TextBox7.Text + "',gender='" + DropDownList10.SelectedValue + "', dept = '" + DropDownList5.SelectedValue + "', maxbooks = '" + DropDownList8.SelectedValue + "' WHERE fid = '" + Label17.Text + "'";
                                      SqlCommandBuilder cb = new SqlCommandBuilder(da);
                    DataSet ds = new DataSet();
                    da.Fill(ds, "faculty");


                    DataTable dt = ds.Tables["faculty"];
                    dt.Rows[1]["fname"] = TextBox7.Text;
                    dt.Rows[1]["gender"] = DropDownList10.SelectedValue;
                    dt.Rows[1]["dept"] = DropDownList5.SelectedValue;
                    dt.Rows[1]["maxbooks"] = DropDownList8.SelectedValue;

                    SqlCommand cmd = new SqlCommand(upd, con1);
                    da.UpdateCommand = cmd;

                    da.Update(ds, "faculty");

                    con1.Close();
}

when i debug the code,the control does not go after " dt.Rows[1]["fname"] = TextBox7.Text;"
why is it so? Im unable to find the reason.,what does dt.Rows[number] stand for? does it indicate the row number or what it is? plz help
Posted
Updated 3-Aug-13 21:24pm
v4

1 solution

Change it as :
C#
dt.Rows[0]["fname"] = TextBox7.Text;
dt.Rows[0]["gender"] = DropDownList10.SelectedValue;
dt.Rows[0]["dept"] = DropDownList5.SelectedValue;
dt.Rows[0]["maxbooks"] = DropDownList8.SelectedValue;
 
Share this answer
 
Comments
sundaram meenakshi 4-Aug-13 3:17am    
@Kuthuparakkal thank you soo much it worked superb.i have been trying for many hours,thank you.but,what does the 0 indicate can u explain?
ridoy 4-Aug-13 3:19am    
It indicates first row of your datatable.
Kuthuparakkal 4-Aug-13 3:20am    
Datatable/Rows/Columns etc have zero based indices, 0 first row, 1 second row etc.. In your case, there wasnt any second row so it fails there..
sundaram meenakshi 4-Aug-13 3:20am    
can u plz tell if it indicates the first row,how the row specified in the where condition is identified?
Kuthuparakkal 4-Aug-13 3:23am    
I dont understand yr question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900