ASP.NET - Qry Err

Asked By Rahul
10-Feb-12 12:54 AM

I have qry err in this:

cmd=new SqlCommand("update agentoffer set fromdate='"+txfrdate.Text+"',todate='"+txtodate.Text+"', title='"+txtitle.Text+"', description='"+txdescription.Text+"', category='"+txradiocategory.SelectedItem.Text+"'  where id='"+lbid.Text+"'",con); //180 line

it was not update correctly it shows err:

Error:System.NullReferenceException: Object reference not set to an instance of an object. at offers.GridView1_RowCommand(Object sender, GridViewCommandEventArgs e) in d:\tour\offers.aspx.cs:line 180

here category is radiobuttonlist.
i inserted in agentoffer table  and i have select member radiobtnlist means it want to add that data automatically in member table how ?

send me qry ?

  Web Star replied to Rahul
10-Feb-12 01:37 AM
Query is ok you need to check the parameter value which are passing with the query any one having null referance that's why you are getting error so you need to check your code in debug mode query will be created correctly with textbox and rediobutton value.
  dipa ahuja replied to Rahul
10-Feb-12 06:00 AM
Its advisable to take the parameters when parameter no. are more than 2. This type of queries always confuse you and create problems of single quotes and many other

change your query format to this:

UPDATE Query

 
private void button1_Click(object sender, EventArgs e)
{
     SqlConnection connect = new SqlConnection(ConnString);
     connect.Open();
 
     string q = "Update people set name=@name where id=@id";
 
     SqlCommand comm = new SqlCommand(q, connect);
     comm.Parameters.AddWithValue("name", txtname.Text);
     comm.Parameters.AddWithValue("id", int.Parse(txtid.Text.ToString()));
     comm.ExecuteNonQuery();
     connect.Close();
 
}
 

Create New Account
help
selected Error Error:System.ArgumentOutOfRangeException: 'DropDownList2' has a SelectedIndex which is invalid because it does not exist in value) at System.Web.UI.WebControls.DropDownList.set_SelectedIndex(Int32 value) at _Default.GridView1_RowCommand(Object sender, GridViewCommandEventArgs e) in d: \ maddy \ editupdate \ Default.aspx.cs:line 190 if (e.CommandName = = "edt" ) { ImageButton SelectedIndex = dr[2].ToString(); / / DropDownList2.SelectedItem.Value.ToString() = = ""; DropDownList2.SelectedIndex = Convert .ToInt16(dr[2].ToString()); / / error txthotelname.Text = dr[3].ToString(); dr.Close(); } else { dr.Close(); } As error it self told you the selectedindex of DropDownList2 is invalid it means the index you at 5 index than you can't select that one so you are getting such error in your code DropDownList2.SelectedIndex = Convert .ToInt16(dr[2].ToString()); / / error So make sure the item should be bound at that index before selection. hope this if it is giving more than available index in dropDownList then you will get outofIndex error. ok how to check this error ? first check dr[2].ToString() ; what is returning . . . and
Error on update qry ? kindly tel me this pls ? it shows this error ? Error:System.NullReferenceException: Object reference not set to an instance of an object. at creditadd.GridView1_RowCommand(Object sender, GridViewCommandEventArgs e) in d: \ tour \ creditadd.aspx.cs:line 298 .style2 { width: 18%; height: 39px; } .style3 Parameters.Add(param); / / to this executing correctly below this is not executing correctly it shows error ? / / cmd = new SqlCommand("update creditadd set total = @total, @total = @total + credit where cid = '" + lbid.Text set total = @total, @total = @total + credit where userid = '" + txusrid.Text + "' and username = '" + txusrname.Text + "'" , con); error it shows this line ? cmd.ExecuteNonQuery(); Response.Write( "<script> alert(Running Total Updated Successfully');< / script> " ); GridView1.EditIndex = -1; bindgrid(); } You can
Error on asp.net I have error in this codings? Error:System.NullReferenceException: Object reference not set to an instance of an object. at creditadd.GridView1_RowCommand(Object sender, GridViewCommandEventArgs e) in d: \ tour \ creditadd.aspx.cs:line 265 else if (e.CommandName = = "upd" ) { LinkButton param.DbType = DbType.Decimal; param.SqlDbType = SqlDbType .Float; param.Value = 0; cmd.Parameters.Add(param); / / error cmd = new SqlCommand ( "update creditadd set total = @total, @total = @total + credit where userid = '" + txusrid.Text + "' and username = '" + txusrname.Text + "'" , con); cmd.ExecuteNonQuery(); / / cmd.Parameters.Add(param); / / cmd.Parameters.Add("@total", SqlDbType.Decimal, 10).Value = 0; / / param.DbType GridView1.EditIndex = -1; bindgrid(); } now i write codings like this means it shows this type error ? what i do tel any one perfectly ? Error:System.Data.SqlClient.SqlException: Must declare the
Object Reference Error:System.NullReferenceException: Object reference not set to an instance of an object. at cal.CalculateRunningtotal() in d: \ maddy \ editupdate \ cal.aspx.cs:line 201 at cal.GridView1_RowCommand(Object sender, GridViewCommandEventArgs e) in d: \ maddy \ editupdate \ cal.aspx.cs:line 155 protected void GridView1_RowCommand( object sender GridViewCommandEventArgs e) { try { if (con.State = = ConnectionState .Closed) { con.Open(); } if (e.CommandName = = "can" ) { GridView1.EditIndex update tbl_cal set amount = '" + txamount.Text + "', total = '" +txtotal.Text+ "' where id = '" + lbid.Text + "'" , con); cmd.ExecuteNonQuery(); GridView1.EditIndex = -1; ShowAlertMessage( "Updated Successfully" ); bindgrid(); / / CalculateRunningtotal(); } else if (e.CommandName = = "del" ) { LinkButton lnkdel row.FindControl( "lblid" ); cmd = new SqlCommand ( "delete from tbl_cal where id = '" + lid.Text + "'" , con); cmd.ExecuteNonQuery(); ShowAlertMessage( "Deleted Successfully" ); bindgrid(); } } catch ( Exception ex) { Response.Write( "Error:" + ex.ToString()); } finally { if (con.State = = ConnectionState .Open) { con.Close(); } } } public void CalculateRunningtotal() { int prevvalue lbltotal" ); prevvalue = prevvalue + Convert .ToInt32(lbamount.Text); lbtotal.Text = prevvalue.ToString(); } } In Most cases this error comes when an object is trying to access a null object. so to access the
total = @total, @total = @total + credit where userid = '" + TextBox1.Text + "' and username = '" + TextBox2.Text + "'" , con); cmd.ExecuteNonQuery(); how to insert this into this row command ? protected void GridView1_RowCommand( object sender, GridViewCommandEventArgs e) { try { if (con.State = = ConnectionState .Closed) { con.Open(); } if (e.CommandName = = "can" ) { GridView1.EditIndex row.FindControl( "lblid" ); cmd = new SqlCommand ( "delete from creditadd where cid = '" + lid.Text + "'" , con); cmd.ExecuteNonQuery(); Response.Write( "<script> alert('Deleted Successfully');< / script> " ); / / Response.Write("Deleted Successfully"); bindgrid(); dropbind(); } } catch ( Exception ex) { Response.Write( "Error:" + ex.ToString()); } finally { if (con.State = = ConnectionState .Open) { con.Close(); } } } You required to insert that total, @total = @total + credit where userid = '" + TextBox1.Text + "' and username = '" + TextBox2.Text + "'" , con); } shows this error ? Error:System.NullReferenceException: Object reference not set to an instance of an object. at creditadd.GridView1_RowCommand(Object sender, GridViewCommandEventArgs e) in d: \ tour \ creditadd.aspx.cs:line 310 else if (e.CommandName = = "upd" ) { LinkButton