a bit confusion about parameterized query

Last post 07-03-2009 3:23 PM by qwe123kids. 6 replies.

Sort Posts:

  • a bit confusion about parameterized query

    07-03-2009, 1:39 AM
    • Member
      93 point Member
    • demoninside9
    • Member since 12-20-2007, 8:39 AM
    • New Delhi
    • Posts 652

    Hi all,

            I am having a datatable, where I am inserting a record.

            just let me know that when I use parameter in my insert or update query, so it is necessary,  that i have to mention SqlDbType as exact i have set in the column of that field in my Db table.

    means

    if I am adding a parameter like

    param = com.Parameters.Add("@NewsSectiont", SqlDbType.NVarChar, 250);

    so it's mean should I set exact same (NVarChar, 250) type in DB table for column "NewsSectiont".

    just let me know.

    Thank you all

    never let you down..always be happy...
  • Re: a bit confusion about parameterized query

    07-03-2009, 3:09 AM

    No - it's not necessary to specify the type.  You can use the AddwithValue() overload just to pass the name and the value:

    com.Parameters.AddWithValue("param1", TextBox1.Text);


    Regards Mike
    [MVP - ASP/ASP.NET]
    My site
  • Re: a bit confusion about parameterized query

    07-03-2009, 3:09 AM
    • All-Star
      21,209 point All-Star
    • qwe123kids
    • Member since 03-27-2008, 5:49 AM
    • Posts 3,653

    Hi,

    250 is Not important but Datatype yes


    Check the Below 2 links..

    http://aspnet101.com/aspnet101/tutorials.aspx?id=1

    http://aspnet101.com/aspnet101/tutorials.aspx?id=10

    Thanks
    Avinash Tiwari

    Remember to click “Mark as Answer” on the post, if it helps you.

    MY Blog

    Hacking Inside .net exe
  • Re: a bit confusion about parameterized query

    07-03-2009, 3:23 AM
    • Member
      93 point Member
    • demoninside9
    • Member since 12-20-2007, 8:39 AM
    • New Delhi
    • Posts 652

    thanx for replying

    U didnt specify type and length.... in ur doce line. u just specified control TextBox1.Text

    that means the below code is not ok(bt it si working fine..)

    string strSql = "update mtblCentreNews set NewsSection=?,NewsSectiont=?,NewsDate=?,NewsHeading=?,NewsDetail=?,Image1=? where NewsId="+lblNewsId.Text;
            OleDbCommand com = new OleDbCommand(strSql, con);
            OleDbParameter param;
            param = com.Parameters.Add("@NewsSection", OleDbType.VarChar, 250);
            param.Value = ddlNewsSection.SelectedValue;
            param = com.Parameters.Add("@NewsSectiont", OleDbType.VarChar, 250);
            param.Value = ddlNewsSection.SelectedItem.Text;
            param = com.Parameters.Add("@NewsDate", OleDbType.VarChar, 250);
            param.Value = ddlDay.SelectedValue + "/" + ddlMonth.SelectedItem.Text + "/" + ddlYear.SelectedValue;
            param = com.Parameters.Add("@NewsHeading", OleDbType.VarWChar);
            param.Value = txtHeading.Text;
            param = com.Parameters.Add("@NewsDetail", OleDbType.VarWChar);
            param.Value = txtMessage.Value;
            param = com.Parameters.Add("@Image1", OleDbType.VarWChar);
            param.Value = lblImage.Text;
            int row = com.ExecuteNonQuery();


    reply please

    thanx

    never let you down..always be happy...
  • Re: a bit confusion about parameterized query

    07-03-2009, 1:44 PM
    • Member
      93 point Member
    • demoninside9
    • Member since 12-20-2007, 8:39 AM
    • New Delhi
    • Posts 652

    may be my question be foolish

    But

    qwe123kids says datatype is important

    but you says datatype is not impotant.(according to your code explanation)

    which way i should follow...

    please tell me

    please

    thank you both

    thanx

    never let you down..always be happy...
  • Re: a bit confusion about parameterized query

    07-03-2009, 3:16 PM
    Answer

    There are a number of methods that come with the SqlParameterCollection class for adding parameters.  AddWithValue() does not accept the datatype or size as an argument.  The datatype is inferred from the value.  If you use one of the overloads that requires the type to be passed, you need to provide it.  Which method you choose to use is a matter of choice largely, although I'm sure that some development shops will insist on the really long version being used: http://msdn.microsoft.com/en-us/library/e5xwx8sc.aspx.

    Generally, I find life is too short. I prefer less typing.


    Regards Mike
    [MVP - ASP/ASP.NET]
    My site
  • Re: a bit confusion about parameterized query

    07-03-2009, 3:23 PM
    Answer
    • All-Star
      21,209 point All-Star
    • qwe123kids
    • Member since 03-27-2008, 5:49 AM
    • Posts 3,653

    If U use dataType in Your Application it helps In Unserstanding The Databse. Structure and Help U send proper Data on Runtime..

    Example : If data base Name in Nvarchar , In Application also Uhave provided It Will help andTo Prevent Runtime eror.. Because U Know what U have provide To Database..

    As I Told Numer is Not Important.. Because Nu7mer Limit Your Entery.Take Scenario If U have Deployed Application to 200 People and U have Decreased The Legth of Surname and All Application is having Single Database.. Know U cannot Go on Each PC Deploy The Code.



    Thanks
    Avinash Tiwari

    Remember to click “Mark as Answer” on the post, if it helps you.

    MY Blog

    Hacking Inside .net exe
Page 1 of 1 (7 items)