|
You need to iterate over the collection and build the SQL to insert the values. Of course, you're also wide open for SQL injection attacks.
Can u elaborate please?
Chaitra N
|
|
|
|
|
You are getting an error because the ListBox2.Items.ToString() function just returns the text System.Web.UI.WebControls.ListItemCollection - because that is what you are referencing....it's just a collection.
You would need to loop through each of the ListItems and get out the text value of each and run that as a seperate insert...i.e.
foreach(ListItem item in ListBox2.Items){
str = "insert into a_software (soft)values('" & item.Text & "')"
//then run the insert
}
that would be a way to do it using your current code, or build up a collection of inserts and run them all at once (not sure if you can do that with ExecuteNonQuery() though) - running everything at once would definitely be the better option.
|
|
|
|
|
I am using vb not c#. foreach(ListItem item in ListBox2.Items) will not work in vb
Chaitra N
|
|
|
|
|
|
Following is the code i have written, But it is giving the error, ExecuteNonQuery: CommandText property has not been initialized
Partial Class _Default
Inherits System.Web.UI.Page
Dim constr As String = ConfigurationSettings.AppSettings("conn")
Dim dbconn As New SqlConnection(constr)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Button1.Attributes.Add("onclick", "return fnMoveItems('ListBox1','ListBox2')")
Button2.Attributes.Add("onclick", "return fnMoveItems('ListBox2','ListBox1')")
End Sub
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim str As String
If dbconn.State <> ConnectionState.Open Then
dbconn.Open()
End If
For Each item As ListItem In ListBox2.Items
str = "insert into a_software (soft)values('" & item.Text & "')"
Next
Dim comm As New SqlCommand(str, dbconn)
Try
comm.ExecuteNonQuery()
Label1.ForeColor = Drawing.Color.Green
Label1.Text = "Success"
Catch ex As Exception
Label1.Text = ex.Message
End Try
End Sub
End Class
Chaitra N
|
|
|
|
|
Hi, This function is working, But the each value is getting stored in different row of the table. I want them to get stored in a single row.
Dim str As String
If dbconn.State <> ConnectionState.Open Then
dbconn.Open()
End If
For Each item As ListItem In lstselectedemployees.Items
str = "insert into a (soft)values('" & item.Text & "')"
Dim comm As New SqlCommand(str, dbconn)
Try
comm.ExecuteNonQuery()
Label1.ForeColor = Drawing.Color.Green
Label1.Text = "Success"
Catch ex As Exception
Label1.Text = ex.Message
End Try
Next
If I use the following code then, only the last item of the list box is getting stored,
Dim str As String
If dbconn.State <> ConnectionState.Open Then
dbconn.Open()
End If
For Each item As ListItem In lstselectedemployees.Items
str = "insert into a (soft)values('" & item.Text & "')"
next
Dim comm As New SqlCommand(str, dbconn)
Try
comm.ExecuteNonQuery()
Label1.ForeColor = Drawing.Color.Green
Label1.Text = "Success"
Catch ex As Exception
Label1.Text = ex.Message
End Try
Chaitra N
|
|
|
|
|
Hello Chaitra..
have a try with following code..
I think u have to save the all items of ListBox2 to database ...is i am correct???
if yes then,
dim szItem as new ListBoxItem
for each szItem in Listbox2.Items
//Now fire your insert query here
str = "insert into a_software (soft)values('" & szItem.text & "')"
Next
hope it will helps....
regards,
koolprasad2003
If the message is useful for U then please Rate This message...
Be a good listener...Because Opprtunity knoughts softly...N-Joy
|
|
|
|
|
koolprasad2003 wrote: str = "insert into a_software (soft)values('" & szItem.text & "')"
writing a query in your code, make u wide open to SQL Injection attack..please tell me that is only for example explanation
Kind Regards,
Tomi
|
|
|
|
|
Following is the code i have written, But it is giving the error, ExecuteNonQuery: CommandText property has not been initialized
Partial Class _Default
Inherits System.Web.UI.Page
Dim constr As String = ConfigurationSettings.AppSettings("conn")
Dim dbconn As New SqlConnection(constr)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Button1.Attributes.Add("onclick", "return fnMoveItems('ListBox1','ListBox2')")
Button2.Attributes.Add("onclick", "return fnMoveItems('ListBox2','ListBox1')")
End Sub
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim str As String
If dbconn.State <> ConnectionState.Open Then
dbconn.Open()
End If
For Each item As ListItem In ListBox2.Items
str = "insert into a_software (soft)values('" & item.Text & "')"
Next
Dim comm As New SqlCommand(str, dbconn)
Try
comm.ExecuteNonQuery()
Label1.ForeColor = Drawing.Color.Green
Label1.Text = "Success"
Catch ex As Exception
Label1.Text = ex.Message
End Try
End Sub
End Class
Chaitra N
|
|
|
|
|
u have to take following line in for each..Next loop
n_gchaitra wrote: comm.ExecuteNonQuery()
and the following statement move above the connection.Open()
n_gchaitra wrote: Dim comm As New SqlCommand(str, dbconn)
make the changes and First check that connection is successfully open or not
regards,
koolprasad2003
Be a good listener...Because Opprtunity knoughts softly...N-Joy
|
|
|
|
|
How is it possible, use comm.executenonquey() before declaring comm? I get the following message near the word str i.e Dim comm As New SqlCommand(str, dbconn)
var 'str' is used before it has been assigned a value. A null reference exception could result at runtime
Chaitra N
|
|
|
|
|
Hi, This function is working, But the each value is getting stored in different row of the table. I want them to get stored in a single row.
Dim str As String
If dbconn.State <> ConnectionState.Open Then
dbconn.Open()
End If
For Each item As ListItem In lstselectedemployees.Items
str = "insert into a (soft)values('" & item.Text & "')"
Dim comm As New SqlCommand(str, dbconn)
Try
comm.ExecuteNonQuery()
Label1.ForeColor = Drawing.Color.Green
Label1.Text = "Success"
Catch ex As Exception
Label1.Text = ex.Message
End Try
Next
If I use the following code then, only the last item of the list box is getting stored,
Dim str As String
If dbconn.State <> ConnectionState.Open Then
dbconn.Open()
End If
For Each item As ListItem In lstselectedemployees.Items
str = "insert into a (soft)values('" & item.Text & "')"
next
Dim comm As New SqlCommand(str, dbconn)
Try
comm.ExecuteNonQuery()
Label1.ForeColor = Drawing.Color.Green
Label1.Text = "Success"
Catch ex As Exception
Label1.Text = ex.Message
End Try
Chaitra N
|
|
|
|
|
n_gchaitra wrote: str = "insert into a_software (soft)values('" & ListBox2.Items.ToString() & "')"
if u want to avoid SQL injection attack..u have to write u'r query into stored procedure
Regards,
Tomi
|
|
|
|
|
How to connect xml file in tally. I have rts.dll(link) tally version(7.2). I want aspx file code....
v.surendrakumar
|
|
|
|
|
1 - try a sensible subject, everyone here wants help
2 - what is tally ? What is rts.dll ? What are you talking about ?
You can open an XML file with the XmlDocument or XMLDataDocument classes.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Any suggestion on free asp.net cms? Thanks.
|
|
|
|
|
i have a website which is to run at the client end. currently what i am doing is installing the .net framework an then installing the application.Is there any way to integrate the .net framework with the application so that there does not occur any need to install the .net framework manually.
after the installation the code pages also appearing with the aspx pages.
Is there any way to get rid of this problem.
Sonia Gupta
Soniagupta1@yahoo.co.in
Yahoo messengerId-soniagupta1
Love is Friendship and Friendship is Love....
|
|
|
|
|
You need to add .NET redestributable package with your application. Then configure your installer to install framework first and your application. It looks like Visual Studio packaging won't provide option for doing this. I used a third party installer to do this. Try Installmaker.
|
|
|
|
|
Hi,
I have two fileds in a form both are combo box ie month and year and not data bound (form1) .
from this form I am going to another page (form2)
and from form2 back to form1
I have to show the same values of the combo ie month and year(which was selected by user ) when form 1 is loaded again
what is the correct way of passing data in two web pages in asp.net ie retaining the values of the drop down list in the form1
no use of sessions or global variables please What is the correct way of doing it
thanks in advance
Sakshi
|
|
|
|
|
It's not possible. Instead, you'd need to turn the two pages into controls, so they could both exist on one page, then viewstate will maintain the state of form1, even when it's not visible, and form2 is being shown.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Hi,
I am using a gridview to display user information, and one column I am trying to display the user's first name and last name together. They are 2 seperate properties in the UserInfo class.
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<a href='MemberDetails.aspx?MemberID=<%# DataBinder.Eval(Container.DataItem, "ID") %>'><%# GetMemberName(UserInfo)%></a>
</ItemTemplate>
<HeaderStyle CssClass="gridview_header" />
<ItemStyle CssClass="gridview_cell" />
</asp:TemplateField>
</Columns>
The GetMemberName method looks like this:
protected string GetMemberName(UserInfo info)
{
string strName = string.Empty;
// Check for a first and last name
if ((info.FirstName == string.Empty) && (info.LastName == string.Empty))
strName = "Unknown";
strName = info.FirstName + " " + info.LastName;
// Return
return strName;
}
I do not know how to call the GetMemberName method correctly from the gridview. Please can someone help me?
Regards
ma se
|
|
|
|
|
Where is the userinfo object coming from ? If it's the item stored in the datasource, pass Container.DataItem.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
In a custom web control I took a look at some time ago, I saw that the writer programmer locked the thread before editing or reading from one of the member variables of the control (a generic Dictionary). Now, what I guess the locking does, because of the result of the script, is making that member variable a kind of application variable. My question is, how is it that the locking before touching the variable makes it a sort of application variable (available to all users viewing the page)?
Thanks in advance
-- modified at 8:30 Tuesday 31st July, 2007
|
|
|
|
|
Hi,
I am new to asp.net but have learnt a lot through the tutorials and random searching here and elsewhere. Have many years expereince with classic ASP but this task I am working on is sending me around the bend !
I want to have a nested repeater (this bit I understand fine) but am unsure how to make my data from two tables in the one database join together.
I have a table called boatcats which list a few different categoies of boat and I have a second table called 'boats' that list all of the boats and their details. The two tables share boatcats.ID and boats.catid
I doubt my code is heading in the right direction and I am unsure how to link the two queries. I am using VB not C# and should I be doing this sort of thing at code level (which I am used to in asp classic or with Visual Web Developer 2005)
Code so far is....
<script language="vb" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then
BindData() 'Only bind the data on the first page load
End If
End Sub
Sub BindData()
dim dbconn,strq,dbcomm,dbread
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & server.mappath("boats.mdb"))
dbconn.Open()
strq = "Select * from boatcats"
dbcomm=New OleDbCommand(strq,dbconn)
dbread=dbcomm.ExecuteReader()
boatcats.DataSource=dbread
boatcats.DataBind()
dbread.Close()
dbconn.Close()
End Sub
Sub BindData2()
dim dbconn2,strq2,dbcomm2,dbread2,boats2
dbconn2=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & server.mappath("boats.mdb"))
dbconn2.Open()
strq2 = "Select * from boats"
dbcomm2=New OleDbCommand(strq2,dbconn2)
dbread2=dbcomm2.ExecuteReader()
boats.DataSource=dbread2
boats.DataBind()
dbread2.Close()
dbconn2.Close()
end sub
</script>
I havn't added the asp:repeater section, this I have no problems with.
Any help appreciated.
Martin
|
|
|
|
|
Depends on what you want to do with the boatcats data i guess, but i will assume this is what you want in the repeater:
Boat Category
Boat Name
Boat Name
Boat Category
Boat Name
...
then you wouldn't need to have a nested repeater, you could do it in one repeater and just do a join in you database query:
"Select * from boats join boatcats on boatcats.id on boats.boatcats.id order by boatcats.name, boats.name"
(returning all fields from boats and boatcats) and order on the category name then the boat name.
OR
"Select boats.*, boatcats.title [boatCategoryTitle] from boats join boatcats on boatcats.id on boats.boatcats.id order by boatcats.name, boats.name"
(returning all fields from boats and boatcat title) and order on the category name then the boat name.
That way you can just put in two placeholders within the <ItemTemplate> and on the placeholder have visible="<%# ShowCategory(Container.DataItem) %>" (for the header placeholder) then visible="<%# !ShowCategory(Container.DataItem) %>" (for the boat section placeholder).
Then put a function ShowCategory(object dataItem) which works out whether this is the same category as the last, if not store the category name and show the header. Have another function inside that displays the name of the category. Then display whatever you want in the body section....
Does that make sense?? Is that what you are trying to do??
|
|
|
|