Data is not getting inserted

Last post 05-27-2008 7:24 AM by NamrataC. 14 replies.

Sort Posts:

  • Data is not getting inserted

    05-27-2008, 3:05 AM
    • Member
      399 point Member
    • NamrataC
    • Member since 05-10-2008, 7:34 AM
    • Posts 182

    Hi All,

    Please someone help to insert data into the database.

    In my code I've written a function for data insertion.

    Now this function  must return some value so I'm stating it as:

     int var=sqlcmd.ExecuteNonQuery();

    and then closing the connection.

    But the above statement throws the exception as it is unable to convert String variable to Guid even though I'm not using Guid anywhere all through my code.

     

    Thanking you in anticipation. 

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Data is not getting inserted

    05-27-2008, 3:48 AM
    • Participant
      1,832 point Participant
    • RicIshay
    • Member since 04-30-2008, 2:44 PM
    • India
    • Posts 329

     you have written a db funcation to insert data or  c#??

    Monika
    http://www.learnlinq.blogspot.com/
  • Re: Data is not getting inserted

    05-27-2008, 4:04 AM
    • Participant
      1,069 point Participant
    • elegantkvc
    • Member since 07-30-2007, 4:20 AM
    • chennai
    • Posts 247

    hi,

     

     I think u shoude use sqlcmd.ExecuteNonQuery() rather than assigning it to a variable..Because  ExecuteNonQuery doesnot return anything..

    cheers mate

    vijay
  • Re: Data is not getting inserted

    05-27-2008, 4:24 AM
    • Star
      8,888 point Star
    • satalaj
    • Member since 11-28-2007, 5:41 AM
    • Pune
    • Posts 1,772

    Hi,
     ExecuteNonQuery returns number of rows affected.
    if your store proc is like this and you are genataring pk automatically on an insert

    e.g.

    Insert Into XYZ(col1) values(@value1)

    select @@Identity  

    then you can execute scalar

    int id = Convert.ToInt32(command.ExecuteScalar());

  • Re: Data is not getting inserted

    05-27-2008, 4:37 AM
    • Member
      399 point Member
    • NamrataC
    • Member since 05-10-2008, 7:34 AM
    • Posts 182

    It gave me an InvalidCastException with the explaination: Failed to convert parameter value from a String to a Guid.

    This is the same exception I was getting when I was using ExecuteNonQuery(). 

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Data is not getting inserted

    05-27-2008, 5:28 AM
    • Member
      399 point Member
    • NamrataC
    • Member since 05-10-2008, 7:34 AM
    • Posts 182

    I have written a C# function which inserts the data from textboxes into the database table.

    I'm getting InvalidCastException. Please someone help me..

     

    Keep Coding.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Data is not getting inserted

    05-27-2008, 5:44 AM
    • Contributor
      2,160 point Contributor
    • Talib_dotnet
    • Member since 01-31-2008, 11:02 AM
    • Riyadh , Saudi Arabia
    • Posts 457

    Please post your  code  ,  so  as  to  help  you   out.........

    Click "Mark As Answer" if it helped you.


    TALIB ALI KHAN
    Software Engineer / Consultant
    MCTS


    Welcome to TALIB's World | My BLOG | More Asp.net Articles



  • Re: Data is not getting inserted

    05-27-2008, 6:00 AM
    • Member
      399 point Member
    • NamrataC
    • Member since 05-10-2008, 7:34 AM
    • Posts 182

    My code for the concern is as follows: 

     

    public bool InsertUserDetails(String psUID, String psFirstName, String psLastName)

    {

    DataAccess clsda = new DataAccess();
    String vsconnstr;

    vsconnstr = clsda.GetConnectionString();

    SqlConnection vsqlconn = new SqlConnection(vsconnstr);

    vsqlconn.Open();

    SqlCommand vsqlcmd = new SqlCommand();

    vsqlcmd.Connection = vsqlconn;

    vsqlcmd.CommandType = CommandType.Text;

    vsqlcmd.CommandText = "Insert Into investor values (@UserId, @FirstName, @LastName)";

    SqlParameter vpUserId = new SqlParameter();

    vpUserId.ParameterName = "@UserId";

    vpUserId.SqlDbType = SqlDbType.UniqueIdentifier;

    vpUserId.Direction = ParameterDirection.Input;

    vpUserId.Value = psUID;

    vsqlcmd.Parameters.Add(vpUserId);

    SqlParameter vpFirstName = new SqlParameter();

    vpFirstName.ParameterName = "@FirstName";

    vpFirstName.SqlDbType = SqlDbType.VarChar;

    vpFirstName.Size = 50;

    vpFirstName.Direction =
    ParameterDirection.Input;

    vpFirstName.Value = psFirstName;

    vsqlcmd.Parameters.Add(vpFirstName);

    SqlParameter vpLastName = new SqlParameter();

    vpLastName.ParameterName = "@LastName";

    vpLastName.SqlDbType = SqlDbType.VarChar;

    vpLastName.Size = 50;

    vpLastName.Direction =
    ParameterDirection.Input;

    vpLastName.Value = psLastName;

    vsqlcmd.Parameters.Add(vpLastName);

    int viresult = vsqlcmd.ExecuteNonQuery();

    vsqlconn.Close();

    if (viresult == 1)

    {

    return true;

    }

    else

    {

    return false;

    }

    }

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Data is not getting inserted

    05-27-2008, 6:13 AM
    • Participant
      1,069 point Participant
    • elegantkvc
    • Member since 07-30-2007, 4:20 AM
    • chennai
    • Posts 247

    hi,

            Use try catch whenever you are transacting with the database....ExecuteNonQuery return value is a boolean so use that type .

    cheersSmile

    vijay
  • Re: Data is not getting inserted

    05-27-2008, 6:19 AM
    • Contributor
      6,676 point Contributor
    • ramblor
    • Member since 03-13-2008, 10:03 AM
    • Posts 1,013

    elegantkvc:
    ExecuteNonQuery return value is a boolean so use that type .
     

    ExecuteNonQuery doesn't return a boolean, it returns an int value for the number of rows affected by the query.

    The problem probably lies with the UserID parameter which is set to a type of SqlDbType.UniqueIdentifier. Check the value you're setting this parameter to.

    "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
  • Re: Data is not getting inserted

    05-27-2008, 6:35 AM
    • Member
      399 point Member
    • NamrataC
    • Member since 05-10-2008, 7:34 AM
    • Posts 182

    Surely ExecuteNonQuery returns int value and I'm passing a String value to the UserID field.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Data is not getting inserted

    05-27-2008, 6:41 AM
    Answer
    • Participant
      1,255 point Participant
    • Abhishek khanna
    • Member since 05-01-2008, 6:01 PM
    • India
    • Posts 217

    Hi Namrata

    Please change

    vpUserId.SqlDbType = SqlDbType.UniqueIdentifier;

    to

    vpUserId.SqlDbType = SqlDbType.Varchar.

    Should solve your problem. Hope this helps.

     

  • Re: Data is not getting inserted

    05-27-2008, 6:47 AM
    Answer
    • Participant
      1,255 point Participant
    • Abhishek khanna
    • Member since 05-01-2008, 6:01 PM
    • India
    • Posts 217

    To be exact when you do the following:

    SqlParameter vpUserId = new SqlParameter();

    vpUserId.ParameterName = "@UserId";

    vpUserId.SqlDbType = SqlDbType.UniqueIdentifier;vpUserId.Direction = ParameterDirection.Input;

    vpUserId.Value = psUID;

     

    Please change

    vpUserId.SqlDbType = SqlDbType.UniqueIdentifier; 

    to

    vpUserId.SqlDbType = SqlDbType.VarChar.

  • Re: Data is not getting inserted

    05-27-2008, 7:19 AM
    • Participant
      884 point Participant
    • murthysrn
    • Member since 12-15-2006, 5:22 AM
    • Bangalore
    • Posts 314

    ParameterDirection.Output;

     

    Thanks & Regards,
    Murthy.
  • Re: Data is not getting inserted

    05-27-2008, 7:24 AM
    • Member
      399 point Member
    • NamrataC
    • Member since 05-10-2008, 7:34 AM
    • Posts 182

    Thank you this change has made my code working.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (15 items)