issue with popualting a message on asp:label server control

Discussion in 'ASP .Net Web Controls' started by Dhananjay, Dec 12, 2007.

  1. Dhananjay

    Dhananjay Guest

    hi all

    i have a problem on populating a message on asp:label server control.I
    am able to insert records into the table.but it is not populating any
    message like "Records entered successfully". i wanted to implement
    this .how to proceed can anybody tell me about this . i am using
    asp.net/vb.net 2.0

    here i am providing the code also:-

    vb code:-

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As
    EventArgs)


    If Page.IsPostBack = True Then

    Dim dbcon As SqlConnection
    dbcon = New SqlConnection("Data Source=.
    \SQLEXPRESS;AttachDbFilename=|DataDirectory|\HRISDB.mdf;Integrated
    Security=True;User Instance=True")
    Dim dbcom As New SqlCommand()
    dbcom.Connection = dbcon
    dbcom.CommandType = CommandType.Text
    dbcom.CommandText = "Insert into
    TalentTeam(TalentTeamName) values(@TalentTeamName)"
    dbcom.Parameters.Add("@TalentTeamName", SqlDbType.VarChar)

    dbcom.Parameters("@TalentTeamName").Value =
    txtRecruiterName.Text.ToString()

    If txtRecruiterName.Text.Length = 0 Then
    dbcom.Parameters("@TalentTeamName").Value =
    System.DBNull.Value
    Else
    dbcom.Parameters("@TalentTeamName").Value =
    txtRecruiterName.Text
    End If

    Try

    dbcon.Open()
    dbcom.ExecuteNonQuery()
    lblMessage.Visible = "True"
    txtRecruiterName.Text = ""
    Dim result As Integer
    result = dbcom.ExecuteNonQuery()

    If result = 0 Then
    lblMessage.Text = "Record not entered
    successfully"
    Else
    lblMessage.Text = "Record entered successfully"
    End If
    Catch ex As Exception
    Response.Write(ex.Message)
    Response.End()
    Finally
    If dbcon.State = ConnectionState.Open Then
    dbcon.Close()
    End If
    End Try


    Response.Redirect("~/AddForms/addRecruiter.aspx")
    Else
    lblMessage.Text = "Please enter Recruiter Name"

    End If

    End Sub

    ..aspx code :-

    <table>

    <tr>
    <td>
    <asp:panel ID="PanelEffectiveDate"
    runat="server" >
    <table>
    <tr>
    <td
    align="center">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Label
    ID="lblAddRecruiter" runat="server" Text="Recruiter Name" ></
    asp:Label>

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </td>
    <td align="right">
    <asp:TextBox
    ID="txtRecruiterName" runat="server" Text="" ></asp:TextBox>

    <asp:RequiredFieldValidator ID="rfvEnterRecruiterName" runat="server"
    ControlToValidate="txtRecruiterName" ErrorMessage="Plase Enter
    Recruiter Name"></asp:RequiredFieldValidator>
    </td>
    </tr>
    <tr>
    <td colspan="3" align="right">
    <asp:Button ID="btnAdd"
    runat="server" Text="ADD" OnClick="btnAdd_Click" />

    </td>
    </tr>
    <tr>
    <td colspan="2"
    align="center">
    <asp:Label ID="lblMessage"
    runat="server" Font-Bold="true" ForeColor="blue" ></asp:Label>
    </td>
    </tr>
    </table>
    </asp:panel>
    </td>
    </tr>
    </table>


    please help me know to solve this issue.


    Thanks in advance
    Dhananjay
     
    Dhananjay, Dec 12, 2007
    #1
    1. Advertising

  2. Dhananjay

    John Padilla Guest

    Dhananjay,

    I use a Literal control because you can insert HTML formatting into the text
    value and make a really nice summary message as compared to the boreing Label
    control.

    Here is an example reusable method i use to notify the user of
    update/instert statuses:


    private void SetLiteralMessage(string Message, string Color, Literal
    LiteralObject)
    {
    string curColor = "";

    if (Color.ToLower() == "red")
    {
    curColor = "color:#ff0000;";
    }
    else if (Color.ToLower() == "blue")
    {
    curColor = "color:#0066cc;";
    }

    LiteralObject.Text = "<span style=\"font-size:small; " + curColor +
    " font-family: Arial; \">" + Message + "</span>";
    }

    You would call this method in this way:

    Say you are Inserting a new record for Foo:

    if(ds.InsertFoo(FooName, FooPhone, FooAge) > 0)
    {
    SetLiteralMessage("Whew, youre lucky because the new record was
    sucessfully added!", "Blue", litYourLiteralControl
    }
    else //0 = no record was inserted
    {
    SetLiteralMessage("Oops, record was not added for some reason, don't call
    me I will call you!, "Red", litYourLiteralControl
    }

    Hope this helps!

    "Dhananjay" wrote:

    > hi all
    >
    > i have a problem on populating a message on asp:label server control.I
    > am able to insert records into the table.but it is not populating any
    > message like "Records entered successfully". i wanted to implement
    > this .how to proceed can anybody tell me about this . i am using
    > asp.net/vb.net 2.0
    >
    > here i am providing the code also:-
    >
    > vb code:-
    >
    > Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As
    > EventArgs)
    >
    >
    > If Page.IsPostBack = True Then
    >
    > Dim dbcon As SqlConnection
    > dbcon = New SqlConnection("Data Source=.
    > \SQLEXPRESS;AttachDbFilename=|DataDirectory|\HRISDB.mdf;Integrated
    > Security=True;User Instance=True")
    > Dim dbcom As New SqlCommand()
    > dbcom.Connection = dbcon
    > dbcom.CommandType = CommandType.Text
    > dbcom.CommandText = "Insert into
    > TalentTeam(TalentTeamName) values(@TalentTeamName)"
    > dbcom.Parameters.Add("@TalentTeamName", SqlDbType.VarChar)
    >
    > dbcom.Parameters("@TalentTeamName").Value =
    > txtRecruiterName.Text.ToString()
    >
    > If txtRecruiterName.Text.Length = 0 Then
    > dbcom.Parameters("@TalentTeamName").Value =
    > System.DBNull.Value
    > Else
    > dbcom.Parameters("@TalentTeamName").Value =
    > txtRecruiterName.Text
    > End If
    >
    > Try
    >
    > dbcon.Open()
    > dbcom.ExecuteNonQuery()
    > lblMessage.Visible = "True"
    > txtRecruiterName.Text = ""
    > Dim result As Integer
    > result = dbcom.ExecuteNonQuery()
    >
    > If result = 0 Then
    > lblMessage.Text = "Record not entered
    > successfully"
    > Else
    > lblMessage.Text = "Record entered successfully"
    > End If
    > Catch ex As Exception
    > Response.Write(ex.Message)
    > Response.End()
    > Finally
    > If dbcon.State = ConnectionState.Open Then
    > dbcon.Close()
    > End If
    > End Try
    >
    >
    > Response.Redirect("~/AddForms/addRecruiter.aspx")
    > Else
    > lblMessage.Text = "Please enter Recruiter Name"
    >
    > End If
    >
    > End Sub
    >
    > ..aspx code :-
    >
    > <table>
    >
    > <tr>
    > <td>
    > <asp:panel ID="PanelEffectiveDate"
    > runat="server" >
    > <table>
    > <tr>
    > <td
    > align="center">
    >
    >
    > <asp:Label
    > ID="lblAddRecruiter" runat="server" Text="Recruiter Name" ></
    > asp:Label>
    >
    >
    > </td>
    > <td align="right">
    > <asp:TextBox
    > ID="txtRecruiterName" runat="server" Text="" ></asp:TextBox>
    >
    > <asp:RequiredFieldValidator ID="rfvEnterRecruiterName" runat="server"
    > ControlToValidate="txtRecruiterName" ErrorMessage="Plase Enter
    > Recruiter Name"></asp:RequiredFieldValidator>
    > </td>
    > </tr>
    > <tr>
    > <td colspan="3" align="right">
    > <asp:Button ID="btnAdd"
    > runat="server" Text="ADD" OnClick="btnAdd_Click" />
    >
    > </td>
    > </tr>
    > <tr>
    > <td colspan="2"
    > align="center">
    > <asp:Label ID="lblMessage"
    > runat="server" Font-Bold="true" ForeColor="blue" ></asp:Label>
    > </td>
    > </tr>
    > </table>
    > </asp:panel>
    > </td>
    > </tr>
    > </table>
    >
    >
    > please help me know to solve this issue.
    >
    >
    > Thanks in advance
    > Dhananjay
    >
     
    John Padilla, Dec 15, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shapper
    Replies:
    2
    Views:
    752
    Scott Mitchell [MVP]
    May 7, 2005
  2. Josef K.

    <label><div></div></label> allowed?

    Josef K., May 20, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    13,552
    Josef K.
    May 22, 2005
  3. Praveen
    Replies:
    1
    Views:
    1,014
    Rob Williscroft
    Nov 5, 2006
  4. Dhananjay
    Replies:
    0
    Views:
    285
    Dhananjay
    Dec 12, 2007
  5. Knut Krueger
    Replies:
    8
    Views:
    293
    Knut Krueger
    Aug 20, 2013
Loading...

Share This Page