0

I have a problem while inserting records into SQL Server.

The string from C# doesn't show up in SQL Server as I'm inserting the sql just insert the first char

Example: If I insert 22222 in the data base just the first 2 inserted

Note I'm using a stored procedure for my first time.

This is my code:

public void insert_workshop(DateTime Pdate, string PTime, string PDesc, byte[] Img)
{
        SqlCommand cmd = new SqlCommand("[InsertWorkShops]", conn);
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add("@WorkshopsDate", SqlDbType.Date).Value = Pdate;
        cmd.Parameters.Add("@Time", SqlDbType.NVarChar).Value = PTime;
        cmd.Parameters.Add("@WorkshopsDescription", SqlDbType.NVarChar).Value = PDesc;
        cmd.Parameters.Add("@WorkshopsImage", SqlDbType.Image).Value = Img;
        cmd.Parameters.Add("@CreatedBy", SqlDbType.NVarChar).Value = 1;

        try
        {
            cmd.ExecuteNonQuery();
            Msg = "Add Done ";
        }
        catch
        {
            Msg = "Error While Adding";
        }

        WorkShopTransactions Ws = new WorkShopTransactions();
        Ws.insert_workshop(WorkShopDT.Value, txtWorshopTime.Text.ToString(),
                          txtWorkshopDescriptions.Text.ToString(), img);

T-SQL:

ALTER PROCEDURE [dbo].[InsertWorkShops] 
    @WorkshopsDate date,
    @Time nvarchar, 
    @WorkshopsDescription nvarchar,
    @WorkshopsImage image,
    @CreatedBy int
AS
BEGIN
    SET NOCOUNT ON;

    insert into Workshops 
    values(@WorkshopsDate, @Time, @WorkshopsDescription, @WorkshopsImage, @CreatedBy)
END
1
  • Also image is deprecated and why isn't time given a time datatype? Commented Sep 13, 2014 at 21:12

1 Answer 1

4

In the stored procedure, you have declared your nvarchar parameters without a length. They default to nvarchar(1).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks A lot every thing now is fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.