Send parameters to ASP.Net SqlDataSource from class file using C#

Last Reply one month ago By pandeyism

Posted one month ago

I am using the code shown below to enter a record into SQL server.

I tested my code with the values that I entered manually, and it works correctly but if I get the value from person class it doesn't insert or update that parameter.

How can I do it  right?!

I have defined SqlDataSource and typed InsertParameters and UpdateParameters as shown below, 

                <asp:SqlDataSource ID="sqlDtSrcEmployees" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:KDUIS-v1ConnectionString %>"
                    SelectCommand ="SELECT * FROM [IMS_Person]"
                    InsertCommand="INSERT INTO [IMS_Person] ([GuidId], [FirstName], [LastName], [FatherName], [NationalCode], [BirthDate], [CertificateNo], [BirthPlace],
                [BirthCertificatePlace], [Gender], [ReligionId], [MaritalId], [Mobile], [Email], [CreateDate], [Description])
                    VALUES (@GuidId, @FirstName, @LastName, @FatherName, @NationalCode, @BirthDate, @CertificateNo, @BirthPlace, @BirthCertificatePlace, @Gender, @ReligionId,
                @MaritalID, @Mobile, @Email, @CreateDate,@Description)"
 		UpdateCommand="Update IMS_Person SET [FirstName]=@FirstName,[LastName]=@LastName,[FatherName]=@FatherName,[NationalCode]=@NationalCode,
                    [BirthDate]=@BirthDate,[CertificateNo]=@CertificateNo,[BirthPlace]=@BirthPlace,[BirthCertificatePlace]=@BirthCertificatePlace,[Gender]=@Gender,
                    [ReligionId]=@ReligionId,[MaritalId]=@MaritalId,[Mobile]=@Mobile,[Email]=@Email,[Description]=@Description WHERE id=@id"
                     OnUpdating="sqlDtSrcUpdate_Updating">
                   
                    OnInserting="sqlDtSrcEmployees_Inserting"
        	    OnUpdating="sqlDtSrcUpdate_Updating">
                    <InsertParameters>
                        <asp:Parameter Name="GuidId" />
                        <asp:ControlParameter ControlID="txtName" Name="FirstName" PropertyName="Text"  Type="String" />
                        <asp:ControlParameter ControlID="txtFamily" Name="LastName" PropertyName="Text" Type="String" />
                        <asp:ControlParameter ControlID="txtFathername" Name="FatherName" PropertyName="Text" Type="String" />
                        <asp:ControlParameter ControlID="txtNationalcode" Name="NationalCode" PropertyName="Text" Type="String" />
                        <asp:Parameter  Name="BirthDate" Type="DateTime" />
                        <asp:ControlParameter ControlID="txtCertificateNo" Name="CertificateNo" PropertyName="Text" Type="String" />
                        <asp:ControlParameter ControlID="txtBirthPlace" Name="BirthPlace" PropertyName="Text" Type="String" />
                        <asp:ControlParameter ControlID="txtBirthCertificatePlace" Name="BirthCertificatePlace" PropertyName="Text" Type="String" />
                        <asp:Parameter  Name="Gender" Type="Int32" />
                        <asp:Parameter Name="ReligionId" Type="Int32" />
                        <asp:Parameter Name="MaritalID" Type="Int32" />
                        <asp:ControlParameter ControlID="txtMobile" Name="Mobile" PropertyName="Text" Type="String" />
                        <asp:ControlParameter ControlID="txtEmail" Name="Email" PropertyName="Text" Type="String" />
                        <asp:Parameter  Name="CreateDate"  Type="DateTime" />
                        <asp:ControlParameter ControlID="txtDescription" Name="Description" PropertyName="Text"
                            Type="String" />
                    </InsertParameters>

      		    <UpdateParameters>
                        <asp:ControlParameter ControlID="LblPersonID" Name="id" Type="Int32" />
                        <asp:ControlParameter ControlID="txtName" Name="FirstName" PropertyName="Text" Type="String" />
                        <asp:ControlParameter ControlID="txtFamily" Name="LastName" PropertyName="Text" Type="String" />
                        <asp:ControlParameter ControlID="txtFathername" Name="FatherName" PropertyName="Text" Type="String" />
                        <asp:ControlParameter ControlID="txtNationalcode" Name="NationalCode" PropertyName="Text" Type="String" />
                        <asp:ControlParameter ControlID="txtCertificateNo" Name="CertificateNo" PropertyName="Text" Type="String" />
                        <asp:ControlParameter ControlID="txtBirthPlace" Name="BirthPlace" PropertyName="Text" Type="String" />
                        <asp:ControlParameter ControlID="txtBirthCertificatePlace" Name="BirthCertificatePlace" PropertyName="Text" Type="String" />
                        <asp:Parameter Name="BirthDate" Type="DateTime" />
                        <asp:Parameter Name="Gender" Type="Int32" />
                        <asp:Parameter Name="ReligionId" Type="Int32" />
                        <asp:Parameter Name="MaritalID" Type="Int32" />
                        <asp:ControlParameter ControlID="txtMobile" Name="Mobile" PropertyName="Text" Type="String" />
                        <asp:ControlParameter ControlID="txtEmail" Name="Email" PropertyName="Text" Type="String" />
                        <asp:ControlParameter ControlID="txtDescription" Name="Description" PropertyName="Text" Type="String" />
                    </UpdateParameters>
                </asp:SqlDataSource>

I get this parameter GuidId, Gender, ReligionId, MaritalID and CreateDate  from a class person that I write it before

     public int Gender(string genderlist)
        {

            switch (genderlist)
            {
                case "woman":
                    return 0;

                case "man":
                    return 1;

                default:
                    return -1;

            }

        }


     public int Religion(string religionlist)
        {
            switch (religionlist)
            {
                case "Muslim":
                    return 1;

                case "Christianity":
                    return 2;

                default:
                    return -1;

            }

        }



        public int Marital(string maritallist)
        {
            switch (maritallist)
            {
                case "Single":
                    return 1;

                case "Married":
                    return 2;
                default:
                    return -1;

            }

        }


      public Guid GenerateGuid()
        {
            Guid guidid = System.Guid.NewGuid();
            return guidid;

        }



        public string CreateDateTime()
        {
            ///////date &time of insert onfo of person
            ///// use of datetime utc
            TimeZoneInfo timeZoneInfo;

            DateTime dateTime;

            //Set the time zone information to Iran Standard Time

            timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Iran Standard Time");

            //Get date and time in Iran Standard Time

            dateTime = TimeZoneInfo.ConvertTime(DateTime.UtcNow, timeZoneInfo);

            //Print out the date and time
            string datetimeinsert = dateTime.ToString();
            return datetimeinsert;
        }

 

 

You are viewing reply posted by: pandeyism one month ago.
Posted one month ago Modified on one month ago

Hey dorsa,

Please refer below sample.

HTML

<asp:SqlDataSource ID="sqlDtSrcEmployees" runat="server"
 ConnectionString="<%$ ConnectionStrings:constr %>"
 SelectCommand="SELECT * FROM [Customers]"
 InsertCommand="INSERT INTO [Customers] ([Name], [Country]) VALUES (@Name, @Country)">
 <InsertParameters>
 <asp:ControlParameter ControlID="txtName" Name="Name" PropertyName="Text" Type="String" />
 <asp:ControlParameter ControlID="txtCountry" Name="Country" PropertyName="Text" Type="String" />
 </InsertParameters>
</asp:SqlDataSource>
Name: 
<asp:TextBox runat="server" ID="txtName" />
Country:
<asp:TextBox runat="server" ID="txtCountry" />
<asp:Button Text="Insert" runat="server" OnClick="Insert" />

Code

Person.cs

public class Person
{
    public string Name(string namee)
    {
        string name = namee;
        return name;
    }
    public string Country(string countri)
    {

        string country = countri;
        return country;

    }

}

CS.aspx.cs

protected void Insert(object sender, EventArgs e)
{
    sqlDtSrcEmployees.Insert();

}
protected void sqlDtSrcEmployees_Inserting(object sender, SqlDataSourceCommandEventArgs e)
{
    Person p = new Person();
    string name = p.Name(txtName.Text);
    string country = p.Country(txtCountry.Text);
    e.Command.Parameters["@Name"].Value = name;
    e.Command.Parameters["@Country"].Value = country;
}