0

Hi,

I'm creating a simple form where you select a company name from a dropdown list, and you can then update the values associated with this using textboxes and a submit button.

The dropdown works fine (it shows all the companies in the database) and the submit button submits data to the database OK.

However, only the first alphabetical company attributes are changed, no matter which company from the dropdown is selected - the dropdown seems to "jump back" to the first value in the list when I press submit, and no VB tutorial I've found has helped me work out what is wrong...

I know its not the most elegant code, but it's part of my very first web application - I'm hoping it is going to be a fairly simple "rookie mistake"... Help would be appreciated!

(I'm using an SQLExpress 2008 database)

Thanks

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Web.UI.Page" %>

<script language="VB" runat="server">

Dim dbconn As SqlConnection
Dim dropcomm, dropread

Sub page_load(Sender As Object, E As EventArgs) 
	Dim sqlselect
	connect()
	sqlselect="SELECT Name FROM Data ORDER BY Name"
	dropcomm=New SQLCommand(sqlselect,dbconn)
	dropread=dropcomm.ExecuteReader()
	dname.DataSource=dropread
	dname.DataBind()
End Sub	

Sub back_OnClick(sender as Object, e as EventArgs)
	Response.Redirect("contacts.aspx")
End Sub

Sub connect
	dbconn=New SQLConnection("Data Source=(local)\DEVELOPMENT; Initial Catalog=contacts; User Id=sa;Password=password;")
	dbconn.Open()
End Sub

Sub updatedb_OnClick(sender as Object, e as EventArgs)
	Dim sqlinsert
		connect()
			sqlinsert="UPDATE Data SET ID=@id, Phone=@phone, Email=@email, Addr_1=@addr_1, Addr_2=@addr_2, Addr_3=@addr_3, Post_Code=@post_code " _
			& "WHERE Name=@name;"
	dropcomm = New SqlCommand(sqlinsert, dbconn)
	dropcomm.Parameters.add("@id", tid.text.ToLower)
	dropcomm.Parameters.add("@name", dname.Text.ToLower)
	dropcomm.Parameters.add("@phone", tphone.Text.ToLower)
	dropcomm.Parameters.add("@email", temail.Text.ToLower)
	dropcomm.Parameters.add("@addr_1", taddr1.Text.ToLower)
	dropcomm.Parameters.add("@addr_2", taddr2.Text.ToLower)
	dropcomm.Parameters.add("@addr_3", taddr3.Text.ToLower)
	dropcomm.Parameters.add("@post_code", tpostcode.text.ToLower)
		
	dropcomm.ExecuteNonQuery()
	
	dropread.Close()
	dbconn.Close()
	updatedb.Attributes.Add("onclick", "this.disabled=true;")
	lbl1.text = "Company updated sucessfully!"
	lbl1.forecolor = System.Drawing.Color.Green

End Sub
	
</script>

<html>
<body>

<form runat="server">

<table border="0" width="100%">
<tr align="center">
<td width="10%"><img src=".\cslogo.jpg" /></td>
<td width="50%"><h1 style="text-align:center">
Update an existing Contact</h1></td>
<td width="10%"></td>
<td width="5%"></td>
<td width="20%">
<asp:Button id = back runat="server"
	Text    = "Back to main list"
	OnClick = back_OnClick/></td>
</table>
	
<table border="0" width="50%">	
<tr align="left">
<tr><br /></tr>
<td width="50%"></td>
<td width="20%">
Company Name:</td>
<td width="30%">
<asp:DropDownList id="dname" DataValueField="Name" DataTextField="Name" runat="server" />
</td>
</tr>

<tr align="left">
<td width="50%"></td>
<td width="20%">
Telephone Number:</td>
<td width="30%">
<asp:TextBox id="tphone" runat="server" />
</td>
</tr>

<tr align="left">
<td width="50%"></td>
<td width="20%">
Speed Dial:</td>
<td width="30%">
<asp:TextBox id="tid" runat="server" />
</td>
</tr>

<tr align="left">
<td width="50%"></td>
<td width="20%">
Contact E-Mail:</td>
<td width="30%">
<asp:TextBox id="temail" runat="server" />
</td>
</tr>

<tr align="left">
<td width="50%"></td>
<td width="20%">
Address Line 1:</td>
<td width="30%">
<asp:TextBox id="taddr1"  runat="server" />
</tr>
</td>

<tr align="left">
<td width="50%"></td>
<td width="20%">
Address Line 2:</td>
<td width="30%">
<asp:TextBox id="taddr2" runat="server" />
</tr>
</td>

<tr align="left">
<td width="50%"></td>
<td width="20%">
Address Line 3:</td>
<td width="30%">
<asp:TextBox id="taddr3" runat="server" />
</tr>
</td>

<tr align="left">
<td width="50%"></td>
<td width="20%">
Post Code:</td>
<td width="30%">
<asp:TextBox id="tpostcode" runat="server" />
</tr>
</td>

<tr align="center">
<td width="50%"></td>
<td width="20%"></td>
<td width="30%">
<asp:Button id = updatedb runat="server"
	Text    = "Submit"
	OnClick = updatedb_OnClick/>
</tr>
</td>


<tr align="center">
<td width="50%"></td>
<td width="20%"></td>
<td width="30%"><asp:Label id="lbl1" runat="server" />
</tr>
</td>


</table>

</form>

</body>
</html>
3
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by benk1
0

when submit clicked the page is posted thatswhy the dropdownlist is reloaded,if you wants to update without a page refresh then you need to apply ajax,

1

Property IsPostBack : It indicates whether the page is being rendered for the first time or is being loaded in response to a postback.

Sub page_load(Sender As Object, E As EventArgs) 
   IF Not IsPostBack Then
	Dim sqlselect
	connect()
	sqlselect="SELECT Name FROM Data ORDER BY Name"
	dropcomm=New SQLCommand(sqlselect,dbconn)
	dropread=dropcomm.ExecuteReader()
	dname.DataSource=dropread
	dname.DataBind()
   End If
End Sub
Votes + Comments
Thank you so much! Problem solved
0

Adatapost - you hero of ASP.NET! Hats off to you.

Chandru7 - Thanks anyway, but I didn't use Ajax this time to solve this one. Maybe I'll look at it as my skills develop

Cheers guys!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.