VB.NET - adding items into listview

Asked By sinduja arunkumar on 12-Nov-10 01:30 AM

hi
im not able to add records into the listview im getting this error"There is already an open DataReader associated with this Command which must be closed first".Here is the code:

If chklist.CheckOnClick = True Then

str1 = chklist.Text

End If

If lblCustCode.Text = "" Then

MessageBox.Show(" Please Select the CustomerCode ", strprjName, MessageBoxButtons.OK, MessageBoxIcon.Information)

End If

If lblCustName.Text = "" Then

MessageBox.Show(" Please Select the CustomerName ", strprjName, MessageBoxButtons.OK, MessageBoxIcon.Information)

End If

If cb1.Text = "Select" Or cb1.Text = "" Then

MessageBox.Show(" Select the Payment Type ", strprjName, MessageBoxButtons.OK, MessageBoxIcon.Information)

End If

com = New SqlCommand("select CustCode from tbl_Purchase where CustCode='" & lblCustCode.Text & "'", con)

dr = com.ExecuteReader

If dr.Read() Then

MessageBox.Show(" This Code Already Exists ", strprjName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

Else

cmd = New SqlCommand("insert into tbl_Purchase values('" & lblCustCode.Text & "','" & lblCustName.Text & "','" & str1 & "','" & TextBox1.Text & "','" & TextBox2.Text & "','" & cb1.Text & "'", con)

cmd.ExecuteNonQuery()

Purchase()

lblCustCode.Text = ""

lblCustName.Text = ""

TextBox1.Text = ""

TextBox2.Text = ""

cb1.Text = "Select"

End If

dr.Close()


pls help me in solving this
Reena Jain replied to sinduja arunkumar on 12-Nov-10 01:32 AM
hi,

do this

com = New SqlCommand("select CustCode from tbl_Purchase where CustCode='" & lblCustCode.Text & "'", con)

dr = com.ExecuteReader

If dr.Read() Then

con.open(); //open the connection

MessageBox.Show(" This Code Already Exists ", strprjName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

Else

cmd = New SqlCommand("insert into tbl_Purchase values('" & lblCustCode.Text & "','" & lblCustName.Text & "','" & str1 & "','" & TextBox1.Text & "','" & TextBox2.Text & "','" & cb1.Text & "'", con)

cmd.ExecuteNonQuery()

Purchase()

lblCustCode.Text = ""

lblCustName.Text = ""

dr.Close();

con.close() //close the connection


hope this will help you

Nowshad M replied to sinduja arunkumar on 12-Nov-10 01:33 AM
Hi,
For working with DataReader you must have open connection.

You should open the connection before the execution of DataReader and U must close the connection after reading all the data from DataReader.
Sagar P replied to sinduja arunkumar on 12-Nov-10 01:36 AM

As error suggest you have opened datareader but you havent close it.. so just close it like;

If chklist.CheckOnClick = True Then

str1 = chklist.Text

End If

If lblCustCode.Text = "" Then

MessageBox.Show(" Please Select the CustomerCode ", strprjName, MessageBoxButtons.OK, MessageBoxIcon.Information)

End If

If lblCustName.Text = "" Then

MessageBox.Show(" Please Select the CustomerName ", strprjName, MessageBoxButtons.OK, MessageBoxIcon.Information)

End If

If cb1.Text = "Select" Or cb1.Text = "" Then

MessageBox.Show(" Select the Payment Type ", strprjName, MessageBoxButtons.OK, MessageBoxIcon.Information)

End If

com = New SqlCommand("select CustCode from tbl_Purchase where CustCode='" & lblCustCode.Text & "'", con)

dr = com.ExecuteReader

If dr.Read() Then

MessageBox.Show(" This Code Already Exists ", strprjName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

Else

if NOT dr.IsClosed then
  dr.Close()
cmd = New SqlCommand("insert into tbl_Purchase values('" & lblCustCode.Text & "','" & lblCustName.Text & "','" & str1 & "','" & TextBox1.Text & "','" & TextBox2.Text & "','" & cb1.Text & "'", con)

cmd.ExecuteNonQuery()

Purchase()

lblCustCode.Text = ""

lblCustName.Text = ""

TextBox1.Text = ""

TextBox2.Text = ""

cb1.Text = "Select"

End If

if NOT dr.IsClosed then
  dr.Close()

Danasegarane Arunachalam replied to Nowshad M on 12-Nov-10 01:45 AM

There are two things you can do with this.

1. You can close the connection as soons as close the datareader

dr = com.ExecuteReader(CommandBehaviour.CloseConnection)

and you must use the dr.close method. This will take care the closing of connection

1. Use http://blogs.msdn.com/b/angelsb/archive/2004/09/07/226597.aspxin .Net

sinduja arunkumar replied to Reena Jain on 12-Nov-10 04:10 AM

hi Reena
Thank u so much now im getting it correctly ,can you pls tell me how to insert the checked item into the listview from checkedlistbox control,the problem is that if i click more than one record only single record is getting inserted can you

pls help me.....

cmd = New SqlCommand("insert into tbl_Purchase values('','" & lblCustCode.Text & "','" & lblCustName.Text & "','" & str1 & "','" & TextBox1.Text & "','" & TextBox2.Text & "','" & cb1.Text & "')", con)

str1 is checkeditem text

Reena Jain replied to sinduja arunkumar on 12-Nov-10 05:31 AM
hi,
happy to help you
just merge the checklistbox item in a string like this
string mycheckitem;
for(id=0; i<checkbox1.items.count; i++)
{
mycheckitem+=checkbox1.selecteditem.tostring() + ",";
}

and use this to insert in database

hope this will help you
sinduja arunkumar replied to Reena Jain on 12-Nov-10 07:17 AM
hi
wat is that 'id' refers used in the for loop