Friday, December 28, 2012

Inserting Multiple Records into Database in one shot

you need to create a Sql Type for the table you will pass in :


CREATE TYPE dbo.DistrictsType AS TABLE
    ( DistrictID int, StoreID int )
and a StoredProcedure that will insert the data from the datatable passed in
CREATE PROCEDURE usp_InsertDistricts 
(@tvpNewDistricts dbo.DistrictsType READONLY)
AS
BEGIN
    INSERT INTO dbo.Districts (DistrictID, StoreID)
    SELECT dt.DistrictID, dt.StoreID FROM @tvpNewDistricts AS dt;
END

then, back to your code you pass the district into the storedprocedure

DataTable dtDistricts = ConvertListToDataTable(Districts);
SqlCommand insertCommand = new SqlCommand("usp_InsertDistricts", sqlConnection);
SqlParameter p1 = insertCommand.Parameters.AddWithValue("@tvpNewDistricts", dtDistricts);
p1.SqlDbType = SqlDbType.Structured;
p1.TypeName = "dbo.DistrictsType";
insertCommand.ExecuteNonQuery();
2nd way to this is by Xml as datatype in store procedure :
ALTER PROCEDURE [dbo].[insertStore]
@XMLDATA xml,
@name varchar(50),
@image datatype
 AS
 Begin
  INSERT INTO Store
  (name
   ,image
  )
Select XMLDATA.item.value('@name[1]', 'varchar(10)') AS Name,   
XMLDATA.item.value('@image[1]', 'yourData type') AS Image
FROM @XMLDATA.nodes('//Stores/InsertList/Store') AS XMLDATA(item)
END

Similarly you can write for update and delete .In C# u need to create the xml
public  string GenerateXML(List<District> Districts)
 var xml = new StringBuilder();
 var insertxml = new StringBuilder();
 xml.Append("<Stores>");
 for (var i = 0; i < Districts.Count; i++)
        { var obj = Districts[i];
          insertxml.Append("<Store");
          insertxml.Append(" Name=\"" + obj.Name  + "\" ");
          insertxml.Append(" Image=\"" + obj.Image + "\" ");
          insertxml.Append(" />");
        }
xml.Append("<InsertList>");
xml.Append(insertxml.ToString());
xml.Append("</InsertList>");

SqlCommand cmd= new SqlCommand("insertStore",connectionString);
cmd.CommandType=CommandType.StoredProcedure;
SqlParameter param = new SqlParameter ();
param.ParameterName ="@XMLData";
param.value=xml;
paramter.Add(param);
cmd.ExecuteNonQuery();

ref:
http://stackoverflow.com/questions/11376498/a-better-way-to-achieve-insert-without-hitting-the-database-multiple-times

Wednesday, December 12, 2012

How to add string in double quota


  Image = Image + "\"" + "  <img height='60' width='60' src='Upload/saAdsImages/wmSmall_" + objProp[i].PropertyTitle.ToString() + "' />" + "\"";
               

Sunday, December 9, 2012

Difference between ref and out parameters in C#

Ref and Out Parameters: 

Both the parameters passed by reference, While for the Ref Parameter you need to initialize it before passing to the function and out parameter you do not need to initialize before passing to function. 

you need to assign values into these parameter before returning to the function. 

Ref (initialize the variable) 
int getal = 0; 
Fun_RefTest(ref getal); 


Out (no need to initialize the variable) 
int getal; 
Fun_OutTest(out getal); 


The out and the ref parameters are used to return values in the same variables, that you pass an an argument of a method. These both parameters are very useful when your method needs to return more than one values. 

In this article, I will explain how do you use these parameters in your C# applications. 

The out Parameter 

The out parameter can be used to return the values in the same variable passed as a parameter of the method. Any changes made to the parameter will be reflected in the variable. 

public class mathClass 

public static int TestOut(out int iVal1, out int iVal2) 

iVal1 = 10; 
iVal2 = 20; 
return 0; 

public static void Main() 

int i, j; // variable need not be initialized 
Console.WriteLine(TestOut(out i, out j)); 
Console.WriteLine(i); 
Console.WriteLine(j); 



The ref parameter 

The ref keyword on a method parameter causes a method to refer to the same variable that was passed as an input parameter for the same method. If you do any changes to the variable, they will be reflected in the variable. 

You can even use ref for more than one method parameters. 

namespace TestRefP 

using System; 
public class myClass 

public static void RefTest(ref int iVal1 ) 

iVal1 += 2; 

public static void Main() 

int i; // variable need to be initialized 
i = 3; 
RefTest(ref i ); 
Console.WriteLine(i); 


Wednesday, December 5, 2012

find url from string and convert all the url in lower case


Here I show a simple variable that receives the HTML string and then extracts all the links in match collection class .

   Dim txt As String = ds.Tables(0).Rows(0)("matter").ToString()
                Dim regx As New Regex("href=\""(.*?)\""", RegexOptions.IgnoreCase)
                Dim mactches As MatchCollection = regx.Matches(txt)
                For Each match As Match In mactches
                    Dim url = match.Groups("url").Value
                    txt = txt.Replace(match.Value, match.Value.ToString().ToLower())
                Next

Note -If you find this article helpfull plz like or post comment on this article.