Wednesday, May 30, 2012

Saving Date Using Microsoft SQL Helper Class


Saving Date Using Microsoft SQL Helper Class
   
First Import  :-   Imports Microsoft.ApplicationBlocks.Data

Private Sub SaveData()
        Dim param(12) As SqlParameter
        param(0) = New SqlParameter("@patient_Id", sCookPatMrn)
        param(1) = New SqlParameter("@eventId", sCookPatVisit)
        param(2) = New SqlParameter("@created_User", sCookPatMrn)
        param(3) = New SqlParameter("@modified_User", sCookPatMrn)
        param(4) = New SqlParameter("@VoidedVol", Int32.Parse(txtVoided.Text))
        param(5) = New SqlParameter("@MaxRate", Int32.Parse(txtMaxRate.Text))
        param(6) = New SqlParameter("@PVR", Int32.Parse(txtPVR.Text))
        param(7) = New SqlParameter("@Sensation", Int32.Parse(txtSensation.Text))
        param(8) = New SqlParameter("@MaxCapacity", Int32.Parse(txtMaxCapacity.Text))
        param(9) = New SqlParameter("@VoidingPressureAvg", Int32.Parse(txtPressureAvg.Text))
        param(10) = New SqlParameter("@VoidingPressureIso", Int32.Parse(txtPressureISO.Text))
        param(11) = New SqlParameter("@id", SqlDbType.Int, 0, ParameterDirection.Output, False, 0, 0, "ID", DataRowVersion.Default, Nothing)
        '' param(12) = New SqlParameter("@masterGroup", "CystoProstate")
      SqlHelper.ExecuteNonQuery(dbconn, CommandType.StoredProcedure, "Put_StoreProcedure_Name", param)
        If (IsDBNull(param(11).Value) = False) Then
            _cystoInstrumId = param(11).Value
        End If

    End Sub

Friday, May 25, 2012

SQL Listing all column names alphabetically



Dynamic Query to list all column Alphabetically.


DECLARE @QUERY VARCHAR(4000)SET @QUERY = 'SELECT '
 SELECT @QUERY = @QUERY + Column_name + ','  + CHAR(13) + CHAR(10)   FROM  INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'<TableName>' ORDER BY Column_name   

SET @QUERY = @QUERY + 'FROM  <TableName> '

PRINT @QUERY

Note:- Replace  <TableName> with your table name .Also if you want all fields in a string ( a,b,c,d  etc)  then remove  CHAR(13) + CHAR(10) 



Update :-
 Underscore (_) and quotes within SQL LIKE queries 
Suppose we have a field having data  'xxxx_S'   now We have to select  Table Columns having '_S'


DECLARE @QUERY VARCHAR(4000)
SET @QUERY = 'SELECT '

SELECT @QUERY = @QUERY + Column_name + ','
  FROM  INFORMATION_SCHEMA.COLUMNS
 WHERE TABLE_NAME = N'insuredinfo' AND Column_name LIKE '%' + char(13) + '_S' escape char(13)

   SET @QUERY = @QUERY + 'FROM insuredinfo'
PRINT @QUERY

Tuesday, February 28, 2012

Unchecking all checkboxes through JavaScript



Suppose we have 10 check boxes ..& checkboxes ID are chk1,chk2,chk3...
<script>
function selectOnlyThis(id) {
    for (var i = 0;i <= 10; i++)
    {
        document.getElementById("Chk" + i).checked = false;
    }
    
}
</script>
Call this function on a button click.

Friday, January 13, 2012

Send Mail in C#


Create a simple aspx form :

// Use Validation
<asp:TextBox ID="txtName" runat="server" />
<asp:TextBox ID="txtFrom" runat="server" />                                  
<asp:TextBox ID="txtBody" runat="server"  TextMode="MultiLine"  />
<asp:Button ID="Btn_SendMail" Text="Send Mail" runat="server" onclick="Btn_SendMail_Click" />                                



C#

Add Namespace using System.Net.Mail;



 protected void Btn_SendMail_Click(object sender, EventArgs e)
    {
        MailMessage mailObj = new MailMessage(
           txtFrom.Text, "x@xxx.org ", "Message from" + txtName.Text.ToString(), txtBody.Text);
        SmtpClient SMTPServer = new SmtpClient("xxx.org");      // Check your SMTP server Name
        try
        {
            SMTPServer.Send(mailObj);
// show a success pop up
        }
        catch (Exception ex)
        {
            Label1.Text = ex.ToString();
        }
    }


show pop up JavaScript message through C#

Add this following script in html code :



<script>
        function ShowMessage() {
            alert('Thank you for your submission!  To complete, please continue with PayPal transaction');
            window.location.href = 'BuyNow.aspx';
        }
   
    </script>


C# Code :-

ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "ShowMessage()", true);


Wednesday, December 21, 2011

Display Image from Database in ASP.NET

Scenario -

I have image names (eg:- abc.jpg) saved in a database & actual images lie in a folder in file system (~/ClientImages/).

Now I want to retrieve image from db & diplay it on webpage,in a div

Solution :-
 It is a very basic method,You can also fill the datagrid from CS file,But I opted dirty approach ie.SqlAdaperSource Take a Gridview choose - DataSource (ie :- SqlDataAddapter,go through the wizard ).

DataKeyNames="id" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="img12" runat="server" Width="600px" Height="400" ImageUrl='<%# Page.ResolveUrl(string.Format("~/ClientImages/{0}", Eval("image"))) %>' />

</ItemTemplate>
</asp:TemplateField>

</Columns>

</asp:GridView>


## SqlDataSource ##
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:XXXXConnectionStringName %>"
SelectCommand="Stored_Procedure_Name" SelectCommandType="StoredProcedure">
</asp:SqlDataSource

Monday, December 19, 2011

Checkbox Validation in ASP.NET

Validation of check box is little bit tricky  but you can validate with Java Script

Add this script in the page.


<script type="text/javascript">
    function validateCheckBox(source,args)
    {
        var cb=document.getElementById("Privacy1");
        if(!cb.checked)
         args.IsValid=false;
    }
    </script>
add a Checkbox Control

<asp:CheckBox ID="Privacy1" runat="server"></asp:CheckBox>
Now you need to add a custom Validator:-

  <asp:CustomValidator ID="CustomValidator1" runat="server" 
            ErrorMessage="CustomValidator" 
            ValidateEmptyText="True" ClientValidationFunction="validateCheckBox"></asp:CustomValidator>