selecting one row from datagrid

Discussion in 'ASP .Net' started by rcoco, Jan 10, 2007.

  1. rcoco

    rcoco Guest

    I want to select one Row from a datagrid so I create a
    textBbox(txtname) and Button for the user to write the name in textBox
    and By the click of the button it select the corresponding row. But
    it's not selecting the row neighther I'm getting any mistake message
    I'm I missng some thing? I wrote the code on Button_Click sqlCommand as
    follows:
    SqlCommand myCommand = new SqlCommand("SELECT * from employee where
    fullname Like %+txtname+%" ,con);
    Thank you.
    rcoco, Jan 10, 2007
    #1
    1. Advertising

  2. Well, I'm not quite sure if you made mistake when you wrote this post, but,
    according to
    what you wrote here, mistake is in string concatenation. You should write:
    SqlCommand myCommand = new SqlCommand("SELECT * from employee where
    fullname Like %" + txtname + "%" ,con); // Note opening and closing quotes

    Furthermore, if txtname is id of textbox (and not the string) then you
    should write txtname.Text
    This much I could write, because you haven't supplied much info...

    "rcoco" <> wrote in message
    news:...
    >I want to select one Row from a datagrid so I create a
    > textBbox(txtname) and Button for the user to write the name in textBox
    > and By the click of the button it select the corresponding row. But
    > it's not selecting the row neighther I'm getting any mistake message
    > I'm I missng some thing? I wrote the code on Button_Click sqlCommand as
    > follows:
    > SqlCommand myCommand = new SqlCommand("SELECT * from employee where
    > fullname Like %+txtname+%" ,con);
    > Thank you.
    >
    Rastko Soskic, Jan 10, 2007
    #2
    1. Advertising

  3. rcoco

    rcoco Guest

    Thanks,
    the whole code actually goes like this as per now hope you could hlp
    me:

    try
    {
    con.Open();
    SqlCommand myCommand = new SqlCommand();
    myCommand.Connection=con;
    myCommand.CommandText="SELECT * from employee where fullname Like
    %"+"%";
    SqlDataAdapter myAdapter=new SqlDataAdapter(myCommand);
    DataSet ds = new DataSet();
    myAdapter.Fill(ds,"isp_email.staff");
    con.Open();
    myCommand.ExecuteNonQuery();
    dgupdate.DataSource=ds;
    dgupdate.DataBind();
    con.Close();
    }
    catch(Exception err)
    {
    Console.WriteLine(err.Message);
    }
    finally
    {
    con.Close();
    }
    Thank you
    rcoco, Jan 10, 2007
    #3
  4. rcoco

    bpd Guest

    I believe you just need to remove the @ in front of txtname.text.

    rcoco wrote:
    > Thanks,
    > the whole code actually goes like this as per now hope you could hlp
    > me:
    >
    > try
    > {
    > con.Open();
    > SqlCommand myCommand = new SqlCommand();
    > myCommand.Connection=con;
    > myCommand.CommandText="SELECT * from employee where fullname Like
    > %"+"%";
    > SqlDataAdapter myAdapter=new SqlDataAdapter(myCommand);
    > DataSet ds = new DataSet();
    > myAdapter.Fill(ds,"isp_email.staff");
    > con.Open();
    > myCommand.ExecuteNonQuery();
    > dgupdate.DataSource=ds;
    > dgupdate.DataBind();
    > con.Close();
    > }
    > catch(Exception err)
    > {
    > Console.WriteLine(err.Message);
    > }
    > finally
    > {
    > con.Close();
    > }
    > Thank you
    bpd, Jan 10, 2007
    #4
  5. Well, as bpd said, first you should remove @.
    Then, after line: "myAdapter.Fill..." ...
    there is no need to perform opening con, executing command and closing con
    again,
    (unless you are trying to do something else with that?)
    adapter should do it for you, but there is nothing wrong if you are opening
    and closing manually.

    So, your try block should be something like this...
    try
    {
    con.Open(); // Assuming that con object is properly created with correct
    connection string
    SqlCommand myCommand = new SqlCommand();
    myCommand.Connection=con;
    myCommand.CommandText="SELECT * from employee where fullname Like %" +
    txtname.Text + "%";
    SqlDataAdapter myAdapter=new SqlDataAdapter(myCommand);
    DataSet ds = new DataSet();
    myAdapter.Fill(ds,"isp_email.staff");
    con.Close();
    dgupdate.DataSource=ds;
    dgupdate.DataBind();
    }

    "bpd" <> wrote in message
    news:...
    >I believe you just need to remove the @ in front of txtname.text.
    >
    > rcoco wrote:
    >> Thanks,
    >> the whole code actually goes like this as per now hope you could hlp
    >> me:
    >>
    >> try
    >> {
    >> con.Open();
    >> SqlCommand myCommand = new SqlCommand();
    >> myCommand.Connection=con;
    >> myCommand.CommandText="SELECT * from employee where fullname Like
    >> %"+"%";
    >> SqlDataAdapter myAdapter=new SqlDataAdapter(myCommand);
    >> DataSet ds = new DataSet();
    >> myAdapter.Fill(ds,"isp_email.staff");
    >> con.Open();
    >> myCommand.ExecuteNonQuery();
    >> dgupdate.DataSource=ds;
    >> dgupdate.DataBind();
    >> con.Close();
    >> }
    >> catch(Exception err)
    >> {
    >> Console.WriteLine(err.Message);
    >> }
    >> finally
    >> {
    >> con.Close();
    >> }
    >> Thank you

    >
    Rastko Soskic, Jan 11, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stu
    Replies:
    1
    Views:
    495
    makthar
    Aug 1, 2003
  2. =?Utf-8?B?QmluIFNvbmcsIE1DUA==?=

    RE: Selecting a DataGrid row upon Page_Load

    =?Utf-8?B?QmluIFNvbmcsIE1DUA==?=, May 14, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    403
    =?Utf-8?B?QmluIFNvbmcsIE1DUA==?=
    May 14, 2004
  3. Scott Mitchell [MVP]

    Re: Selecting a DataGrid row upon Page_Load

    Scott Mitchell [MVP], May 14, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    415
    Scott Mitchell [MVP]
    May 14, 2004
  4. Chip

    Selecting a row within a row

    Chip, Feb 10, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    353
  5. misiek
    Replies:
    0
    Views:
    2,263
    misiek
    Feb 20, 2006
Loading...

Share This Page