Select DropDownlist and search each row in data table

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<span style="color:#313c46; font-family:Arial,Helvetica,sans-serif; font-size:13px; line-height:16px; background-color:#fafafa
<p style=" Hi everyone
<p style=" Hi every one
<p style=" I have 4 dropdownlist to select Start time, Am/Pm1 ,End Time, AmPm2 . When i click button i want the command to search each row in<strong style=" TimeSlot table and if selected match i want to display Total charge at TotalLabel and
and at the same time selected TimeStart , AmPm1, TimeEnd, AmPm2, TotalCharge update into <strong style=" TempahanInfo table
<p style=" <span style=" <strong style=" TimeSlot Table
<p style=" TimeStart
<p style=" AmPm1
<p style=" TimeEnd
<p style=" AmPm1
<p style=" TotalCharge
<p style="
<p style=" <span style=" <strong style=" TempahanInfo Table
<p style=" Tempahan
<p style=" Hari
<p style=" Bulan
<p style=" Tahun
<p style=" Mula (TimeStart)
<p style=" AmPm1
<p style=" Tamat (TimeEnd)
<p style=" AmPm2
<p style=" Jumlah (TotalCharge) #null
<p style="
<p style=" When i try code below, the total charge not display in TotalLabel
<p style="
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; protected <span style="color:Blue; void TempahButton_Click(<span style="color:Blue; object sender, EventArgs e)
{
<span style="color:Blue; string display = <span style="color:#A31515; "RM = ";
SqlConnection conn = <span style="color:Blue; new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings[<span style="color:#A31515; "MytempahanConnectionString"].ConnectionString;
DataTable dt = <span style="color:Blue; new DataTable();
conn.Open();

<span style="color:Blue; string tempahan = Convert.ToString(TempahanDropDownList.SelectedItem);
<span style="color:Blue; string day = Convert.ToString(TarikhDropDownList.SelectedItem);
<span style="color:Blue; string month = Convert.ToString(BulanDropDownList.SelectedItem);
<span style="color:Blue; string year = Convert.ToString(TahunDropDownList.SelectedItem);
<span style="color:Blue; string starttime = Convert.ToString(MasaMulaDropDownList.SelectedItem);
<span style="color:Blue; string ampm1 = Convert.ToString(AmPm1DropDownList.SelectedItem);
<span style="color:Blue; string endtime = Convert.ToString(MasaTamatDropDownList.SelectedItem);
<span style="color:Blue; string ampm2 = Convert.ToString(AmPm2DropDownList.SelectedItem);

<span style="color:Blue; string sql = <span style="color:#A31515; "insert into TempahanInfo(Tempahan,Hari,Bulan,Tahun,Mula,AmPm1,Tamat,AmPm2) values(" +tempahan+<span style="color:#A31515; "," +day+ <span style="color:#A31515; "," +month+ <span style="color:#A31515; "," +year+ <span style="color:#A31515; "," +starttime+ <span style="color:#A31515; "," +ampm1+ <span style="color:#A31515; "," +endtime+ <span style="color:#A31515; "," +ampm2+<span style="color:#A31515; ")";
<span style="color:Blue; string sql1 = <span style="color:#A31515; "update TempahanInfo set Username=@Username where Tempahan=@Tempahan";

SqlCommand cmd = <span style="color:Blue; new SqlCommand(sql,conn);
SqlCommand cmd1 = <span style="color:Blue; new SqlCommand(sql1, conn);

cmd1.Parameters.AddWithValue(<span style="color:#A31515; "@Username",HiddenField.Value);
cmd1.Parameters.AddWithValue(<span style="color:#A31515; "@Tempahan",TempahanDropDownList.SelectedItem.Value);

cmd.ExecuteNonQuery();
cmd1.ExecuteNonQuery();

<span style="color:Blue; try
{
<span style="color:Blue; string sql2 = <span style="color:#A31515; "select TotalCharge from TimeTable where TimeStart = @TimeStart, AmPm1 = @AmPm1 , TimeEnd = @TimeEnd and AmPm2 = @AmPm2";
SqlCommand cmd2 = <span style="color:Blue; new SqlCommand(sql2, conn);
SqlDataAdapter ad = <span style="color:Blue; new SqlDataAdapter(cmd);
cmd2.Parameters.AddWithValue(<span style="color:#A31515; "@Timestart", MasaMulaDropDownList.SelectedItem);
cmd2.Parameters.AddWithValue(<span style="color:#A31515; "@AmPm1", AmPm1DropDownList.SelectedItem);
cmd2.Parameters.AddWithValue(<span style="color:#A31515; "@TimeEnd", MasaTamatDropDownList.SelectedItem);
cmd2.Parameters.AddWithValue(<span style="color:#A31515; "@AmPm2", AmPm2DropDownList.SelectedItem);
ad.Fill(dt);

<span style="color:Blue; if (dt.Rows.Count > 0)
{
TotalLabel.Text = dt.Rows[0][<span style="color:#A31515; "TotalCharge"].ToString();

}

<span style="color:Blue; else
{
MessageLabel.Text = <span style="color:#A31515; "Harap Maaf, tempahan penuh. Sila cuba lagi";
}

}
<span style="color:Blue; finally
{
conn.Close();
}

MessageLabel.Text = <span style="color:#A31515; "Tempahan berjaya disimpan";
RMLabel.Text = display.ToString();
}
[/code]
<br/>
<br/>



View the full article
 
Back
Top