SQL Server vs. C# Data Types


SQL data type C# data type

binary, tinyint
Byte
varbinary Byte[]
smallint Int16, Short
bigint Int64, long
bit Bool
int Int32, int
money, smallmoney Decimal
float Double
decimal, numeric Decimal
nchar, nvarchar String, Char Array
real Single
sql_variant Object
time TimeSpan
date DateTime, Nullable<DateTime>
uniqueidentifier Guid
The Datatypes C# hasn't but SQL has
Please visit to our YouTube Channel for .net and other tech tutorial Thanks
varchar None
xml None
image None
table None
timestamp None
text None
cursor None

Inserting records into a database table using a stored procedure:
1. Create Table

CREATE TABLE [dbo].[RegisteredClients] (
    [ ClientUserName]        VARCHAR (20)    NOT NULL,
    [MasterDistributor]      VARCHAR (20)    NOT NULL,);

2. Create Stored Procedure

CREATE PROCEDURE [dbo].[sp_InsertClientData]
@ClientUserName VARCHAR (20),
@MasterDistributor      VARCHAR (20),
AS
Insert into RegisteredClients values
(
@ClientUserName,
@MasterDistributor,
)
RETURN 0

3. C# Code

string strConnString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            SqlConnection con = new SqlConnection(strConnString);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_InsertClientData";

            cmd.Parameters.Add("@ClientUserName", SqlDbType.VarChar).Value = txtClientUserName.Text;
            cmd.Parameters.Add("@MasterDistributor", SqlDbType.VarChar).Value = ddlMasterDistributor.SelectedItem.Value;

Please visit to our YouTube Channel for .net and other tech tutorial Thanks

Inserting null values into database from TextBox using StoredProcedure:

1. First you have to allow null to your column what you have created in table
2. Allow null to that column too in stored procedure parameter like this:

create procedure sp_PassNullValueInSp
(@id varhcra(10), @name varchar(10)= null)

3.c# code
if(txtMobileNumber.Text!="")
cmd.Parameters.Add("@name",SqlDbType.BigInt).Value
= txtname.Text);

Please visit to our YouTube Channel for .net and other tech tutorial Thanks
  
                
 


How I directly pass the checkbox value to database as a bit:


cmd.Parameters.Add("@isapproved", SqlDbType.Bit).Value = chkbox.Checked; 

OR

bool isapproved= checkbox.checked;

cmd.Parameters.Add("@isapproved", SqlDbType.Bitisapproved ? 1:0).Valueisapproved;

Please visit to our YouTube Channel for .net and other tech tutorial Thanks


Connect to LocalDB existing in App_Data Folder in ASP.net Project Solution:
<connectionStrings>
<add name="ConnectionName"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;
AttachDbFilename=|DataDirectory|DatabaseName.mdf;
Integrated Security=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>

And please don't use  this (User Instance=True;) in your connection string.
Use above connection string as it is, just change your connection name to as you want to name it and Data base file name to your database file name (.mdf).
It will work fine, Thanks...

Please visit to our YouTube Channel for .net and other tech tutorial Thanks
 


The user instance login flag is not supported:

Its a feature of SQL Server Express edition, that is why it is not supported to your SQL Edition.
Your Connection string has user Instance attribute, Remove  the "User Instance=True;".

Please visit to our YouTube Channel for .net and other Tech tutorial, Thanks

Connection String for database which exists in asp.net Project: 

If you have your database (.mdf) in App_Data Folder then it will work fine.

<connectionStrings>
  <add name="ConnectionName"
    connectionString="Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|DatabaseName.mdf;
Integrated Security=True;
User Instance=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>

Please visit to our YouTube Channel for .net and othe tech tutorial Thanks

Button OnClick event not firing in asp.net Project:

You must be copied this method from other application. don't you? So you need to delete the event and event name assigned to the button from both of the files .cs and .aspx, 

Step 1:  First Change the Button name property to what you want to keep the name of click event function, 

Step 2: Go to design and go to button hit F4 to open properties window. 

Step 3: In event tab go to onClick event double click next to it, it will generate event and it automatically assigns event name to the button according to the name what you name it in name property. 

It will work now... Thanks,