CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2011
    Posts
    7

    [RESOLVED] Restrict text input

    hi,

    i am new in C# , i have 2 text boxes in which the user will enter the department and name,
    1- I want the application to retrict the user to only enter letters not numbers . This is my following code:
    public string PMEWDirectorates_Insert(string PMEWDirectorate_Name, string PMEWDirectorate_Head)
    {
    SqlConnection con = new SqlConnection();
    SqlCommand com = new SqlCommand();
    // SqlDataAdapter da = new SqlDataAdapter();

    try
    {

    con.ConnectionString = conString;
    con.Open();
    com.Connection = con;
    com.CommandType = CommandType.StoredProcedure;
    com.CommandText = "PMEWDirectorates_Insert";
    //da.SelectCommand = com;


    com.Parameters.AddWithValue("@PMEWDirectorate_Name", PMEWDirectorate_Name);
    com.Parameters.AddWithValue("@PMEWDirectorate_Head", PMEWDirectorate_Head);
    com.ExecuteNonQuery();

    return "0";

    }
    catch(Exception ex)
    {
    return ex.Message;
    }
    finally
    {
    con.Close();

    // da.Dispose();
    com.Dispose();
    con.Dispose();
    }

    }
    }

    public partial class Parts_PMEWDirectorate : System.Web.UI.UserControl
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void btn_save_Click(object sender, EventArgs e)
    {
    btn_save.Enabled = false;

    lookups insert = new lookups();
    string result = insert.PMEWDirectorates_Insert(txt_DirName.Text.Trim(), txt_DirHead.Text.Trim());
    if (result == "0")
    {
    lbl_msg.ForeColor = System.Drawing.Color.Blue;
    lbl_msg.Text = "Successful";
    }
    else
    {
    lbl_msg.ForeColor = System.Drawing.Color.Red;
    lbl_msg.Text = result;
    }
    btn_save.Enabled = true;
    }


    2- How can i put a grid view to view the records he entered?
    public DataSet PMEWDirectorates_GetAll()
    {
    SqlConnection con = new SqlConnection();
    SqlCommand com = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    try
    {
    con.ConnectionString = conString;
    con.Open();
    com.Connection = con;
    com.CommandType = CommandType.StoredProcedure;
    com.CommandText = "PMEWDirectorates_GetAll";
    da.SelectCommand = com;


    com.Parameters.AddWithValue("@ID", DBNull.Value);
    com.ExecuteNonQuery();
    da.Fill(ds);
    return ds;

    }
    catch
    {
    return ds;
    }
    finally
    {
    con.Close();
    ds.Dispose();
    da.Dispose();
    com.Dispose();
    con.Dispose();
    }

    }


    Thank you

  2. #2
    Join Date
    Jul 2005
    Location
    Louisville, KY
    Posts
    201

    Re: Restrict text input

    Hello Nmohammed, the following code will allow only a-z and A-Z characters to be entered into the textbox. You'll need to create the KeyDown event for the textboxes, in order to utilize the code. In the future, I'd also like to suggest, for readability, to use [ code ] tags, on your code. Enjoy.

    Code:
    private void txtTextbox1_KeyDown(object sender, KeyEventArgs e)
    {
        if ((e.Key < Key.A) || (e.Key > Key.Z))
           e.Handled = true;
    }
    Regards,
    Quinn

    If this post resolves your question, please make sure to flag post as resolved and please rate up this post. Thanks.
    Last edited by QuinnJohns; October 16th, 2011 at 03:06 AM.

  3. #3
    Join Date
    Oct 2011
    Posts
    7

    Re: Restrict text input

    Thank you very much

    Is there a solution for my second question, How can i put a grid view to view the records from the database?
    Code:
    public DataSet PMEWDirectorates_GetAll()
    {
    SqlConnection con = new SqlConnection();
    SqlCommand com = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    try
    {
    con.ConnectionString = conString;
    con.Open();
    com.Connection = con;
    com.CommandType = CommandType.StoredProcedure;
    com.CommandText = "PMEWDirectorates_GetAll";
    da.SelectCommand = com;
    
    
    com.Parameters.AddWithValue("@ID", DBNull.Value);
    com.ExecuteNonQuery();
    da.Fill(ds);
    return ds;
    
    }
    catch
    {
    return ds;
    }
    finally
    {
    con.Close();
    ds.Dispose();
    da.Dispose();
    com.Dispose();
    con.Dispose();
    }
    
    }

  4. #4
    Join Date
    Jul 2005
    Location
    Louisville, KY
    Posts
    201

    Re: Restrict text input

    Well, assuming you have the control, usually you can just set the Datasource element in code, to that of the Dataset.

    See the following:

    Code:
    DataSet ds = PMEWDirectorates_GetAll();
    if (ds.Tables.Count > 0)
    {
         GridView.DataSource = ds;
         GridView.DataBind();
    }
    else
    {
         Message.Text = "Unable to connect to the database.";
    }
    Regards,
    Quinn

    If this post resolves your question, please make sure to flag post as resolved. Thanks.
    Last edited by QuinnJohns; October 16th, 2011 at 10:55 AM.

  5. #5
    Join Date
    Oct 2011
    Posts
    7

    Re: Restrict text input

    where shall i include this code?

  6. #6
    Join Date
    Jul 2005
    Location
    Louisville, KY
    Posts
    201

    Resolved Re: Restrict text input

    Wherever, you intend on populating this content into the control. So, a Form_Load for example.

  7. #7
    Join Date
    Oct 2011
    Posts
    7

    Re: Restrict text input

    sorry to ask this question , but which control do you mean ? the gridview?

  8. #8
    Join Date
    Oct 2011
    Posts
    7

    Re: Restrict text input

    My public DataSet PMEWDirectorates_GetAll() is located in a .cs file called functions.cs and i'm writing my code in a .ascx.cs page

    i want to display the results in a grid view in this page

  9. #9
    Join Date
    Jul 2005
    Location
    Louisville, KY
    Posts
    201

    Re: Restrict text input

    Quote Originally Posted by nmohammed View Post
    My public DataSet PMEWDirectorates_GetAll() is located in a .cs file called functions.cs and i'm writing my code in a .ascx.cs page

    i want to display the results in a grid view in this page
    You need to learn how to access a public function in another class, please see google for help.

    Regards,
    Quinn

  10. #10
    Join Date
    Oct 2011
    Posts
    7

    Re: Restrict text input

    Thank you
    solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured