i am doing a project using asp.net and i have created a form that inserts data in to sql database but i have two problems:
1.i need the date text box to have todays date by default and then the user could change it or keep it ,i tried using <asp:textbox id="std" value="<%datetime.now%>"> but didn't work
2.i have adrop down list that contains data from a table in database and i want to save the selected item into another table,i have highlited the code below where i tried to do that.
the code is:
Import Namespace="System.Data.SQLClient" %>
<%@ Import Namespace="System.Data" %>
<script language="
VB" runat="server">
sub Page_Load(Source as Object, E as EventArgs)
dim txtusername as integer
dim group_id as integer
Session("Username") = txtusername
Session("group_id") = group_id
if not Page.IsPostBack then
Dim MySQL as string = "Select [station_id], [station_name] from station where group_id='G5'"
Dim myConn As SqlConnection = New SqlConnection("data source=ldcTestsrv;database=WaterControl")
Dim objDR as SQLDataReader
Dim Cmd as New SQLCommand(MySQL, MyConn)
MyConn.Open()
objDR=Cmd.ExecuteReader(system.data.CommandBehavio r.CloseConnection)
ddlCustomer.DataSource = objDR
ddlCustomer.DataValueField="station_name"
ddlCustomer.DataTextField="station_id"
ddlCustomer.DataBind()
MyConn.Close()
end if
end sub
Sub doInsert(Source as Object, E as EventArgs)
Dim MySQL as string = "Insert into generation_station_operation_data (date_stamp,station_id,production,outflow,storage, internal_consumption,total_MGD,production_target,c hange_of_storage_target, change_of_storage) values (@dat,@ddlcustomer,@pro,@out,@sto,@intcon,@tot,@
pr odtar,@chstrtr,@chstr)"
Dim myConn As SqlConnection = New SqlConnection("data source=ldcTestsrv;uid=sa;pwd=;database=WaterContro l")
Dim Cmd as New SQLCommand(MySQL, MyConn)
cmd.Parameters.Add(New SQLParameter("@dat", stm.text))
'cmd.Parameters.Add(New SQLParameter("@sta", stid.text))
cmd.parameters.add( ddlcustomer.SelectedItem.Value)
cmd.Parameters.Add(New SQLParameter("@pro", prd.text))
cmd.Parameters.Add(New SQLParameter("@out", ouf.text))
cmd.Parameters.Add(New SQLParameter("@sto", sor.text))
cmd.Parameters.Add(New SQLParameter("@intcon", icons.text))
cmd.Parameters.Add(New SQLParameter("@tot", MGD.text))
cmd.Parameters.Add(New SQLParameter("@prodtar", tar.text))
cmd.Parameters.Add(New SQLParameter("@chstrtr", chtar.text))
cmd.Parameters.Add(New SQLParameter("@chstr", chstor.text))
MyConn.Open()
cmd.ExecuteNonQuery
label1.visible="true"
BindData()
MyConn.Close()
label1.text = "Your data has been received!"
End Sub
Sub viewthem(Source as Object, E as EventArgs)
mydatagrid.visible="true"
label1.visible="false"
BindData
End Sub
Sub Page_Change(sender As Object, e As DataGridPageChangedEventArgs)
mydatagrid.visible="true"
MyDataGrid.CurrentPageIndex = e.NewPageIndex
BindData
End Sub
Sub BindData()
Dim MySQL as string = "Select * from generation_station_operation_data order by date_stamp"
Dim myConn As SqlConnection = New SqlConnection( "data source=ldcTestsrv;uid=sa;pwd=;database=WaterContro l")
Dim ds as DataSet=New DataSet()
Dim Cmd as New SQLDataAdapter(MySQL,MyConn)
Cmd.Fill(ds,"generation_station_operation_data")
MyDataGrid.Datasource=ds.Tables("generation_statio n_operation_data").DefaultView
MyDataGrid.DataBind()
End Sub
Sub cleartextboxes(Source as Object, E as EventArgs)
Dim myForm As Control = Page.FindControl("form1")
Dim ctl As Control
For Each ctl In myForm.Controls
If ctl.GetType().ToString().Equals("System.Web.UI.Web Controls.TextBox") Then
CType(ctl, TextBox).Text = ""
End If
Next ctl
End Sub
</script>
<form id="form1" runat="Server">
<P><IMG alt="" src="Untitled-43.gif"></P>
<P align="left"><U><A href="http://localhost/web_test/header.aspx">Home</A>
| Log Off</P>
<STRONG>
</STRONG></U>
<P></P>
<P align="center"><STRONG><U>Generation Station Operation Data</U></STRONG></P>
<table align="center" border="1">
<tr>
<td align="right">
<P align="left">Date_stamp</P>
</td>
<td><asp:textbox id="stm" runat="server"></asp:textbox></td>
<tr> <td align="right">
<P align="left">Station_id</P>
</td>
<td><asp:dropdownlist id="ddlcustomer" runat="server"></asp:dropdownlist></td> <tr>
<td align="right">
<P align="left">Production</P>
</td>
<td><asp:textbox id="prd" runat="server"></asp:textbox></td>
<tr>
<td align="right">
<P align="left">Outflow</P>
</td>
<td><asp:textbox id="ouf" runat="server"></asp:textbox></td>
<tr>
<td align="right">
<P align="left">Storage</P>
</td>
<td><asp:textbox id="sor" runat="server"></asp:textbox></td>
<tr>
<td align="right">
<P align="left">Internal_Consumption</P>
</td>
<td><asp:textbox id="icons" runat="server"></asp:textbox></td>
<tr>
<td align="right">
<P align="left">Total_MGD</P>
</td>
<td><asp:textbox id="MGD" runat="server"></asp:textbox></td>
<tr>
<td align="right">
<P align="left">Production_Target</P>
</td>
<td><asp:textbox id="tar" runat="server"></asp:textbox></td>
<tr>
<td align="right">
<P align="left">Change_Of_Storage_Target</P>
</td>
<td><asp:textbox id="chtar" runat="server"></asp:textbox></td>
<tr>
<td align="right">
<P align="left">Change_Of_Storage</P>
</td>
<td><asp:textbox id="chstor" runat="server"></asp:textbox></td>
<tr>
<tr>
<td align="right" height="22"></td>
<td height="22"></td>
<tr>
<td align="center" colSpan="2">
<P align="left"><asp:button id="button1" onclick="doInsert" runat="server" Text="Insert"></asp:button><asp:button id="button2" onclick="viewthem" runat="server" Text="view data" causesvalidation="false"></asp:button><asp:button id="button3" onclick="cleartextboxes" runat="server" Text="Clear" causesvalidation="false"></asp:button></P>
</td>
</tr>
</table>
</asp:datagrid></div>
</form>
<asp:label id="label1" runat="server"></asp:label>