Showing posts with label datagrid. Show all posts
Showing posts with label datagrid. Show all posts

Monday, March 12, 2012

problem with executing an sqlcommand

when i push a button my datagrid doesn't show up. please help this is my code. I only try to execute one of the sql command and when i push the button it just redirects me to my homepage.

Private Sub btnbid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbid.Click

Dim conn As New SqlConnection((Application("SQL_Connection_String")))

conn.Open()

Dim cmdbid As New SqlCommand("insert into Bids (CustID,ItemCode,BidAmount) values (@.CustID,@.ItemCode,@.BidAmount)", conn)

cmdbid.Parameters.Add("@.CustID", SqlDbType.Int, 4)
cmdbid.Parameters("@.CustID").Value = Session("CustID")
cmdbid.Parameters.Add("@.ItemCode", SqlDbType.Int, 4)
cmdbid.Parameters("@.ItemCode").Value = CInt(Request.QueryString("Id"))
cmdbid.Parameters.Add("@.BidAmount", SqlDbType.Decimal, 9)
cmdbid.Parameters("@.BidAmount").Value = CDec(txtbidamount.Text)

Dim cmdhighbid As New SqlCommand("update items set Highestbid=@.BidAmount,HighestBidder=@.bidder where ItemCode=@.ItemCode and Highestbid<@.BidAmount", conn)

cmdhighbid.Parameters.Add("@.BidAmount", SqlDbType.Decimal, 9)
cmdhighbid.Parameters("@.BidAmount").Value = CDec(txtbidamount.Text)
cmdhighbid.Parameters.Add("@.bidder", SqlDbType.Int, 4)
cmdhighbid.Parameters("@.bidder").Value = Session("CustID")
cmdhighbid.Parameters.Add("@.ItemCode", SqlDbType.Int, 4)
cmdhighbid.Parameters("@.ItemCode").Value = Request.QueryString("Id")

Try
cmdbid.ExecuteNonQuery()

lblbidstatus.Text = "Bid Inserted Successfully!! Good Luck!!!"
Catch ex As Exception

lblbidstatus.Text = ex.Message
End Try

btnbid.Enabled = False
conn.Close()
End Subare you binding the datagrid within if not ispostback loop in page_load.. ?

hth|||i don't have anything in the page load only the bindgrid() proc.|||because you are prbly losing the datagrid during postbck. try this


sub page_load(...)
if not ispostback() then
bindgrid()
end if
end sub

hth|||no that's not the problem. when i push the button it redirects me to the homepage with no reason and does not do any changes i do with the sql command|||i dont see anything in your code to redirect to another page. are you sure you are calling the right event ?

Monday, February 20, 2012

Problem with datareader

Hello

i creae one programm, there is an two data reader and two Gridview or datagrid , and my programm have one error

my programm is there

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class aries : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
SqlDataReader dr;
SqlDataReader dr1;
SqlCommand cmd1;

protected void Page_Load(object sender, EventArgs e)
{
string str;
str = ConfigurationSettings.AppSettings["DBconnect"];
con = new SqlConnection(str);
con.Open();
cmd = new SqlCommand("select color from zodiac_color where Sno=1", con);
dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
cmd1=new SqlCommand("select number from zodiac_number where Sno=1",con);
dr1 = cmd.ExecuteReader();
GridView2.DataSource = dr1;
GridView2.DataBind();

}
}

i want to calll two value in a same database but the table is diffrent

so please help me ?

The error is ::---

There is already an open DataReader associated with this Command which must be closed first.

please help me

ashwani kumar

dr.Close();
cmd1=new SqlCommand("select number from zodiac_number where Sno=1",con);
dr1 = cmd1.ExecuteReader();

Basically you are trying to use the same connection and command to open 2 datareaders. DataReaders make exclusive use of connections until you actually close them.

However, you don't need a datareader to obtain sinlge values. You should use ExecuteScalar for that. On the other hand, you should be able to use a JOIN in your query to link both tables and return both values at once in one datareader.

|||

You can addMultipleActiveResultSets=True inside your ConnectionString in web.config file and use this code:

SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["DBconnect"]);
SqlCommand command = conn.CreateCommand();
command.CommandText = "select color from zodiac_color where Sno=1";
SqlCommand command2 = conn.CreateCommand();
command2.CommandText = "select number from zodiac_number where Sno=1";
conn.Open();
GridView1.DataSource = command.ExecuteReader();
GridView2.DataSource = command2.ExecuteReader();
GridView1.DataBind();
GridView2.DataBind();
conn.Close();