C# method to add newline or Br to string after n charectors

enter value to split and delimitter to split

public string splitString(string value, int Count)
{
int result = 0;
string new_value = “”;
foreach (char c in value)
{
result++;
new_value += c.ToString();
if (result == Count)
{
new_value += “
“;
result = 0;
}
}
return new_value;
}

Posted in Uncategorized | Tagged | Leave a comment

Advanced Ado.net

Example of basic way of calling stored procedure

private void button1_Click(object sender, EventArgs e) {
  using (SqlConnection con = new SqlConnection(dc.Con)) {
    using (SqlCommand cmd = new SqlCommand("sp_Add_contact", con)) {
      cmd.CommandType = CommandType.StoredProcedure;

      cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text;
      cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLastName.Text;

      con.Open();
      cmd.ExecuteNonQuery();
    }
  }
}

The Other way of calling sp
using (SqlConnection con = new SqlConnection(dc.Con))
            {
                using (SqlCommand cmd = new SqlCommand("SP_ADD", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@FirstName", txtfirstname);
                    cmd.Parameters.AddWithValue("@LastName", txtlastname);
                    con.Open();
                    cmd.ExecuteNonQuery();
                }

            }


Advanced Program to call OutPut Parameter value from sp
using( SqlConnection cn = new SqlConnection("server=(local);Database=Northwind;user id=sa;password=;")) 
    { 
        SqlCommand cmd = new SqlCommand("CustOrderOne", cn); 
        cmd.CommandType=CommandType.StoredProcedure ; 

        SqlParameter parm= new SqlParameter("@CustomerID",SqlDbType.NChar) ; 
        parm.Value="ALFKI"; 
        parm.Direction =ParameterDirection.Input ; 
        cmd.Parameters.Add(parm); 

        SqlParameter parm2= new SqlParameter("@ProductName",SqlDbType.VarChar); 
        parm2.Size=50; 
        parm2.Direction=ParameterDirection.Output; 
        cmd.Parameters.Add(parm2); 

        SqlParameter parm3=new SqlParameter("@Quantity",SqlDbType.Int); 
        parm3.Direction=ParameterDirection.Output; 
        cmd.Parameters.Add(parm3);

        cn.Open(); 
        cmd.ExecuteNonQuery(); 
        cn.Close(); 

        Console.WriteLine(cmd.Parameters["@ProductName"].Value); 
        Console.WriteLine(cmd.Parameters["@Quantity"].Value.ToString());
        Console.ReadLine(); 
    } 

Sp with Output Parameter
Create  PROCEDURE GetstudentnameInOutputVariable
(

@studentid INT,                       
@studentname VARCHAR(200)  OUT    
)
AS
BEGIN
SELECT @studentname= Firstname+' '+Lastname FROM tbl_Students WHERE studentid=@studentid
END

Getting Value from the above sp using the following Query
Declare @Studentname as nvarchar(200)   
Declare @Studentemail as nvarchar(50)    
Execute GetstudentnameInOutputVariable 1 , @Studentname output
select @Studentname
Posted in Uncategorized | Leave a comment

How to work with Grid view

Creating Customized Grid View

<asp:GridView ID=”GV” OnRowDataBound=”Gv_RowDataBound” Width=”96%” runat=”server”
AutoGenerateColumns=”False” ShowFooter=”True” BorderWidth=”1px” OnRowEditing=”gv_RowEditing”
OnRowDataBound=”gv_RowDataBound” OnRowUpdating=”gv_RowUpdating” OnRowDeleting=”gv_RowDeleting”
OnRowCancelingEdit=”gv_RowCancelingEdit” OnRowCommand=”OnRowCommandBTC” TabIndex=”3″
RowStyle-HorizontalAlign=”Center”>
</asp:GridView>

 

DataTable BTC = ViewBTCData().Tables[0]; // data from some data table source

gvBenefitType.DataSource = null;
gvBenefitType.DataSource = BTC;
gvBenefitType.DataBind();

Public void Gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.EmptyDataRow)
{

DataRowView datarow = (DataRowView)e.Row.DataItem;
GridViewRow currentRow = (GridViewRow)e.Row;
Label lblLastModifiedBy = (Label)e.Row.FindControl(“lblLastModified”);
if (datarow[“LastModified”].ToString() != string.Empty)
{
lblLastModified.Text = datarow[“LastModified”].ToString() + “&nbsp;”;
}

}

}

Posted in Uncategorized | Leave a comment

Some Times My DotNet Code works fine in Local but not when deployed

HI friends ,

i came through the scenario where my code was working as expected in local when i run it through local host but it was not working as expected when i deployed the code into IIS server.

Generally this issues happen for several reasons one among and most common among them is while using sessions

Example :

if i wrote a peace of code like this

Session[“Test”] =”1″; // here i am assigning session to a string value “1”

so when using it in a condition or so

if i am testing it some what like

Session[“Test”] =”1″;

if(Session[“Test”] == “1”){

Response.Write(“Session named Test value is 1”);

}else

{

Response.Write(“Session named Test value is not 1”);

}

when i run this code in local i will get output like this

“Session named Test value is 1”

but if i run this code after deploying into server i will get output like this

“Session named Test value is not 1”

basically the reason for this is when we deploy this code in server it wont convert session object into string to compare where as this thing happens when you are running code in local machine… i really don’t know the reason behind this behavior. most of the times it will take several hours to find the reason so here in order to get this code work in server also we need to convert the session object which is the solution to this problem

i will solve this problem by writing the if condition like this

if(Session[“Test”].ToString() == “1”)

here i am converting session object into string to compare which will work in both places

 

i will update other reasons also which are going to cause this issue in my next post ……

 

Regards

Santosh Kakani

 

Posted in Uncategorized | Tagged , , , | Leave a comment

All Folders in Pendrive are Hidden by virus how to Fix it

Hi Friends

This Topic is not related to development its just used genereally

recently i got a problem in my pen drive that  all the folders inside that are hidden and was not visible even by selecting show hidden folders in explorer options so i googled the solution for the same and got a good solution for that

Steps to Get your folders back

1) Start-> Run–> Enter “CMD”

2) a black dos prompt will open please check into your pendrive by typing “I:” (here I is my pendrive drive)

3) after that type the following code

attrib -h -r -s /s /d I:\*.* (Here I is my Pendrive drive name in system)

after that your problem is solved you can see your hidden folders as normal folders

have  a nice Day

Regards

Santosh Kakani

Posted in Uncategorized | Leave a comment

Restrict Printscreen and copy Paste in website

Hi friends

 i got this peace of usefull code while surfing internet

the code mentioned here is used to block print screen

<html>
<head>
<script type=”text/javascript”>
function start(){
 block = setInterval(“window.clipboardData.setData(‘text’,”)”,20);  
}
function stop(){
 clearInterval(block);
}
</script>
</head>

<body onload=”start();”>
YOU TRY TO COPY AND PASTE THIS SCREEN, AND ALL THE ACTIVE SCREENS
<input type=”button” onclick=”stop();” value=”stop blocking copy/paste!”>

</body>
</html>

 

Posted in Uncategorized | Leave a comment

Handling undefined elements in Javascript

Some times you may verify the element for null but if the element is undefined then null condition fails so best practice is to check for null and undefined.

checking for null

if(document.getElementsByName("TxtElement1")  == null)
{
alert("the element is null");
}

checking for undefined

if(document.getElementsByName("TxtElement1").value == 'undefined')
{
alert("the element is undefined");
}

Posted in Uncategorized | Tagged , | Leave a comment