ASP.NET - Qry Err and Updating

Asked By Rahul
09-Feb-12 02:51 AM
Distinct Qry err:

cmd = new SqlCommand("select sum(DISTINCT credit) AS total from creditadd where cid<=cid and userid='" + TextBox1.Text + "' and username='" + TextBox2.Text + "'", con);

in updating:

i have updated total amount successfully but i am not getting correct total after updating it shows only already entered. what i have modify in my codings:

full codings:

try

 

 

 

{

 

 

if (con.State == ConnectionState.Closed)

{

con.Open();

}

cmd =

 

new SqlCommand("select Count(*) from agentreg where userid='" + TextBox1.Text + "' and username='" + TextBox2.Text + "'", con);

 

 

int i = (int)cmd.ExecuteScalar();

 

 

if (i != 0)

{

cmd =

 

new SqlCommand("select count (username) from creditadd where username='" + TextBox2.Text + "' and userid='" + TextBox1.Text + "'", con);

 

 

int j = (int)cmd.ExecuteScalar();

 

 

if (j != 0)

{

 

 

cmd =

 

new SqlCommand("select sum(DISTINCT credit) AS total from creditadd where cid<=cid and userid='" + TextBox1.Text + "' and username='" + TextBox2.Text + "'", con);

 

 

dr = cmd.ExecuteReader();

dr.Read();

 

 

if (dr.HasRows)

{

total =

 

float.Parse(dr[0].ToString());

 

 

float f = total + float.Parse(TextBox3.Text);

dr.Close();

 

cmd =

 

new SqlCommand("insert into creditadd(userid,username,services,credit,total)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + DropDownList1.SelectedItem.Value + "','" + TextBox3.Text + "','"+f+"')", con);

cmd.ExecuteNonQuery();

TextBox1.Text =

 

"";

TextBox2.Text =

 

"";

TextBox3.Text =

 

"";

Response.Write(

 

"<script>alert('Successfull');</script>");

bindgrid();

dropbind();

}

 

 

else

 

 

{

dr.Close();

}

}

 

 

else

 

 

{

 

 

cmd =

 

new SqlCommand("insert into creditadd(userid,username,services,credit,total)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + DropDownList1.SelectedItem.Value + "','" + TextBox3.Text + "','" + TextBox3.Text + "')", con);

cmd.ExecuteNonQuery();

TextBox1.Text =

 

"";

TextBox2.Text =

 

"";

TextBox3.Text =

 

"";

Response.Write(

 

"<script>alert('Successfull');</script>");

bindgrid();

dropbind();

}

}

else

 

 

{

Response.Write(

"<script>alert('Not Registered');</script>");

}

}

}

 

 

catch (Exception ex)

{

Response.Write(

"Error:" + ex.ToString());

}

 

finally

 

{

 

if (con.State == ConnectionState.Open)

{

con.Close();

}

}

}

edit/update/del codings :

protected

void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{

 

try

 

{

 

if (con.State == ConnectionState.Closed)

{

con.Open();

}

 

if (e.CommandName == "can")

{

GridView1.EditIndex = -1;

bindgrid();

dropbind();

}

 

else if (e.CommandName == "upd")

{

 

LinkButton lkbupd = (LinkButton)e.CommandSource as LinkButton;

 

GridViewRow row = lkbupd.NamingContainer as GridViewRow;

 

Label lbid = (Label)row.FindControl("lblid");

 

TextBox txcredit = (TextBox)row.FindControl("txtcredit"

);

 

TextBox txtotal = (TextBox)row.FindControl("txttotal");

cmd =

new SqlCommand("update creditadd set credit='" + txcredit.Text + "',total='"+txtotal.Text+"' where cid ='" + lbid.Text + "'", con);

cmd.ExecuteNonQuery();

GridView1.EditIndex = -1;

Response.Write(

"Updated Successfully");

bindgrid();

dropbind();

}

 

else if (e.CommandName == "del")

{

 

LinkButton lnkdel = (LinkButton)e.CommandSource as LinkButton;

 

GridViewRow row = lnkdel.NamingContainer as GridViewRow;

 

Label lid = (Label)row.FindControl("lblid");

cmd =

new SqlCommand("delete from creditadd where cid='" + lid.Text + "'", con);

cmd.ExecuteNonQuery();

Response.Write(

"Deleted Successfully");

bindgrid();

dropbind();

}

}

 

catch (Exception ex)

{

Response.Write(

"Error:" + ex.ToString());

}

 

finally

 

{

 

if (con.State == ConnectionState.Open)

{

con.Close();

}

}

}

 


  Web Star replied to Rahul
09-Feb-12 03:12 AM
If you are put the code to get total before update statement execution than you get only old value as total if you want to update first and than get total value than you need to first put code for update after that you fetch the total  value than you will get correct one.
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 img = ( ImageButton )e.CommandSource as ImageButton ; GridViewRow row = img.NamingContainer as GridViewRow ; Label lid = ( Label )row.FindControl( "lblid" ); int id = Convert .ToInt16(lid.Text); cmd = new SqlCommand 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
Getting value error i am not getting this value correctly here ? cmd = new SqlCommand ( "select *from tbl_hoteldetails where DropDownList2.Items.Insert(00, "Select" ); int id1 = Convert .ToInt16(dr[2].ToString()); id1 = DropDownList2.SelectedIndex; / / error dr.Close(); } int id1 = Convert .ToInt16(dr[2].ToString()); id1 = Convert .ToInt16(dr[2].ToString State Select Tamilnadu Maharastra Andrha kerala City Select Madurai chennai trichy it shows like this error ? this coding is correctly display state only not city so what i want to do here ? if (e.CommandName = = "edt" ) { ImageButton img = ( ImageButton )e.CommandSource as ImageButton ; GridViewRow row = img.NamingContainer as GridViewRow ; Label lid = ( Label )row.FindControl( "lblid" ); int id = Convert .ToInt16(lid.Text); cmd = new SqlCommand DropDownList2.Items.Insert(00, "Select"); int id1 = Convert.ToInt16(dr[2].ToString()); id1 = DropDownList2.SelectedIndex; / / error dr.Close(); } You are just binding the dropdown list and trying to read the value else { / / Read the selected index int id1 = Convert.ToInt16(dr[2].ToString()); id1 = DropDownList2.SelectedIndex; / / error } hi, Please try for the postback()is properly done or not like this. If(!ispostback DropDownList2.Items.Add( "Select" ); } } protected void Button1_Click( object sender, EventArgs e) { try { if (con.State = = ConnectionState .Closed) { con.Open(); } if (txthotelname.Text ! = "" ) { cmd = new SqlCommand ( "select count (*) from tbl_city where stateid
Text + "', '" + DropDownList1.SelectedItem.Value + "', '" + TextBox3.Text + "', '" + f + "', '" + DropDownList2.SelectedItem.Value + "', '" + DropDownList3.SelectedItem.Value + "')" , con); cmd.ExecuteNonQuery(); TextBox1.Text = "" ; TextBox2.Text = "" ; TextBox3.Text = "" ; TextBox4.Text = "" ; Response.Write( "<script> alert('Successfull');< / script> " ); bindgrid total = @total, @total = @total + credit where userid = '" + TextBox1.Text + "' and username = '" + TextBox2.Text + "'" , con); cmd.ExecuteNonQuery(); Hope this helpful this is my update coding ? SqlParameter param = new SqlParameter (); param.ParameterName = "@total 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 = -1; bindgrid(); dropbind(); } else if (e CommandName = = "upd" ) { LinkButton lkbupd = ( LinkButton )e.CommandSource as LinkButton ; GridViewRow row = lkbupd.NamingContainer as GridViewRow ; Label lbid = ( Label )row.FindControl( "lblid" ); TextBox txusrid = ( TextBox )row.FindControl( "txtusrid" ); TextBox txusrname = ( TextBox insert that how ?} else if (e.CommandName = = "del" ) { LinkButton lnkdel = ( LinkButton )e.CommandSource as LinkButton ; GridViewRow row = lnkdel.NamingContainer as GridViewRow ; Label lid = ( Label )row.FindControl( "lblid" ); cmd = new SqlCommand ( "delete
this row command codings in button pls tel me anybody ? protected void GridView1_RowCommand( object sender, GridViewCommandEventArgs e) { try { if (con.State = = ConnectionState .Closed) { con.Open(); } else if (e.CommandName = = "upd" ) { ImageButton img = ( ImageButton )e.CommandSource as ImageButton ; GridViewRow row = img.NamingContainer as GridViewRow ; Label lid = ( Label )row.FindControl( "lblid" ); int id = Convert .ToInt16(lid.Text); cmd = new SqlCommand Value + "', hotelname = '" + txthotelname.Text + "', path = '" + "upload / " + file + "' where id = '" + f + "'" , con); int i = ( int )cmd.ExecuteNonQuery(); if (i > = 1) { GridView1.EditIndex = -1; ShowAlertMessage( "Updated Successfully" ); bindgrid(); } } else { cmd = new SqlCommand ( "update cityid = '" + DropDownList2.SelectedItem.Value + "', hotelname = '" + txthotelname.Text + "' where id = '" + f + "'" , con); int i = ( int )cmd.ExecuteNonQuery(); if (i > = 1) { GridView1.EditIndex = -1; ShowAlertMessage( "Updated Successfully" ); bindgrid(); } } } else { dr.Close(); } } else if (e.CommandName = = "del" ) { ImageButton img = ( ImageButton )e.CommandSource as ImageButton ; GridViewRow row = img.NamingContainer as GridViewRow ; Label lid = ( Label )row.FindControl( "lblid" ); cmd = new SqlCommand ( "delete from tbl_hoteldetails where id = '" + lid
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 35px;width:1256px; } else if (e.CommandName = = "upd" ) { LinkButton lkbupd = ( LinkButton )e.CommandSource as LinkButton ; GridViewRow row = lkbupd.NamingContainer as GridViewRow ; Label lbid = ( Label )row.FindControl( "lblid" ); TextBox txusrid = ( TextBox )row.FindControl( "txtusrid" ); TextBox txusrname = ( TextBox 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