-3

I am creating a simple CRUD system in C#. While doing the project i ran into the problem with auto increment id of c#. I receive an error for this line:

int i = Mid(tmp, 3);

The error is "Mid doesn't exist in the current context".

How should I fix this?

public string AutoID()
{
    SqlDataAdapter dr = new SqlDataAdapter("select pid from passanger", con);
    dr.Fill(ds, "passanger");

    dt = ds.Tables["passanger"];

    int k = dt.Rows.Count;

    if(k==0)
    {
        return "C0001";
    }
    else
    {
        DataRow r1 = dt.Rows[k - 1];
        string tmp = r1["pid"].ToString();
        int i =  Mid(tmp, 3);
        i = i + 1;

        if(i<10)
        {
            return "C000" + i;
        }
        else if(i < 100)
        {
            return "C00" + i;
        }

        else if (i > 100)
        {
            return "C0" + i;
        }
        else
        {
            return "C0" + i;
        }

    }
}
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
jaya priya
  • 75
  • 8
  • what do you expect the "Mid" function to do? – Tim Rutter Oct 31 '19 at 08:39
  • int i = Mid(tmp, 3); for the auto id increment purpose – jaya priya Oct 31 '19 at 08:41
  • 1
    You don't understand my question. What do you expect Mid to do? is it taking the character at position 3 in the string and converting it to an integer? – Tim Rutter Oct 31 '19 at 09:22
  • 1
    Need clarification, so many weird things. The whole if/return thing is a simple [padding with zero](https://stackoverflow.com/questions/4325267/c-sharp-convert-int-to-string-with-padding-zeros). Mid function may be "remove first char" int parse. It cannot be VB Mid as Mid even in VB return a string not an int, right? – Drag and Drop Oct 31 '19 at 09:39

3 Answers3

2

The equivalent of the VB function Mid for C# is String.Substring.

Your example would look like this:

//[...]
DataRow r1 = dt.Rows[k - 1];
string tmp = r1["pid"].ToString();
tmp = tmp.Substring(3); // this is where you had Mid(...)
int i = int.Parse(tmp); // converting to int
i = i + 1;
//[...]

Note that there's no error handling here for the case that e. g. tmp is shorter than four characters or that it's not a valid number (for int.Parse).

germi
  • 4,628
  • 1
  • 21
  • 38
1

Use Substring

String value = "This is a string.";
int startIndex = 5;
int length = 2
String substring = value.Substring(startIndex, length);
Console.WriteLine(substring);

https://learn.microsoft.com/en-us/dotnet/api/system.string.substring?view=netframework-4.8

jasttim
  • 723
  • 8
  • 19
1

This is bad practice to increment an ID in code. Sql Server has identity fields that will do this for you. You will run into big problems if the application has multiple users.

That said, to make your code work... 1. Use using blocks for your database object. They need to be closed and disposed and using will do this for you even if there is an error. Keep your connection local so you can be sure it is closed and disposed.

  1. I don't think Sql Server guarentees the order that records are returned. That makes the dependability of this method questionable. Don't try to add an Order By clause of pid because pid is a string and will order by alphabetical order not numeric order.

  2. You don't need a DataAdapter or DataSet. Just a Command and DataTable.

Now to the creating the new ID.

  1. You can't do arithmetic with a string, so, we take the SubString and use int.Parse to get the number. Notice that .SubString(1) retrieves the second character to the end of the string. The parse accepts the zeros.

  2. Now that we have a number we can do arithmetic. The += is just a shortcut.

  3. Turn it back to a string and pad the string with zeros to make a total length of 4.

  4. Append C to the start of the string.

    public string AutoID()
    {
        DataTable dt = new DataTable();
        using (SqlConnection con = new SqlConnection("Your connection string"))
        {
            using (SqlCommand cmd = new SqlCommand("select pid from passanger", con))
            {
                con.Open();
                dt.Load(cmd.ExecuteReader());
            }
        }
        int k = dt.Rows.Count;
    
        if (k == 0)
        {
            return "C0001";
        }
        else
        {
            DataRow r1 = dt.Rows[k - 1];
            string oldID = r1["pid"].ToString();
            int i = int.Parse(oldID.Substring(1));
            i += 1;
            string newID = i.ToString();
            newID = newID.PadLeft(4, '0');
            newID = "C" + newID;
            return newID;
        }
    }
    
Mary
  • 14,926
  • 3
  • 18
  • 27
  • int i = int.Parse(oldID.Substring(1)); //Input string was not in a correct format. error displayed – jaya priya Oct 31 '19 at 10:12
  • @jayapriya I made the assumption that pid was a string with a format of C####. Is that true? To restate a string 5 characters long starting with a C and followed by 4 digits. – Mary Oct 31 '19 at 10:15