Click here to Skip to main content
15,905,144 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why the error is giving There is no row at position 0.
?

My code is given below


C#
void submit(Object s, EventArgs e)
{
con.Open();
if((File1.PostedFile!=null)&&(File1.PostedFile.ContentLength>0))
{
fn=System.IO.Path.GetFileName(File1.PostedFile.FileName);
fn=txtName.Text.Trim()+fn;
SaveLocation=Server.MapPath("~/Upload/"+fn);
txtPhoto.Text=SaveLocation;
}
else
{
string v=Request.QueryString["value1"];
ds=new DataSet();
string sql="select photo FROM joining where refno='" + v +"' ";
da=new OleDbDataAdapter(sql,con);
da.Fill(ds,"joining");
DataRow dRow=ds.Tables["joining"].Rows[0];
txtPhoto.Text=dRow["photo"].ToString();
}
com=con.CreateCommand();
if(con!=null)
{
com.CommandText="UPDATE joining SET aname = '" + txtName.Text.Trim() + "',fathname='" + txtFather.Text.Trim() + "',dob='" + txtDOB.Text.Trim() + "',pob='" + txtPOB.Text.Trim() +"',add1='" + txtAdd.Value + "',mob='" + txtMob.Text.Trim() + "',city='" + txtCity.Text.Trim() + "',state='" + txtState.Text.Trim() + "',pcode='" + txtPCode.Text.Trim() + "',panno='" + txtPAN.Text.Trim() + "',nominee='" + txtNominee.Text.Trim() + "',relation='" + txtRelation.Text.Trim() + "',photo='" + txtPhoto.Text.Trim() + "',bank='" + txtBank.Text.Trim() + "',branch='" + txtBranch.Text.Trim() + "',account='" + txtAccount.Text.Trim() + "',acctype='" + ddAccType.SelectedItem.Text + "',intby='" + txtIntro.Text.Trim() + "',codno='" + txtCodno.Text.Trim() + "',agent='" + txtACode.Text.Trim() + "',point='" + txtPoints.Text.Trim() + "' where refno='" + txtRefNo.Text.Trim() + "'";
try{int count=com.ExecuteNonQuery();
System.Web.UI.WebControls.Label lbl1=new System.Web.UI.WebControls.Label();
lbl1.ForeColor=System.Drawing.Color.Yellow;
lbl1.BackColor=System.Drawing.Color.Blue;
lbl1.Text = "Your record UPDATED sucessfully";
ph1.Controls.Add(lbl1);
}
catch(Exception ex)
{
Response.Write(ex.Message);
}}
con.Close();

}
Posted
Updated 9-Feb-12 22:32pm
v2
Comments
uspatel 10-Feb-12 4:32am    
Code block added......
MadhuCodePro 10-Feb-12 4:38am    
Are you getting records in select query?
Debug the code to check records in data table.
Janardan Pandey 10-Feb-12 5:06am    
how to debug data table?
MadhuCodePro 10-Feb-12 5:25am    
Insert a break point on below line
DataRow dRow=ds.Tables["joining"].Rows[0];

and move the cursor on to the ds variable.
You will see the find icon on beside of the ds variable.
Just click on the icon you can see the datatable rows.

Hi, Hopefully below code is giving error

C#
DataRow dRow=ds.Tables["joining"].Rows[0];


C#
string sql="select photo FROM joining where refno='" + v +"' "; is not giving any result and you are using directly Rows[0], first check if Rows.Count greater than 0 then write your code.


It will solve your problem.

Don't forget to mark, if it solves your problem. :)
 
Share this answer
 
Comments
Janardan Pandey 10-Feb-12 5:07am    
Thanks....
Error is coming due to no record returned from database table.
You should check first if row is empty or not.
Here is the code:
If (ds.Tables.Count > 0 ){
If (ds.Tables[0].Rows.Count > 0){
DataRow dRow=ds.Tables["joining"].Rows[0];

....
....
}
}
 
Share this answer
 
Comments
Janardan Pandey 10-Feb-12 5:24am    
I solved it but your suggestion more and more comfertable to solve this proble.I will use it.
This error is most likely due to the fact that the SQL statement you constructed does not yield any rows.
C#
string sql="select photo FROM joining where refno='" + v +"' ";


This would yield something like: select photo FROM joining where refno='xxxxxx' where xxxxxx is some suitable value for which there are records in table joining. Caveat: Please do not construct your SQL in such a fashion as it leaves your code vulnerable to SQL injection attacks. Use SQLParameter to parameterize your SQL statements. This will be of added benfit as you will not have to deal with proper formatting and escaping stuff.

Regards,

Manfred
 
Share this answer
 

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